]>
Commit | Line | Data |
---|---|---|
1 | import { ResultList } from '../../shared' | |
2 | import { ApplicationModel } from '../models/application/application' | |
3 | import { execPromise, execPromise2, randomBytesPromise, sha256 } from './core-utils' | |
4 | import { logger } from './logger' | |
5 | import { join } from 'path' | |
6 | import { Instance as ParseTorrent } from 'parse-torrent' | |
7 | import { remove } from 'fs-extra' | |
8 | import * as memoizee from 'memoizee' | |
9 | import { CONFIG } from '../initializers/config' | |
10 | import { isVideoFileExtnameValid } from './custom-validators/videos' | |
11 | ||
12 | function deleteFileAsync (path: string) { | |
13 | remove(path) | |
14 | .catch(err => logger.error('Cannot delete the file %s asynchronously.', path, { err })) | |
15 | } | |
16 | ||
17 | async function generateRandomString (size: number) { | |
18 | const raw = await randomBytesPromise(size) | |
19 | ||
20 | return raw.toString('hex') | |
21 | } | |
22 | ||
23 | interface FormattableToJSON<U, V> { | |
24 | toFormattedJSON (args?: U): V | |
25 | } | |
26 | ||
27 | function getFormattedObjects<U, V, T extends FormattableToJSON<U, V>> (objects: T[], objectsTotal: number, formattedArg?: U) { | |
28 | const formattedObjects = objects.map(o => o.toFormattedJSON(formattedArg)) | |
29 | ||
30 | return { | |
31 | total: objectsTotal, | |
32 | data: formattedObjects | |
33 | } as ResultList<V> | |
34 | } | |
35 | ||
36 | const getServerActor = memoizee(async function () { | |
37 | const application = await ApplicationModel.load() | |
38 | if (!application) throw Error('Could not load Application from database.') | |
39 | ||
40 | const actor = application.Account.Actor | |
41 | actor.Account = application.Account | |
42 | ||
43 | return actor | |
44 | }, { promise: true }) | |
45 | ||
46 | function generateVideoImportTmpPath (target: string | ParseTorrent, extensionArg?: string) { | |
47 | const id = typeof target === 'string' | |
48 | ? target | |
49 | : target.infoHash | |
50 | ||
51 | let extension = '.mp4' | |
52 | if (extensionArg && isVideoFileExtnameValid(extensionArg)) { | |
53 | extension = extensionArg | |
54 | } | |
55 | ||
56 | const hash = sha256(id) | |
57 | return join(CONFIG.STORAGE.TMP_DIR, `${hash}-import${extension}`) | |
58 | } | |
59 | ||
60 | function getSecureTorrentName (originalName: string) { | |
61 | return sha256(originalName) + '.torrent' | |
62 | } | |
63 | ||
64 | async function getServerCommit () { | |
65 | try { | |
66 | const tag = await execPromise2( | |
67 | '[ ! -d .git ] || git name-rev --name-only --tags --no-undefined HEAD 2>/dev/null || true', | |
68 | { stdio: [ 0, 1, 2 ] } | |
69 | ) | |
70 | ||
71 | if (tag) return tag.replace(/^v/, '') | |
72 | } catch (err) { | |
73 | logger.debug('Cannot get version from git tags.', { err }) | |
74 | } | |
75 | ||
76 | try { | |
77 | const version = await execPromise('[ ! -d .git ] || git rev-parse --short HEAD') | |
78 | ||
79 | if (version) return version.toString().trim() | |
80 | } catch (err) { | |
81 | logger.debug('Cannot get version from git HEAD.', { err }) | |
82 | } | |
83 | ||
84 | return '' | |
85 | } | |
86 | ||
87 | /** | |
88 | * From a filename like "ede4cba5-742b-46fa-a388-9a6eb3a3aeb3.mp4", returns | |
89 | * only the "ede4cba5-742b-46fa-a388-9a6eb3a3aeb3" part. If the filename does | |
90 | * not contain a UUID, returns null. | |
91 | */ | |
92 | function getUUIDFromFilename (filename: string) { | |
93 | const regex = /[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/ | |
94 | const result = filename.match(regex) | |
95 | ||
96 | if (!result || Array.isArray(result) === false) return null | |
97 | ||
98 | return result[0] | |
99 | } | |
100 | ||
101 | // --------------------------------------------------------------------------- | |
102 | ||
103 | export { | |
104 | deleteFileAsync, | |
105 | generateRandomString, | |
106 | getFormattedObjects, | |
107 | getSecureTorrentName, | |
108 | getServerActor, | |
109 | getServerCommit, | |
110 | generateVideoImportTmpPath, | |
111 | getUUIDFromFilename | |
112 | } |