diff --git a/server/helpers/core-utils.ts b/server/helpers/core-utils.ts index 9ff67c43a..7e8252aa4 100644 --- a/server/helpers/core-utils.ts +++ b/server/helpers/core-utils.ts @@ -179,18 +179,15 @@ function buildPath (path: string) { } // Consistent with .length, lodash truncate function is not -function peertubeTruncate (str: string, maxLength: number) { - const options = { - length: maxLength - } +function peertubeTruncate (str: string, options: { length: number, separator?: RegExp, omission?: string }) { const truncatedStr = truncate(str, options) // The truncated string is okay, we can return it - if (truncatedStr.length <= maxLength) return truncatedStr + if (truncatedStr.length <= options.length) return truncatedStr // 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 -= truncatedStr.length - maxLength + options.length -= truncatedStr.length - options.length return truncate(str, options) } diff --git a/server/helpers/custom-validators/activitypub/actor.ts b/server/helpers/custom-validators/activitypub/actor.ts index 55bc8cc96..4e9aabf0e 100644 --- a/server/helpers/custom-validators/activitypub/actor.ts +++ b/server/helpers/custom-validators/activitypub/actor.ts @@ -1,9 +1,9 @@ import * as validator from 'validator' import { CONSTRAINTS_FIELDS } from '../../../initializers/constants' import { exists, isArray } from '../misc' -import { truncate } from 'lodash' import { isActivityPubUrlValid, isBaseActivityValid, setValidAttributedTo } from './misc' import { isHostValid } from '../servers' +import { peertubeTruncate } from '@server/helpers/core-utils' function isActorEndpointsObjectValid (endpointObject: any) { return isActivityPubUrlValid(endpointObject.sharedInbox) @@ -88,7 +88,7 @@ function normalizeActor (actor: any) { } if (actor.summary && typeof actor.summary === 'string') { - actor.summary = truncate(actor.summary, { length: CONSTRAINTS_FIELDS.USERS.DESCRIPTION.max }) + actor.summary = peertubeTruncate(actor.summary, { length: CONSTRAINTS_FIELDS.USERS.DESCRIPTION.max }) if (actor.summary.length < CONSTRAINTS_FIELDS.USERS.DESCRIPTION.min) { actor.summary = null diff --git a/server/helpers/custom-validators/activitypub/videos.ts b/server/helpers/custom-validators/activitypub/videos.ts index 3ba6b0744..02f914326 100644 --- a/server/helpers/custom-validators/activitypub/videos.ts +++ b/server/helpers/custom-validators/activitypub/videos.ts @@ -155,7 +155,7 @@ function setValidRemoteVideoUrls (video: any) { function setRemoteVideoTruncatedContent (video: any) { if (video.content) { - video.content = peertubeTruncate(video.content, CONSTRAINTS_FIELDS.VIDEOS.TRUNCATED_DESCRIPTION.max) + video.content = peertubeTruncate(video.content, { length: CONSTRAINTS_FIELDS.VIDEOS.TRUNCATED_DESCRIPTION.max }) } return true diff --git a/server/helpers/youtube-dl.ts b/server/helpers/youtube-dl.ts index b3079370f..87a0d0584 100644 --- a/server/helpers/youtube-dl.ts +++ b/server/helpers/youtube-dl.ts @@ -1,10 +1,9 @@ -import { truncate } from 'lodash' import { CONSTRAINTS_FIELDS, VIDEO_CATEGORIES } from '../initializers/constants' import { logger } from './logger' import { generateVideoImportTmpPath } from './utils' import { join } from 'path' -import { root } from './core-utils' -import { ensureDir, writeFile, remove } from 'fs-extra' +import { peertubeTruncate, root } from './core-utils' +import { ensureDir, remove, writeFile } from 'fs-extra' import * as request from 'request' import { createWriteStream } from 'fs' @@ -212,20 +211,20 @@ function buildVideoInfo (obj: any) { } function titleTruncation (title: string) { - return truncate(title, { - 'length': CONSTRAINTS_FIELDS.VIDEOS.NAME.max, - 'separator': /,? +/, - 'omission': ' […]' + return peertubeTruncate(title, { + length: CONSTRAINTS_FIELDS.VIDEOS.NAME.max, + separator: /,? +/, + omission: ' […]' }) } function descriptionTruncation (description: string) { if (!description || description.length < CONSTRAINTS_FIELDS.VIDEOS.DESCRIPTION.min) return undefined - return truncate(description, { - 'length': CONSTRAINTS_FIELDS.VIDEOS.DESCRIPTION.max, - 'separator': /,? +/, - 'omission': ' […]' + return peertubeTruncate(description, { + length: CONSTRAINTS_FIELDS.VIDEOS.DESCRIPTION.max, + separator: /,? +/, + omission: ' […]' }) } diff --git a/server/models/video/video.ts b/server/models/video/video.ts index 6856dcd9f..0ee3feaf3 100644 --- a/server/models/video/video.ts +++ b/server/models/video/video.ts @@ -1920,7 +1920,7 @@ export class VideoModel extends Model { if (!this.description) return null const maxLength = CONSTRAINTS_FIELDS.VIDEOS.TRUNCATED_DESCRIPTION.max - return peertubeTruncate(this.description, maxLength) + return peertubeTruncate(this.description, { length: maxLength }) } getOriginalFileResolution () {