]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - shared/extra-utils/miscs/miscs.ts
42250886c2b2fc04f5956003e1dce27a16e5d587
[github/Chocobozzz/PeerTube.git] / shared / extra-utils / miscs / miscs.ts
1 /* tslint:disable:no-unused-expression */
2
3 import * as chai from 'chai'
4 import { basename, isAbsolute, join, resolve } from 'path'
5 import * as request from 'supertest'
6 import * as WebTorrent from 'webtorrent'
7 import { pathExists, readFile } from 'fs-extra'
8 import * as ffmpeg from 'fluent-ffmpeg'
9
10 const expect = chai.expect
11 let webtorrent: WebTorrent.Instance
12
13 function immutableAssign <T, U> (target: T, source: U) {
14 return Object.assign<{}, T, U>({}, target, source)
15 }
16
17 // Default interval -> 5 minutes
18 function dateIsValid (dateString: string, interval = 300000) {
19 const dateToCheck = new Date(dateString)
20 const now = new Date()
21
22 return Math.abs(now.getTime() - dateToCheck.getTime()) <= interval
23 }
24
25 function wait (milliseconds: number) {
26 return new Promise(resolve => setTimeout(resolve, milliseconds))
27 }
28
29 function webtorrentAdd (torrent: string, refreshWebTorrent = false) {
30 const WebTorrent = require('webtorrent')
31
32 if (!webtorrent) webtorrent = new WebTorrent()
33 if (refreshWebTorrent === true) webtorrent = new WebTorrent()
34
35 return new Promise<WebTorrent.Torrent>(res => webtorrent.add(torrent, res))
36 }
37
38 function root () {
39 // We are in /miscs
40 let root = join(__dirname, '..', '..', '..')
41
42 if (basename(root) === 'dist') root = resolve(root, '..')
43
44 return root
45 }
46
47 async function testImage (url: string, imageName: string, imagePath: string, extension = '.jpg') {
48 const res = await request(url)
49 .get(imagePath)
50 .expect(200)
51
52 const body = res.body
53
54 const data = await readFile(join(root(), 'server', 'tests', 'fixtures', imageName + extension))
55 const minLength = body.length - ((20 * body.length) / 100)
56 const maxLength = body.length + ((20 * body.length) / 100)
57
58 expect(data.length).to.be.above(minLength)
59 expect(data.length).to.be.below(maxLength)
60 }
61
62 function buildAbsoluteFixturePath (path: string, customTravisPath = false) {
63 if (isAbsolute(path)) {
64 return path
65 }
66
67 if (customTravisPath && process.env.TRAVIS) return join(process.env.HOME, 'fixtures', path)
68
69 return join(root(), 'server', 'tests', 'fixtures', path)
70 }
71
72 async function generateHighBitrateVideo () {
73 const tempFixturePath = buildAbsoluteFixturePath('video_high_bitrate_1080p.mp4', true)
74
75 const exists = await pathExists(tempFixturePath)
76 if (!exists) {
77
78 // Generate a random, high bitrate video on the fly, so we don't have to include
79 // a large file in the repo. The video needs to have a certain minimum length so
80 // that FFmpeg properly applies bitrate limits.
81 // https://stackoverflow.com/a/15795112
82 return new Promise<string>(async (res, rej) => {
83 ffmpeg()
84 .outputOptions([ '-f rawvideo', '-video_size 1920x1080', '-i /dev/urandom' ])
85 .outputOptions([ '-ac 2', '-f s16le', '-i /dev/urandom', '-t 10' ])
86 .outputOptions([ '-maxrate 10M', '-bufsize 10M' ])
87 .output(tempFixturePath)
88 .on('error', rej)
89 .on('end', () => res(tempFixturePath))
90 .run()
91 })
92 }
93
94 return tempFixturePath
95 }
96
97 // ---------------------------------------------------------------------------
98
99 export {
100 dateIsValid,
101 wait,
102 webtorrentAdd,
103 immutableAssign,
104 testImage,
105 buildAbsoluteFixturePath,
106 root,
107 generateHighBitrateVideo
108 }