diff options
Diffstat (limited to 'server/helpers/core-utils.ts')
-rw-r--r-- | server/helpers/core-utils.ts | 18 |
1 files changed, 18 insertions, 0 deletions
diff --git a/server/helpers/core-utils.ts b/server/helpers/core-utils.ts index 65f18d644..a3dfe27b5 100644 --- a/server/helpers/core-utils.ts +++ b/server/helpers/core-utils.ts | |||
@@ -12,6 +12,7 @@ import { isAbsolute, join } from 'path' | |||
12 | import * as pem from 'pem' | 12 | import * as pem from 'pem' |
13 | import * as rimraf from 'rimraf' | 13 | import * as rimraf from 'rimraf' |
14 | import { URL } from 'url' | 14 | import { URL } from 'url' |
15 | import { truncate } from 'lodash' | ||
15 | 16 | ||
16 | function sanitizeUrl (url: string) { | 17 | function sanitizeUrl (url: string) { |
17 | const urlObject = new URL(url) | 18 | const urlObject = new URL(url) |
@@ -78,6 +79,22 @@ function buildPath (path: string) { | |||
78 | return join(root(), path) | 79 | return join(root(), path) |
79 | } | 80 | } |
80 | 81 | ||
82 | // Consistent with .length, lodash truncate function is not | ||
83 | function peertubeTruncate (str: string, maxLength: number) { | ||
84 | const options = { | ||
85 | length: maxLength | ||
86 | } | ||
87 | const truncatedStr = truncate(str, options) | ||
88 | |||
89 | // The truncated string is okay, we can return it | ||
90 | if (truncatedStr.length <= maxLength) return truncatedStr | ||
91 | |||
92 | // Lodash takes into account all UTF characters, whereas String.prototype.length does not: some characters have a length of 2 | ||
93 | // We always use the .length so we need to truncate more if needed | ||
94 | options.length -= truncatedStr.length - maxLength | ||
95 | return truncate(str, options) | ||
96 | } | ||
97 | |||
81 | function promisify0<A> (func: (cb: (err: any, result: A) => void) => void): () => Promise<A> { | 98 | function promisify0<A> (func: (cb: (err: any, result: A) => void) => void): () => Promise<A> { |
82 | return function promisified (): Promise<A> { | 99 | return function promisified (): Promise<A> { |
83 | return new Promise<A>((resolve: (arg: A) => void, reject: (err: any) => void) => { | 100 | return new Promise<A>((resolve: (arg: A) => void, reject: (err: any) => void) => { |
@@ -145,6 +162,7 @@ export { | |||
145 | sanitizeUrl, | 162 | sanitizeUrl, |
146 | sanitizeHost, | 163 | sanitizeHost, |
147 | buildPath, | 164 | buildPath, |
165 | peertubeTruncate, | ||
148 | 166 | ||
149 | promisify0, | 167 | promisify0, |
150 | promisify1, | 168 | promisify1, |