1 import { ResultList } from '../../shared'
2 import { CONFIG } from '../initializers'
3 import { ActorModel } from '../models/activitypub/actor'
4 import { ApplicationModel } from '../models/application/application'
5 import { pseudoRandomBytesPromise, sha256, unlinkPromise } from './core-utils'
6 import { logger } from './logger'
7 import { join } from 'path'
8 import { Instance as ParseTorrent } from 'parse-torrent'
10 function deleteFileAsync (path: string) {
12 .catch(err => logger.error('Cannot delete the file %s asynchronously.', path, { err }))
15 async function generateRandomString (size: number) {
16 const raw = await pseudoRandomBytesPromise(size)
18 return raw.toString('hex')
21 interface FormattableToJSON {
22 toFormattedJSON (args?: any)
25 function getFormattedObjects<U, T extends FormattableToJSON> (objects: T[], objectsTotal: number, formattedArg?: any) {
26 const formattedObjects: U[] = []
28 objects.forEach(object => {
29 formattedObjects.push(object.toFormattedJSON(formattedArg))
34 data: formattedObjects
38 async function getServerActor () {
39 if (getServerActor.serverActor === undefined) {
40 const application = await ApplicationModel.load()
41 if (!application) throw Error('Could not load Application from database.')
43 getServerActor.serverActor = application.Account.Actor
46 if (!getServerActor.serverActor) {
47 logger.error('Cannot load server actor.')
51 return Promise.resolve(getServerActor.serverActor)
53 namespace getServerActor {
54 export let serverActor: ActorModel
57 function generateVideoTmpPath (target: string | ParseTorrent) {
58 const id = typeof target === 'string' ? target : target.infoHash
60 const hash = sha256(id)
61 return join(CONFIG.STORAGE.VIDEOS_DIR, hash + '-import.mp4')
64 function getSecureTorrentName (originalName: string) {
65 return sha256(originalName) + '.torrent'
68 // ---------------------------------------------------------------------------