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