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