From 414f1d11582510f77ea3a927053fe68fa160278b Mon Sep 17 00:00:00 2001 From: Johann150 Date: Wed, 29 Sep 2021 18:44:22 +0200 Subject: fix: truncate image descriptions (#7699) * move truncate function to separate file to reuse it * truncate image descriptions * show image description limit in UI * correctly treat null Co-authored-by: nullobsi * make truncate Unicode-aware The strings that truncate returns should now be valid Unicode. PostgreSQL also counts Unicode Code Points instead of bytes so this should be correct. * move truncate to internal, validate in API Truncating could also be done in src/services/drive/add-file.ts or src/services/drive/upload-from-url.ts but those would also affect local images. But local images should result in a hard error if the image comment is too long. * avoid overwriting Co-authored-by: nullobsi --- src/server/api/endpoints/drive/files/update.ts | 3 ++- src/server/api/endpoints/drive/files/upload-from-url.ts | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) (limited to 'src/server/api/endpoints') diff --git a/src/server/api/endpoints/drive/files/update.ts b/src/server/api/endpoints/drive/files/update.ts index 1ef445625c..f277a9c3dc 100644 --- a/src/server/api/endpoints/drive/files/update.ts +++ b/src/server/api/endpoints/drive/files/update.ts @@ -4,6 +4,7 @@ import { publishDriveStream } from '@/services/stream'; import define from '../../../define'; import { ApiError } from '../../../error'; import { DriveFiles, DriveFolders } from '@/models/index'; +import { DB_MAX_IMAGE_COMMENT_LENGTH } from '@/misc/hard-limits'; export const meta = { tags: ['drive'], @@ -33,7 +34,7 @@ export const meta = { }, comment: { - validator: $.optional.nullable.str, + validator: $.optional.nullable.str.max(DB_MAX_IMAGE_COMMENT_LENGTH), default: undefined as any, } }, diff --git a/src/server/api/endpoints/drive/files/upload-from-url.ts b/src/server/api/endpoints/drive/files/upload-from-url.ts index f37f316efb..9f10a42d24 100644 --- a/src/server/api/endpoints/drive/files/upload-from-url.ts +++ b/src/server/api/endpoints/drive/files/upload-from-url.ts @@ -5,6 +5,7 @@ import uploadFromUrl from '@/services/drive/upload-from-url'; import define from '../../../define'; import { DriveFiles } from '@/models/index'; import { publishMainStream } from '@/services/stream'; +import { DB_MAX_IMAGE_COMMENT_LENGTH } from '@/misc/hard-limits'; export const meta = { tags: ['drive'], @@ -35,7 +36,7 @@ export const meta = { }, comment: { - validator: $.optional.nullable.str, + validator: $.optional.nullable.str.max(DB_MAX_IMAGE_COMMENT_LENGTH), default: null, }, -- cgit v1.2.3-freya