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