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 } from './core-utils'
6 import { logger } from './logger'
7 import { join } from 'path'
8 import { Instance as ParseTorrent } from 'parse-torrent'
9 import { remove } from 'fs-extra'
11 function deleteFileAsync (path: string) {
13 .catch(err => logger.error('Cannot delete the file %s asynchronously.', path, { err }))
16 async function generateRandomString (size: number) {
17 const raw = await pseudoRandomBytesPromise(size)
19 return raw.toString('hex')
22 interface FormattableToJSON {
23 toFormattedJSON (args?: any)
26 function getFormattedObjects<U, T extends FormattableToJSON> (objects: T[], objectsTotal: number, formattedArg?: any) {
27 const formattedObjects: U[] = []
29 objects.forEach(object => {
30 formattedObjects.push(object.toFormattedJSON(formattedArg))
35 data: formattedObjects
39 async function getServerActor () {
40 if (getServerActor.serverActor === undefined) {
41 const application = await ApplicationModel.load()
42 if (!application) throw Error('Could not load Application from database.')
44 getServerActor.serverActor = application.Account.Actor
47 if (!getServerActor.serverActor) {
48 logger.error('Cannot load server actor.')
52 return Promise.resolve(getServerActor.serverActor)
54 namespace getServerActor {
55 export let serverActor: ActorModel
58 function generateVideoTmpPath (target: string | ParseTorrent) {
59 const id = typeof target === 'string' ? target : target.infoHash
61 const hash = sha256(id)
62 return join(CONFIG.STORAGE.VIDEOS_DIR, hash + '-import.mp4')
65 function getSecureTorrentName (originalName: string) {
66 return sha256(originalName) + '.torrent'
69 // ---------------------------------------------------------------------------