aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/helpers/utils.ts
diff options
context:
space:
mode:
Diffstat (limited to 'server/helpers/utils.ts')
-rw-r--r--server/helpers/utils.ts29
1 files changed, 12 insertions, 17 deletions
diff --git a/server/helpers/utils.ts b/server/helpers/utils.ts
index 4c6f200f8..ad3b7949e 100644
--- a/server/helpers/utils.ts
+++ b/server/helpers/utils.ts
@@ -1,12 +1,11 @@
1import { ResultList } from '../../shared' 1import { ResultList } from '../../shared'
2import { ApplicationModel } from '../models/application/application' 2import { execPromise, execPromise2, randomBytesPromise, sha256 } from './core-utils'
3import { execPromise, execPromise2, pseudoRandomBytesPromise, sha256 } from './core-utils'
4import { logger } from './logger' 3import { logger } from './logger'
5import { join } from 'path' 4import { join } from 'path'
6import { Instance as ParseTorrent } from 'parse-torrent' 5import { Instance as ParseTorrent } from 'parse-torrent'
7import { remove } from 'fs-extra' 6import { remove } from 'fs-extra'
8import * as memoizee from 'memoizee'
9import { CONFIG } from '../initializers/config' 7import { CONFIG } from '../initializers/config'
8import { isVideoFileExtnameValid } from './custom-validators/videos'
10 9
11function deleteFileAsync (path: string) { 10function deleteFileAsync (path: string) {
12 remove(path) 11 remove(path)
@@ -14,7 +13,7 @@ function deleteFileAsync (path: string) {
14} 13}
15 14
16async function generateRandomString (size: number) { 15async function generateRandomString (size: number) {
17 const raw = await pseudoRandomBytesPromise(size) 16 const raw = await randomBytesPromise(size)
18 17
19 return raw.toString('hex') 18 return raw.toString('hex')
20} 19}
@@ -32,21 +31,18 @@ function getFormattedObjects<U, V, T extends FormattableToJSON<U, V>> (objects:
32 } as ResultList<V> 31 } as ResultList<V>
33} 32}
34 33
35const getServerActor = memoizee(async function () { 34function generateVideoImportTmpPath (target: string | ParseTorrent, extensionArg?: string) {
36 const application = await ApplicationModel.load() 35 const id = typeof target === 'string'
37 if (!application) throw Error('Could not load Application from database.') 36 ? target
37 : target.infoHash
38 38
39 const actor = application.Account.Actor 39 let extension = '.mp4'
40 actor.Account = application.Account 40 if (extensionArg && isVideoFileExtnameValid(extensionArg)) {
41 41 extension = extensionArg
42 return actor 42 }
43}, { promise: true })
44
45function generateVideoImportTmpPath (target: string | ParseTorrent) {
46 const id = typeof target === 'string' ? target : target.infoHash
47 43
48 const hash = sha256(id) 44 const hash = sha256(id)
49 return join(CONFIG.STORAGE.TMP_DIR, hash + '-import.mp4') 45 return join(CONFIG.STORAGE.TMP_DIR, `${hash}-import${extension}`)
50} 46}
51 47
52function getSecureTorrentName (originalName: string) { 48function getSecureTorrentName (originalName: string) {
@@ -97,7 +93,6 @@ export {
97 generateRandomString, 93 generateRandomString,
98 getFormattedObjects, 94 getFormattedObjects,
99 getSecureTorrentName, 95 getSecureTorrentName,
100 getServerActor,
101 getServerCommit, 96 getServerCommit,
102 generateVideoImportTmpPath, 97 generateVideoImportTmpPath,
103 getUUIDFromFilename 98 getUUIDFromFilename