]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/helpers/utils.ts
Merge branch 'feature/correctly-send-activities' into develop
[github/Chocobozzz/PeerTube.git] / server / helpers / utils.ts
1 import { ResultList } from '../../shared'
2 import { CONFIG } from '../initializers'
3 import { ApplicationModel } from '../models/application/application'
4 import { execPromise, execPromise2, pseudoRandomBytesPromise, sha256 } from './core-utils'
5 import { logger } from './logger'
6 import { join } from 'path'
7 import { Instance as ParseTorrent } from 'parse-torrent'
8 import { remove } from 'fs-extra'
9 import * as memoizee from 'memoizee'
10
11 function deleteFileAsync (path: string) {
12 remove(path)
13 .catch(err => logger.error('Cannot delete the file %s asynchronously.', path, { err }))
14 }
15
16 async function generateRandomString (size: number) {
17 const raw = await pseudoRandomBytesPromise(size)
18
19 return raw.toString('hex')
20 }
21
22 interface FormattableToJSON { toFormattedJSON (args?: any) }
23 function getFormattedObjects<U, T extends FormattableToJSON> (objects: T[], objectsTotal: number, formattedArg?: any) {
24 const formattedObjects: U[] = []
25
26 objects.forEach(object => {
27 formattedObjects.push(object.toFormattedJSON(formattedArg))
28 })
29
30 return {
31 total: objectsTotal,
32 data: formattedObjects
33 } as ResultList<U>
34 }
35
36 const getServerActor = memoizee(async function () {
37 const application = await ApplicationModel.load()
38 if (!application) throw Error('Could not load Application from database.')
39
40 const actor = application.Account.Actor
41 actor.Account = application.Account
42
43 return actor
44 })
45
46 function generateVideoImportTmpPath (target: string | ParseTorrent) {
47 const id = typeof target === 'string' ? target : target.infoHash
48
49 const hash = sha256(id)
50 return join(CONFIG.STORAGE.TMP_DIR, hash + '-import.mp4')
51 }
52
53 function getSecureTorrentName (originalName: string) {
54 return sha256(originalName) + '.torrent'
55 }
56
57 async function getServerCommit () {
58 try {
59 const tag = await execPromise2(
60 '[ ! -d .git ] || git name-rev --name-only --tags --no-undefined HEAD 2>/dev/null || true',
61 { stdio: [ 0, 1, 2 ] }
62 )
63
64 if (tag) return tag.replace(/^v/, '')
65 } catch (err) {
66 logger.debug('Cannot get version from git tags.', { err })
67 }
68
69 try {
70 const version = await execPromise('[ ! -d .git ] || git rev-parse --short HEAD')
71
72 if (version) return version.toString().trim()
73 } catch (err) {
74 logger.debug('Cannot get version from git HEAD.', { err })
75 }
76
77 return ''
78 }
79
80 /**
81 * From a filename like "ede4cba5-742b-46fa-a388-9a6eb3a3aeb3.mp4", returns
82 * only the "ede4cba5-742b-46fa-a388-9a6eb3a3aeb3" part. If the filename does
83 * not contain a UUID, returns null.
84 */
85 function getUUIDFromFilename (filename: string) {
86 const regex = /[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/
87 const result = filename.match(regex)
88
89 if (!result || Array.isArray(result) === false) return null
90
91 return result[0]
92 }
93
94 // ---------------------------------------------------------------------------
95
96 export {
97 deleteFileAsync,
98 generateRandomString,
99 getFormattedObjects,
100 getSecureTorrentName,
101 getServerActor,
102 getServerCommit,
103 generateVideoImportTmpPath,
104 getUUIDFromFilename
105 }