-
Notifications
You must be signed in to change notification settings - Fork 8
/
index.js
128 lines (112 loc) · 3.68 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
require('dotenv').config();
const express = require('express');
const fileUpload = require('express-fileupload');
const bodyParser = require('body-parser');
const FormData = require('form-data');
const axios = require('axios');
const app = express();
const port = 3000;
const path = require('path');
const UNSCREEN_API_VIDEOS_URL = "https://api.unscreen.com/v1.0/videos";
app.use(express.static('public'));
app.use(bodyParser.json());
app.use(fileUpload());
// the endpoint for our file upload form
app.post('/upload', (req, res) => {
var formData = new FormData();
if (req.files && req.files.video) {
const buffer = Buffer.from(req.files.video.data);
const filename = "original" + path.extname(req.files.video.name);
formData.append("video_file", buffer, filename);
}
if (req.url) {
formData.append("video_url", req.body.url);
}
if (process.env.WEBHOOK_HOST) {
console.log("WEBHOOK_HOST set, using webhooks");
formData.append("webhook_url", process.env.WEBHOOK_HOST + '/webhook');
} else {
console.log("WEBHOOK_HOST not set, using good old polling");
}
// append the selected format
formData.append("format", req.body.format);
if (req.body.format == "mp4") {
// need to add background_color because mp4 doesn't support transparency
formData.append("background_color", "000000");
}
var headers = formData.getHeaders();
headers['X-Api-Key'] = process.env.API_KEY;
axios({
method: 'post',
url: UNSCREEN_API_VIDEOS_URL,
data: formData,
headers: headers,
'maxContentLength': Infinity,
'maxBodyLength': Infinity,
})
.then(function (response) {
// handle success
console.log(response.data);
if (process.env.WEBHOOK_HOST=="") {
// no webhook host specified so let's start polling
poll(response.data.data.links.self, res);
}
})
.catch(function (error) {
// handle error
console.log(error);
res.json(error);
});
})
// repeatedly fetch video information
function poll(url, res) {
console.log('poll');
axios({
method: 'get',
url: url,
headers: { 'X-Api-Key': process.env.API_KEY },
})
.then(function (response) {
// handle success
console.log(response.data);
if (response.data.data.attributes.status != 'done') {
// poll again
setTimeout(function () { poll(url, res); }, 3000);
} else {
// video processing is finished, let's redirect to the result url
res.redirect(response.data.data.attributes.result_url);
}
})
.catch(function (error) {
// handle error
console.log(error);
res.send(error);
});
}
// fetches a list of all of your videos from the unscreen API
app.get('/videos', (req, res) => {
axios({
method: 'get',
url: UNSCREEN_API_VIDEOS_URL,
headers: { 'X-Api-Key': process.env.API_KEY },
})
.then(function (response) {
// handle success
console.log(response.data);
res.json(response.data);
})
.catch(function (error) {
// handle error
console.log(error);
});
});
app.post('/webhook', (req, res) => {
console.log('webhook');
console.log(req.body);
if (req.body.data.attributes.status == 'done') {
console.log(req.body.data.attributes.result_url);
}
})
app.listen(port, () => {
console.log(`Example app listening at http://localhost:${port}`)
})