]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/helpers/utils.ts
remove isAdministration
[github/Chocobozzz/PeerTube.git] / server / helpers / utils.ts
CommitLineData
6fcd19ba 1import { ResultList } from '../../shared'
0626e7af 2import { CONFIG } from '../initializers'
50d6de9c 3import { ApplicationModel } from '../models/application/application'
499d9015 4import { execPromise, execPromise2, pseudoRandomBytesPromise, sha256 } from './core-utils'
efc32059 5import { logger } from './logger'
3e17515e 6import { join } from 'path'
990b6a0b 7import { Instance as ParseTorrent } from 'parse-torrent'
62689b94 8import { remove } from 'fs-extra'
7348b1fd 9import * as memoizee from 'memoizee'
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
a4101923 22interface FormattableToJSON { toFormattedJSON (args?: any) }
2186386c 23function getFormattedObjects<U, T extends FormattableToJSON> (objects: T[], objectsTotal: number, formattedArg?: any) {
0aef76c4 24 const formattedObjects: U[] = []
55fa55a9 25
075f16ca 26 objects.forEach(object => {
2186386c 27 formattedObjects.push(object.toFormattedJSON(formattedArg))
55fa55a9
C
28 })
29
2186386c 30 return {
55fa55a9 31 total: objectsTotal,
0aef76c4 32 data: formattedObjects
2186386c 33 } as ResultList<U>
55fa55a9
C
34}
35
7348b1fd
C
36const getServerActor = memoizee(async function () {
37 const application = await ApplicationModel.load()
38 if (!application) throw Error('Could not load Application from database.')
2cebd797 39
7ad9b984
C
40 const actor = application.Account.Actor
41 actor.Account = application.Account
42
43 return actor
7348b1fd 44})
7a7724e6 45
6040f87d 46function generateVideoImportTmpPath (target: string | ParseTorrent) {
990b6a0b
C
47 const id = typeof target === 'string' ? target : target.infoHash
48
49 const hash = sha256(id)
6040f87d 50 return join(CONFIG.STORAGE.TMP_DIR, hash + '-import.mp4')
ce33919c
C
51}
52
990b6a0b
C
53function getSecureTorrentName (originalName: string) {
54 return sha256(originalName) + '.torrent'
55}
792dbaf0 56
1b5e2d72 57async function getServerCommit () {
499d9015
C
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 }
abb2c792 76
1b5e2d72 77 return ''
abb2c792
RK
78}
79
edb4ffc7
FA
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 */
85function 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
9f10b292 94// ---------------------------------------------------------------------------
c45f7f84 95
65fcc311 96export {
cf7a61b5 97 deleteFileAsync,
65fcc311 98 generateRandomString,
0aef76c4 99 getFormattedObjects,
990b6a0b 100 getSecureTorrentName,
50d6de9c 101 getServerActor,
1b5e2d72 102 getServerCommit,
6040f87d 103 generateVideoImportTmpPath,
edb4ffc7 104 getUUIDFromFilename
65fcc311 105}