]>
Commit | Line | Data |
---|---|---|
6fcd19ba | 1 | import { ResultList } from '../../shared' |
0626e7af | 2 | import { CONFIG } from '../initializers' |
50d6de9c C |
3 | import { ActorModel } from '../models/activitypub/actor' |
4 | import { ApplicationModel } from '../models/application/application' | |
990b6a0b | 5 | import { pseudoRandomBytesPromise, sha256, unlinkPromise } from './core-utils' |
efc32059 | 6 | import { logger } from './logger' |
3e17515e | 7 | import { join } from 'path' |
990b6a0b | 8 | import { Instance as ParseTorrent } from 'parse-torrent' |
cbe2f7c3 | 9 | |
cf7a61b5 C |
10 | function deleteFileAsync (path: string) { |
11 | unlinkPromise(path) | |
12 | .catch(err => logger.error('Cannot delete the file %s asynchronously.', path, { err })) | |
13 | } | |
14 | ||
f5028693 C |
15 | async function generateRandomString (size: number) { |
16 | const raw = await pseudoRandomBytesPromise(size) | |
17 | ||
18 | return raw.toString('hex') | |
e4c87ec2 C |
19 | } |
20 | ||
40298b02 | 21 | interface FormattableToJSON { |
2186386c | 22 | toFormattedJSON (args?: any) |
154898b0 C |
23 | } |
24 | ||
2186386c | 25 | function getFormattedObjects<U, T extends FormattableToJSON> (objects: T[], objectsTotal: number, formattedArg?: any) { |
0aef76c4 | 26 | const formattedObjects: U[] = [] |
55fa55a9 | 27 | |
075f16ca | 28 | objects.forEach(object => { |
2186386c | 29 | formattedObjects.push(object.toFormattedJSON(formattedArg)) |
55fa55a9 C |
30 | }) |
31 | ||
2186386c | 32 | return { |
55fa55a9 | 33 | total: objectsTotal, |
0aef76c4 | 34 | data: formattedObjects |
2186386c | 35 | } as ResultList<U> |
55fa55a9 C |
36 | } |
37 | ||
50d6de9c | 38 | async function getServerActor () { |
06215f15 | 39 | if (getServerActor.serverActor === undefined) { |
50d6de9c | 40 | const application = await ApplicationModel.load() |
2cebd797 C |
41 | if (!application) throw Error('Could not load Application from database.') |
42 | ||
06215f15 | 43 | getServerActor.serverActor = application.Account.Actor |
7a7724e6 C |
44 | } |
45 | ||
06215f15 | 46 | if (!getServerActor.serverActor) { |
50d6de9c | 47 | logger.error('Cannot load server actor.') |
efc32059 C |
48 | process.exit(0) |
49 | } | |
50 | ||
06215f15 C |
51 | return Promise.resolve(getServerActor.serverActor) |
52 | } | |
53 | namespace getServerActor { | |
54 | export let serverActor: ActorModel | |
7a7724e6 C |
55 | } |
56 | ||
990b6a0b C |
57 | function generateVideoTmpPath (target: string | ParseTorrent) { |
58 | const id = typeof target === 'string' ? target : target.infoHash | |
59 | ||
60 | const hash = sha256(id) | |
ce33919c C |
61 | return join(CONFIG.STORAGE.VIDEOS_DIR, hash + '-import.mp4') |
62 | } | |
63 | ||
990b6a0b C |
64 | function getSecureTorrentName (originalName: string) { |
65 | return sha256(originalName) + '.torrent' | |
66 | } | |
792dbaf0 | 67 | |
9f10b292 | 68 | // --------------------------------------------------------------------------- |
c45f7f84 | 69 | |
65fcc311 | 70 | export { |
cf7a61b5 | 71 | deleteFileAsync, |
65fcc311 | 72 | generateRandomString, |
0aef76c4 | 73 | getFormattedObjects, |
990b6a0b | 74 | getSecureTorrentName, |
50d6de9c | 75 | getServerActor, |
ce33919c | 76 | generateVideoTmpPath |
65fcc311 | 77 | } |