-
Notifications
You must be signed in to change notification settings - Fork 53
/
app.js
168 lines (158 loc) · 6.09 KB
/
app.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
const fs = require('fs');
const express = require('express');
const app = express();
const Busboy = require('busboy');
const compression = require('compression');
const ffmpeg = require('fluent-ffmpeg');
const uniqueFilename = require('unique-filename');
const consts = require(__dirname + '/app/constants.js');
const endpoints = require(__dirname + '/app/endpoints.js');
const winston = require('winston');
app.use(compression());
winston.remove(winston.transports.Console);
winston.add(winston.transports.Console, {'timestamp': true});
for (let prop in endpoints.types) {
if (endpoints.types.hasOwnProperty(prop)) {
let ffmpegParams = endpoints.types[prop];
let bytes = 0;
app.post('/' + prop, function(req, res) {
let hitLimit = false;
let fileName = '';
let savedFile = uniqueFilename(__dirname + '/uploads/');
let busboy = new Busboy({
headers: req.headers,
limits: {
files: 1,
fileSize: consts.fileSizeLimit,
}});
busboy.on('filesLimit', function() {
winston.error(JSON.stringify({
type: 'filesLimit',
message: 'Upload file size limit hit',
}));
});
busboy.on('file', function(
fieldname,
file,
filename,
encoding,
mimetype
) {
file.on('limit', function(file) {
hitLimit = true;
let err = {file: filename, error: 'exceeds max size limit'};
err = JSON.stringify(err);
winston.error(err);
res.writeHead(500, {'Connection': 'close'});
res.end(err);
});
let log = {
file: filename,
encoding: encoding,
mimetype: mimetype,
};
winston.info(JSON.stringify(log));
file.on('data', function(data) {
bytes += data.length;
});
file.on('end', function(data) {
log.bytes = bytes;
winston.info(JSON.stringify(log));
});
fileName = filename;
winston.info(JSON.stringify({
action: 'Uploading',
name: fileName,
}));
let written = file.pipe(fs.createWriteStream(savedFile));
if (written) {
winston.info(JSON.stringify({
action: 'saved',
path: savedFile,
}));
}
});
busboy.on('finish', function() {
if (hitLimit) {
fs.unlinkSync(savedFile);
return;
}
winston.info(JSON.stringify({
action: 'upload complete',
name: fileName,
}));
let outputFile = savedFile + '.' + ffmpegParams.extension;
winston.info(JSON.stringify({
action: 'begin conversion',
from: savedFile,
to: outputFile,
}));
let ffmpegConvertCommand = ffmpeg(savedFile);
ffmpegConvertCommand
.renice(15)
.outputOptions(ffmpegParams.outputOptions)
.on('error', function(err) {
let log = JSON.stringify({
type: 'ffmpeg',
message: err,
});
winston.error(log);
fs.unlinkSync(savedFile);
res.writeHead(500, {'Connection': 'close'});
res.end(log);
})
.on('end', function() {
fs.unlinkSync(savedFile);
winston.info(JSON.stringify({
action: 'starting download to client',
file: savedFile,
}));
res.download(outputFile, null, function(err) {
if (err) {
winston.error(JSON.stringify({
type: 'download',
message: err,
}));
}
winston.info(JSON.stringify({
action: 'deleting',
file: outputFile,
}));
if (fs.unlinkSync(outputFile)) {
winston.info(JSON.stringify({
action: 'deleted',
file: outputFile,
}));
}
});
})
.save(outputFile);
});
return req.pipe(busboy);
});
}
}
require('express-readme')(app, {
filename: 'README.md',
routes: ['/', '/readme'],
});
const server = app.listen(consts.port, function() {
let host = server.address().address;
let port = server.address().port;
winston.info(JSON.stringify({
action: 'listening',
url: 'http://'+host+':'+port,
}));
});
server.on('connection', function(socket) {
winston.info(JSON.stringify({
action: 'new connection',
timeout: consts.timeout,
}));
socket.setTimeout(consts.timeout);
socket.server.timeout = consts.timeout;
server.keepAliveTimeout = consts.timeout;
});
app.use(function(req, res, next) {
res.status(404).send(JSON.stringify({error: 'route not available'})+'\n');
});