forked from illuspas/Node-Media-Server
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patharchive.js
104 lines (93 loc) · 2.86 KB
/
archive.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
const config = require('./misc/config');
const aws = require('aws-sdk');
aws.config.accessKeyId = config.s3.accessKey;
aws.config.secretAccessKey = config.s3.secret;
aws.config.logger = console;
// Fix for Linode object storage error
aws.util.update(aws.S3.prototype, {
addExpect100Continue: function addExpect100Continue(req) {
console.log('Depreciating this workaround, because introduced a bug');
console.log('Check: https://github.com/andrewrk/node-s3-client/issues/74');
}
});
const s3 = new aws.S3({
endpoint: config.s3.endpoint
});
const fs = require('fs');
const axios = require('axios');
const Promise = require('bluebird').Promise;
const random = process.argv[2];
const streamName = process.argv[3];
const key = process.argv[4];
const duration = process.argv[5];
const ouPath = process.argv[6];
const upload = async data => {
try {
await s3.upload(data).promise();
} catch (e) {
console.error(e);
console.error('Retry: ' + data.Key);
await upload(data);
}
};
const uploadThumb = async () => {
try {
const thumb = await axios.get(`http://${process.env['NMS_SERVER'] || 'lon.stream.guac.live'}/live/${streamName}/thumbnail.jpg?v=${Math.floor((new Date().getTime() - 15000) / 60000)}`,
{responseType: 'arraybuffer'});
await upload({
Bucket: config.s3.bucket,
Key: key + 'thumbnail.jpg',
Body: thumb.data,
ACL: 'public-read'
});
} catch (e) {
console.error(e);
}
};
const uploadVideos = async retry => {
const promises = [];
for (const filename of fs.readdirSync(ouPath)) {
if (filename.endsWith('.ts')
|| filename.endsWith('.m3u8')
|| filename.endsWith('.mpd')
|| filename.endsWith('.m4s')
|| filename.endsWith('.tmp')) {
const path = ouPath + '/' + filename;
console.log(path);
promises.push({
Bucket: config.s3.bucket,
Key: key + filename,
Body: fs.createReadStream(path),
ACL: 'public-read'
});
}
}
try {
await Promise.map(promises, data => s3.upload(data).promise().then(() => fs.unlinkSync(data.Body.path)), {concurrency: config.s3.concurrency});
} catch (e) {
console.error(e);
await new Promise(resolve => setTimeout(resolve, 5000));
await uploadVideos(true);
}
if (retry) return;
setTimeout(() => fs.rmdirSync(ouPath), 10000);
axios.post(
`${config.endpoint}/archive`,
{
streamName,
duration,
random,
thumbnail: encodeURIComponent(`https://${config.s3.publishUrl}/${key}thumbnail.jpg`),
stream: encodeURIComponent(`https://${config.s3.publishUrl}/${key}indexarchive.m3u8`)
},
{
headers: {
'Authorization': `Bearer ${config.api_secret}`
}
}
);
};
(async () => {
await uploadThumb();
await uploadVideos(false);
})();