From bffbebbe6b33ce306e7ec1b6051f0e51521c0440 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Wed, 21 Mar 2018 15:00:58 +0100 Subject: Fix issues with truncated description and utf characters --- server/models/video/video.ts | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) (limited to 'server/models') diff --git a/server/models/video/video.ts b/server/models/video/video.ts index 4e175c410..f43b73e49 100644 --- a/server/models/video/video.ts +++ b/server/models/video/video.ts @@ -1166,10 +1166,19 @@ export class VideoModel extends Model { getTruncatedDescription () { if (!this.description) return null + const maxLength = CONSTRAINTS_FIELDS.VIDEOS.TRUNCATED_DESCRIPTION.max + const options = { - length: CONSTRAINTS_FIELDS.VIDEOS.TRUNCATED_DESCRIPTION.max + length: maxLength } + const truncatedDescription = truncate(this.description, options) + + // The truncated string is okay, we can return it + if (truncatedDescription.length <= maxLength) return truncatedDescription + // Lodash takes into account all UTF characters, whereas String.prototype.length does not: some characters have a length of 2 + // We always use the .length so we need to truncate more if needed + options.length -= maxLength - truncatedDescription.length return truncate(this.description, options) } -- cgit v1.2.3