diff options
-rw-r--r-- | server/helpers/core-utils.ts | 9 | ||||
-rw-r--r-- | server/helpers/custom-validators/activitypub/actor.ts | 4 | ||||
-rw-r--r-- | server/helpers/custom-validators/activitypub/videos.ts | 2 | ||||
-rw-r--r-- | server/helpers/youtube-dl.ts | 21 | ||||
-rw-r--r-- | server/models/video/video.ts | 2 |
5 files changed, 17 insertions, 21 deletions
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) { | |||
179 | } | 179 | } |
180 | 180 | ||
181 | // Consistent with .length, lodash truncate function is not | 181 | // Consistent with .length, lodash truncate function is not |
182 | function peertubeTruncate (str: string, maxLength: number) { | 182 | function peertubeTruncate (str: string, options: { length: number, separator?: RegExp, omission?: string }) { |
183 | const options = { | ||
184 | length: maxLength | ||
185 | } | ||
186 | const truncatedStr = truncate(str, options) | 183 | const truncatedStr = truncate(str, options) |
187 | 184 | ||
188 | // The truncated string is okay, we can return it | 185 | // The truncated string is okay, we can return it |
189 | if (truncatedStr.length <= maxLength) return truncatedStr | 186 | if (truncatedStr.length <= options.length) return truncatedStr |
190 | 187 | ||
191 | // Lodash takes into account all UTF characters, whereas String.prototype.length does not: some characters have a length of 2 | 188 | // Lodash takes into account all UTF characters, whereas String.prototype.length does not: some characters have a length of 2 |
192 | // We always use the .length so we need to truncate more if needed | 189 | // We always use the .length so we need to truncate more if needed |
193 | options.length -= truncatedStr.length - maxLength | 190 | options.length -= truncatedStr.length - options.length |
194 | return truncate(str, options) | 191 | return truncate(str, options) |
195 | } | 192 | } |
196 | 193 | ||
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 @@ | |||
1 | import * as validator from 'validator' | 1 | import * as validator from 'validator' |
2 | import { CONSTRAINTS_FIELDS } from '../../../initializers/constants' | 2 | import { CONSTRAINTS_FIELDS } from '../../../initializers/constants' |
3 | import { exists, isArray } from '../misc' | 3 | import { exists, isArray } from '../misc' |
4 | import { truncate } from 'lodash' | ||
5 | import { isActivityPubUrlValid, isBaseActivityValid, setValidAttributedTo } from './misc' | 4 | import { isActivityPubUrlValid, isBaseActivityValid, setValidAttributedTo } from './misc' |
6 | import { isHostValid } from '../servers' | 5 | import { isHostValid } from '../servers' |
6 | import { peertubeTruncate } from '@server/helpers/core-utils' | ||
7 | 7 | ||
8 | function isActorEndpointsObjectValid (endpointObject: any) { | 8 | function isActorEndpointsObjectValid (endpointObject: any) { |
9 | return isActivityPubUrlValid(endpointObject.sharedInbox) | 9 | return isActivityPubUrlValid(endpointObject.sharedInbox) |
@@ -88,7 +88,7 @@ function normalizeActor (actor: any) { | |||
88 | } | 88 | } |
89 | 89 | ||
90 | if (actor.summary && typeof actor.summary === 'string') { | 90 | if (actor.summary && typeof actor.summary === 'string') { |
91 | actor.summary = truncate(actor.summary, { length: CONSTRAINTS_FIELDS.USERS.DESCRIPTION.max }) | 91 | actor.summary = peertubeTruncate(actor.summary, { length: CONSTRAINTS_FIELDS.USERS.DESCRIPTION.max }) |
92 | 92 | ||
93 | if (actor.summary.length < CONSTRAINTS_FIELDS.USERS.DESCRIPTION.min) { | 93 | if (actor.summary.length < CONSTRAINTS_FIELDS.USERS.DESCRIPTION.min) { |
94 | actor.summary = null | 94 | 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) { | |||
155 | 155 | ||
156 | function setRemoteVideoTruncatedContent (video: any) { | 156 | function setRemoteVideoTruncatedContent (video: any) { |
157 | if (video.content) { | 157 | if (video.content) { |
158 | video.content = peertubeTruncate(video.content, CONSTRAINTS_FIELDS.VIDEOS.TRUNCATED_DESCRIPTION.max) | 158 | video.content = peertubeTruncate(video.content, { length: CONSTRAINTS_FIELDS.VIDEOS.TRUNCATED_DESCRIPTION.max }) |
159 | } | 159 | } |
160 | 160 | ||
161 | return true | 161 | 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 @@ | |||
1 | import { truncate } from 'lodash' | ||
2 | import { CONSTRAINTS_FIELDS, VIDEO_CATEGORIES } from '../initializers/constants' | 1 | import { CONSTRAINTS_FIELDS, VIDEO_CATEGORIES } from '../initializers/constants' |
3 | import { logger } from './logger' | 2 | import { logger } from './logger' |
4 | import { generateVideoImportTmpPath } from './utils' | 3 | import { generateVideoImportTmpPath } from './utils' |
5 | import { join } from 'path' | 4 | import { join } from 'path' |
6 | import { root } from './core-utils' | 5 | import { peertubeTruncate, root } from './core-utils' |
7 | import { ensureDir, writeFile, remove } from 'fs-extra' | 6 | import { ensureDir, remove, writeFile } from 'fs-extra' |
8 | import * as request from 'request' | 7 | import * as request from 'request' |
9 | import { createWriteStream } from 'fs' | 8 | import { createWriteStream } from 'fs' |
10 | 9 | ||
@@ -212,20 +211,20 @@ function buildVideoInfo (obj: any) { | |||
212 | } | 211 | } |
213 | 212 | ||
214 | function titleTruncation (title: string) { | 213 | function titleTruncation (title: string) { |
215 | return truncate(title, { | 214 | return peertubeTruncate(title, { |
216 | 'length': CONSTRAINTS_FIELDS.VIDEOS.NAME.max, | 215 | length: CONSTRAINTS_FIELDS.VIDEOS.NAME.max, |
217 | 'separator': /,? +/, | 216 | separator: /,? +/, |
218 | 'omission': ' […]' | 217 | omission: ' […]' |
219 | }) | 218 | }) |
220 | } | 219 | } |
221 | 220 | ||
222 | function descriptionTruncation (description: string) { | 221 | function descriptionTruncation (description: string) { |
223 | if (!description || description.length < CONSTRAINTS_FIELDS.VIDEOS.DESCRIPTION.min) return undefined | 222 | if (!description || description.length < CONSTRAINTS_FIELDS.VIDEOS.DESCRIPTION.min) return undefined |
224 | 223 | ||
225 | return truncate(description, { | 224 | return peertubeTruncate(description, { |
226 | 'length': CONSTRAINTS_FIELDS.VIDEOS.DESCRIPTION.max, | 225 | length: CONSTRAINTS_FIELDS.VIDEOS.DESCRIPTION.max, |
227 | 'separator': /,? +/, | 226 | separator: /,? +/, |
228 | 'omission': ' […]' | 227 | omission: ' […]' |
229 | }) | 228 | }) |
230 | } | 229 | } |
231 | 230 | ||
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<VideoModel> { | |||
1920 | if (!this.description) return null | 1920 | if (!this.description) return null |
1921 | 1921 | ||
1922 | const maxLength = CONSTRAINTS_FIELDS.VIDEOS.TRUNCATED_DESCRIPTION.max | 1922 | const maxLength = CONSTRAINTS_FIELDS.VIDEOS.TRUNCATED_DESCRIPTION.max |
1923 | return peertubeTruncate(this.description, maxLength) | 1923 | return peertubeTruncate(this.description, { length: maxLength }) |
1924 | } | 1924 | } |
1925 | 1925 | ||
1926 | getOriginalFileResolution () { | 1926 | getOriginalFileResolution () { |