}
// 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)
}
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)
}
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
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
-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'
}
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: ' […]'
})
}
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 () {