]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/helpers/utils.js
Server: fix update remote video
[github/Chocobozzz/PeerTube.git] / server / helpers / utils.js
CommitLineData
9f10b292 1'use strict'
8c308c2b 2
cbe2f7c3
C
3const crypto = require('crypto')
4
f0f5567b 5const logger = require('./logger')
8c308c2b 6
f0f5567b 7const utils = {
a6fd2b30 8 badRequest,
c4403b29 9 cleanForExit,
441b66f8 10 generateRandomString,
55fa55a9 11 isTestInstance,
4df023f2 12 getFormatedObjects
cbe2f7c3
C
13}
14
a6fd2b30
C
15function badRequest (req, res, next) {
16 res.type('json').status(400).end()
17}
18
cbe2f7c3
C
19function generateRandomString (size, callback) {
20 crypto.pseudoRandomBytes(size, function (err, raw) {
21 if (err) return callback(err)
22
23 callback(null, raw.toString('hex'))
24 })
9f10b292 25}
8c308c2b 26
bc503c2a 27function cleanForExit (webtorrentProcess) {
9f10b292 28 logger.info('Gracefully exiting.')
bc503c2a 29 process.kill(-webtorrentProcess.pid)
9f10b292 30}
0ae2e7f7 31
441b66f8
C
32function isTestInstance () {
33 return (process.env.NODE_ENV === 'test')
34}
35
55fa55a9
C
36function getFormatedObjects (objects, objectsTotal) {
37 const formatedObjects = []
38
39 objects.forEach(function (object) {
40 formatedObjects.push(object.toFormatedJSON())
41 })
42
43 return {
44 total: objectsTotal,
45 data: formatedObjects
46 }
47}
48
9f10b292 49// ---------------------------------------------------------------------------
c45f7f84 50
9f10b292 51module.exports = utils