-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
295 lines (269 loc) · 12.1 KB
/
index.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
// ssl cert stuff
var fs = require('fs');
var https = require('https');
var request = require('request');
var privateKey = fs.readFileSync('certs/server.key', 'utf8');
var certificate = fs.readFileSync('certs/server.crt', 'utf8');
var credentials = {
key: privateKey,
cert: certificate
};
// chunk with flow
process.env.TMPDIR = 'tmp';
var multipart = require('connect-multiparty');
var multipartMiddleware = multipart();
var flow = require('./flow-node.js')('tmp');
var express = require('express');
const app = express();
var fs = require('fs-extra');
var tus = require('tus-js-client');
var fetch = require('node-fetch')
// Configure access control allow origin header stuff
var ACCESS_CONTROLL_ALLOW_ORIGIN = false;
const crypto = require('crypto')
const bs58 = require('bs58')
// Host most stuff in the public folder
app.use(express.static(__dirname + '/public'));
app.use(express.static(__dirname + '/src'));
// debugging
var uploadCF = true;
// cf auth
var zone_url = "https://api.cloudflare.com/client/v4/zones/YOUR_ZONE_ID/media";
/* get your zone id with e.g. curl -X GET "https://api.cloudflare.com/client/v4/zones?name=example.com&status=active&page=1&per_page=20&order=status&direction=desc&match=all" \
-H "X-Auth-Email: [email protected]" \
-H "X-Auth-Key: c2547eb745079dac9320b638f5e225cf483cc5cfdda41" \
-H "Content-Type: application/json" */
var email = "YOUR_CF_EMAIL";
var auth_key = "YOUR_CF_AUTHKEY";
var cStatus = [];
function ipfsHash(filename, result) {
fs.createReadStream("uploads/" + filename).
pipe(crypto.createHash('sha1').setEncoding('hex')).
on('finish', async function () {
var tempHash = await this.read();
generateHash(tempHash, function (hash) {
result(hash);
})
})
}
function generateHash(sha1, hash) {
const hashFunction = Buffer.from('12', 'hex')
const digest = crypto.createHash('sha256').update(sha1).digest()
const digestSize = Buffer.from(digest.byteLength.toString(16), 'hex')
const combined = Buffer.concat([hashFunction, digestSize, digest])
const multihash = bs58.encode(combined)
hash(multihash.toString());
}
/*
clear all - delete all saved cf videos
request({
url: zone_url,
headers: {
"X-Auth-Email": email,
"X-Auth-Key": auth_key,
}
}, function (err, res, body) {
var temp = JSON.parse(body)["result"];
for (var i = 0; i < temp.length; i++) {
console.log(zone_url+temp[i]["uid"])
request.delete({
url: zone_url+"/"+temp[i]["uid"],
headers: {
"X-Auth-Email": email,
"X-Auth-Key": auth_key,
}
}, function (err, res, body) {
console.log(body)
})
}
})
*/
// check if video already exists
app.get('/check/:hash', function (req, response) {
generateHash(req.params.hash, function (hash) {
//console.log(hash);
request({
url: zone_url,
headers: {
"X-Auth-Email": email,
"X-Auth-Key": auth_key,
}
}, function (err, res, body) {
//console.log(body)
var picked = JSON.parse(body)["result"].find(o => o.meta.name === hash);
if (picked != undefined) {
response.json({
response: 200,
hash: hash
})
} else {
response.json({
response: 404
})
}
})
})
});
// Handle uploads through Flow.js
app.post('/upload', multipartMiddleware, function (req, res) {
console.log(req.headers);
// do some authentication
flow.post(req, function (status, filename, original_filename, identifier) {
if (ACCESS_CONTROLL_ALLOW_ORIGIN) {
res.header("Access-Control-Allow-Origin", "*");
}
if (status === 'done') {
var s = fs.createWriteStream('./uploads/' + filename);
s.on('finish', function () {
console.log("Upload Finished of " + identifier);
flow.clean(identifier);
ipfsHash(filename, function (tempName) {
request({
url: zone_url,
headers: {
"X-Auth-Email": email,
"X-Auth-Key": auth_key,
}
}, function (err, res, body) {
var picked = JSON.parse(body)["result"].find(o => o.meta.name === tempName);
if (picked == undefined) {
if (uploadCF) {
try {
var path = __dirname + '/uploads/' + filename;
var file = fs.createReadStream(path);
var size = fs.statSync(path).size;
var options = {
endpoint: zone_url,
headers: {
"X-Auth-Email": email,
"X-Auth-Key": auth_key,
},
resume: true,
metadata: {
label: [tempName],
name: tempName,
filename: filename
},
uploadSize: size,
onError: function (error) {
console.log(error);
fs.unlink(path, (err) => {
if (err) console.log(err);
console.log(path + ' was deleted');
});
},
onProgress: function (bytesUploaded, bytesTotal) {
var percentage = (bytesUploaded / bytesTotal * 100).toFixed(2);
cStatus[identifier] = {
videohash: tempName,
url: "https://watch.cloudflarestream.com" + (upload.url).split("/media")[1],
status: percentage,
ready: false,
processing: {},
};
//console.log(bytesUploaded, bytesTotal, percentage + "%");
},
onSuccess: function () {
//res.setHeader('Content-Type', 'application/json');
//res.send(JSON.parse('{ "response": 200, "url": "' + ("https://watch.cloudflarestream.com" + (upload.url).split("/media")[1]).toString() + '" }')); //where to go next
var ready = false;
var processing;
var updateInterval;
// clean every day from now on
var minutes = 0.1,
interval = minutes * 60 * 1000;
function updateStatus() {
try {
request({
url: upload.url,
headers: {
"X-Auth-Email": email,
"X-Auth-Key": auth_key,
}
}, function (err, res, body) {
//console.log(JSON.parse(body)["result"])
cStatus[identifier]["ready"] = JSON.parse(body)["result"]["readyToStream"];
cStatus[identifier]["processing"] = JSON.parse(body)["result"]["status"];
ready = JSON.parse(body)["result"]["readyToStream"];
try {
processing = JSON.parse(body)["result"]["status"];
} catch (e) {}
if (ready == true) {
clearInterval(updateInterval);
}
})
} catch (e) {}
}
updateStatus();
updateInterval = setInterval(updateStatus, interval);
fs.unlink(path, (err) => {
if (err) console.log("File not deleted");
console.log(path + ' was deleted');
});
}
}
var upload = new tus.Upload(file, options);
upload.start();
} catch (e) {
console.log(e)
}
};
} else {
cStatus[identifier] = {
videohash: tempName,
status: "100.00",
ready: true,
processing: {
state: "ready"
},
};
}
})
})
});
flow.write(identifier, s, {});
//res.status(/^(partly_done|done)$/.test(status) ? 200 : 500).send();
}
res.status(/^(partly_done|done)$/.test(status) ? 200 : 500).send();
});
});
app.options('/upload', function (req, res) {
console.log('OPTIONS');
if (ACCESS_CONTROLL_ALLOW_ORIGIN) {
res.header("Access-Control-Allow-Origin", "*");
}
res.status(200).send();
});
// Handle status checks on chunks through Flow.js
app.get('/upload', function (req, res) {
flow.get(req, function (status, filename, original_filename, identifier) {
console.log('GET', status);
if (ACCESS_CONTROLL_ALLOW_ORIGIN) {
res.header("Access-Control-Allow-Origin", "*");
}
if (status == 'found') {
status = 200;
} else {
status = 204;
}
res.status(status).send();
});
});
// Handle status checks on chunks through Flow.js
app.get('/status/:id', function (req, res) {
if (req.params.id != undefined) {
try {
res.json(cStatus[req.params.id]);
} catch (e) {
res.json({
"result": "error",
})
}
} else {
res.json({
"result": "error",
})
}
});
var httpsServer = https.createServer(credentials, app);
httpsServer.listen(80, () => console.log("Example app listening on port %d!", httpsServer.address().port));