diff options
Diffstat (limited to 'server/helpers/utils.ts')
-rw-r--r-- | server/helpers/utils.ts | 29 |
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 @@ | |||
1 | import { ResultList } from '../../shared' | 1 | import { ResultList } from '../../shared' |
2 | import { ApplicationModel } from '../models/application/application' | 2 | import { execPromise, execPromise2, randomBytesPromise, sha256 } from './core-utils' |
3 | import { execPromise, execPromise2, pseudoRandomBytesPromise, sha256 } from './core-utils' | ||
4 | import { logger } from './logger' | 3 | import { logger } from './logger' |
5 | import { join } from 'path' | 4 | import { join } from 'path' |
6 | import { Instance as ParseTorrent } from 'parse-torrent' | 5 | import { Instance as ParseTorrent } from 'parse-torrent' |
7 | import { remove } from 'fs-extra' | 6 | import { remove } from 'fs-extra' |
8 | import * as memoizee from 'memoizee' | ||
9 | import { CONFIG } from '../initializers/config' | 7 | import { CONFIG } from '../initializers/config' |
8 | import { isVideoFileExtnameValid } from './custom-validators/videos' | ||
10 | 9 | ||
11 | function deleteFileAsync (path: string) { | 10 | function deleteFileAsync (path: string) { |
12 | remove(path) | 11 | remove(path) |
@@ -14,7 +13,7 @@ function deleteFileAsync (path: string) { | |||
14 | } | 13 | } |
15 | 14 | ||
16 | async function generateRandomString (size: number) { | 15 | async 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 | ||
35 | const getServerActor = memoizee(async function () { | 34 | function 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 | |||
45 | function 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 | ||
52 | function getSecureTorrentName (originalName: string) { | 48 | function 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 |