]>
Commit | Line | Data |
---|---|---|
6fcd19ba | 1 | import { ResultList } from '../../shared' |
50d6de9c | 2 | import { ApplicationModel } from '../models/application/application' |
a1587156 | 3 | import { execPromise, execPromise2, randomBytesPromise, sha256 } from './core-utils' |
efc32059 | 4 | import { logger } from './logger' |
3e17515e | 5 | import { join } from 'path' |
990b6a0b | 6 | import { Instance as ParseTorrent } from 'parse-torrent' |
62689b94 | 7 | import { remove } from 'fs-extra' |
7348b1fd | 8 | import * as memoizee from 'memoizee' |
6dd9de95 | 9 | import { CONFIG } from '../initializers/config' |
d57d1d83 | 10 | import { isVideoFileExtnameValid } from './custom-validators/videos' |
cbe2f7c3 | 11 | |
cf7a61b5 | 12 | function deleteFileAsync (path: string) { |
62689b94 | 13 | remove(path) |
cf7a61b5 C |
14 | .catch(err => logger.error('Cannot delete the file %s asynchronously.', path, { err })) |
15 | } | |
16 | ||
f5028693 | 17 | async function generateRandomString (size: number) { |
a1587156 | 18 | const raw = await randomBytesPromise(size) |
f5028693 C |
19 | |
20 | return raw.toString('hex') | |
e4c87ec2 C |
21 | } |
22 | ||
b5fecbf4 C |
23 | interface FormattableToJSON<U, V> { |
24 | toFormattedJSON (args?: U): V | |
25 | } | |
26 | ||
a8b666e9 C |
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)) | |
55fa55a9 | 29 | |
2186386c | 30 | return { |
55fa55a9 | 31 | total: objectsTotal, |
0aef76c4 | 32 | data: formattedObjects |
a8b666e9 | 33 | } as ResultList<V> |
55fa55a9 C |
34 | } |
35 | ||
7348b1fd C |
36 | const getServerActor = memoizee(async function () { |
37 | const application = await ApplicationModel.load() | |
38 | if (!application) throw Error('Could not load Application from database.') | |
2cebd797 | 39 | |
7ad9b984 C |
40 | const actor = application.Account.Actor |
41 | actor.Account = application.Account | |
42 | ||
43 | return actor | |
35f28e94 | 44 | }, { promise: true }) |
7a7724e6 | 45 | |
d57d1d83 C |
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 | } | |
990b6a0b C |
55 | |
56 | const hash = sha256(id) | |
d57d1d83 | 57 | return join(CONFIG.STORAGE.TMP_DIR, `${hash}-import${extension}`) |
ce33919c C |
58 | } |
59 | ||
990b6a0b C |
60 | function getSecureTorrentName (originalName: string) { |
61 | return sha256(originalName) + '.torrent' | |
62 | } | |
792dbaf0 | 63 | |
1b5e2d72 | 64 | async function getServerCommit () { |
499d9015 C |
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 | } | |
abb2c792 | 83 | |
1b5e2d72 | 84 | return '' |
abb2c792 RK |
85 | } |
86 | ||
edb4ffc7 FA |
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 | ||
9f10b292 | 101 | // --------------------------------------------------------------------------- |
c45f7f84 | 102 | |
65fcc311 | 103 | export { |
cf7a61b5 | 104 | deleteFileAsync, |
65fcc311 | 105 | generateRandomString, |
0aef76c4 | 106 | getFormattedObjects, |
990b6a0b | 107 | getSecureTorrentName, |
50d6de9c | 108 | getServerActor, |
1b5e2d72 | 109 | getServerCommit, |
6040f87d | 110 | generateVideoImportTmpPath, |
edb4ffc7 | 111 | getUUIDFromFilename |
65fcc311 | 112 | } |