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