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