]>
Commit | Line | Data |
---|---|---|
6fcd19ba | 1 | import { ResultList } from '../../shared' |
0626e7af | 2 | import { CONFIG } from '../initializers' |
50d6de9c | 3 | import { ApplicationModel } from '../models/application/application' |
499d9015 | 4 | import { execPromise, execPromise2, pseudoRandomBytesPromise, sha256 } from './core-utils' |
efc32059 | 5 | import { logger } from './logger' |
3e17515e | 6 | import { join } from 'path' |
990b6a0b | 7 | import { Instance as ParseTorrent } from 'parse-torrent' |
62689b94 | 8 | import { remove } from 'fs-extra' |
7348b1fd | 9 | import * as memoizee from 'memoizee' |
a4101923 | 10 | import { isArray } from './custom-validators/misc' |
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 C |
17 | async function generateRandomString (size: number) { |
18 | const raw = await pseudoRandomBytesPromise(size) | |
19 | ||
20 | return raw.toString('hex') | |
e4c87ec2 C |
21 | } |
22 | ||
a4101923 | 23 | interface FormattableToJSON { toFormattedJSON (args?: any) } |
2186386c | 24 | function getFormattedObjects<U, T extends FormattableToJSON> (objects: T[], objectsTotal: number, formattedArg?: any) { |
0aef76c4 | 25 | const formattedObjects: U[] = [] |
55fa55a9 | 26 | |
075f16ca | 27 | objects.forEach(object => { |
2186386c | 28 | formattedObjects.push(object.toFormattedJSON(formattedArg)) |
55fa55a9 C |
29 | }) |
30 | ||
2186386c | 31 | return { |
55fa55a9 | 32 | total: objectsTotal, |
0aef76c4 | 33 | data: formattedObjects |
2186386c | 34 | } as ResultList<U> |
55fa55a9 C |
35 | } |
36 | ||
7348b1fd C |
37 | const getServerActor = memoizee(async function () { |
38 | const application = await ApplicationModel.load() | |
39 | if (!application) throw Error('Could not load Application from database.') | |
2cebd797 | 40 | |
7ad9b984 C |
41 | const actor = application.Account.Actor |
42 | actor.Account = application.Account | |
43 | ||
44 | return actor | |
7348b1fd | 45 | }) |
7a7724e6 | 46 | |
6040f87d | 47 | function generateVideoImportTmpPath (target: string | ParseTorrent) { |
990b6a0b C |
48 | const id = typeof target === 'string' ? target : target.infoHash |
49 | ||
50 | const hash = sha256(id) | |
6040f87d | 51 | return join(CONFIG.STORAGE.TMP_DIR, hash + '-import.mp4') |
ce33919c C |
52 | } |
53 | ||
990b6a0b C |
54 | function getSecureTorrentName (originalName: string) { |
55 | return sha256(originalName) + '.torrent' | |
56 | } | |
792dbaf0 | 57 | |
1b5e2d72 | 58 | async function getServerCommit () { |
499d9015 C |
59 | try { |
60 | const tag = await execPromise2( | |
61 | '[ ! -d .git ] || git name-rev --name-only --tags --no-undefined HEAD 2>/dev/null || true', | |
62 | { stdio: [ 0, 1, 2 ] } | |
63 | ) | |
64 | ||
65 | if (tag) return tag.replace(/^v/, '') | |
66 | } catch (err) { | |
67 | logger.debug('Cannot get version from git tags.', { err }) | |
68 | } | |
69 | ||
70 | try { | |
71 | const version = await execPromise('[ ! -d .git ] || git rev-parse --short HEAD') | |
72 | ||
73 | if (version) return version.toString().trim() | |
74 | } catch (err) { | |
75 | logger.debug('Cannot get version from git HEAD.', { err }) | |
76 | } | |
abb2c792 | 77 | |
1b5e2d72 | 78 | return '' |
abb2c792 RK |
79 | } |
80 | ||
edb4ffc7 FA |
81 | /** |
82 | * From a filename like "ede4cba5-742b-46fa-a388-9a6eb3a3aeb3.mp4", returns | |
83 | * only the "ede4cba5-742b-46fa-a388-9a6eb3a3aeb3" part. If the filename does | |
84 | * not contain a UUID, returns null. | |
85 | */ | |
86 | function getUUIDFromFilename (filename: string) { | |
87 | const regex = /[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/ | |
88 | const result = filename.match(regex) | |
89 | ||
90 | if (!result || Array.isArray(result) === false) return null | |
91 | ||
92 | return result[0] | |
93 | } | |
94 | ||
9f10b292 | 95 | // --------------------------------------------------------------------------- |
c45f7f84 | 96 | |
65fcc311 | 97 | export { |
cf7a61b5 | 98 | deleteFileAsync, |
65fcc311 | 99 | generateRandomString, |
0aef76c4 | 100 | getFormattedObjects, |
990b6a0b | 101 | getSecureTorrentName, |
50d6de9c | 102 | getServerActor, |
1b5e2d72 | 103 | getServerCommit, |
6040f87d | 104 | generateVideoImportTmpPath, |
edb4ffc7 | 105 | getUUIDFromFilename |
65fcc311 | 106 | } |