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