]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/helpers/utils.ts
Underline links in feed popover when hovering
[github/Chocobozzz/PeerTube.git] / server / helpers / utils.ts
CommitLineData
6fcd19ba 1import { ResultList } from '../../shared'
0626e7af 2import { CONFIG } from '../initializers'
50d6de9c
C
3import { ActorModel } from '../models/activitypub/actor'
4import { ApplicationModel } from '../models/application/application'
990b6a0b 5import { pseudoRandomBytesPromise, sha256, unlinkPromise } from './core-utils'
efc32059 6import { logger } from './logger'
3e17515e 7import { join } from 'path'
990b6a0b 8import { Instance as ParseTorrent } from 'parse-torrent'
cbe2f7c3 9
cf7a61b5
C
10function deleteFileAsync (path: string) {
11 unlinkPromise(path)
12 .catch(err => logger.error('Cannot delete the file %s asynchronously.', path, { err }))
13}
14
f5028693
C
15async function generateRandomString (size: number) {
16 const raw = await pseudoRandomBytesPromise(size)
17
18 return raw.toString('hex')
e4c87ec2
C
19}
20
40298b02 21interface FormattableToJSON {
2186386c 22 toFormattedJSON (args?: any)
154898b0
C
23}
24
2186386c 25function 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 38async 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}
53namespace getServerActor {
54 export let serverActor: ActorModel
7a7724e6
C
55}
56
990b6a0b
C
57function 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
64function getSecureTorrentName (originalName: string) {
65 return sha256(originalName) + '.torrent'
66}
792dbaf0 67
9f10b292 68// ---------------------------------------------------------------------------
c45f7f84 69
65fcc311 70export {
cf7a61b5 71 deleteFileAsync,
65fcc311 72 generateRandomString,
0aef76c4 73 getFormattedObjects,
990b6a0b 74 getSecureTorrentName,
50d6de9c 75 getServerActor,
ce33919c 76 generateVideoTmpPath
65fcc311 77}