forked from sgbj/versed
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
64 lines (53 loc) · 1.88 KB
/
index.js
File metadata and controls
64 lines (53 loc) · 1.88 KB
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
'use strict';
const fs = require('fs');
const path = require('path');
const mime = require('mime');
const express = require('express');
const multer = require('multer')
const Middleware = require('./middleware');
// Create processing pipeline
let middleware = new Middleware();
fs.readdirSync(path.join(__dirname, 'middleware')).forEach(function(file) {
middleware.use(require(path.join(__dirname, 'middleware', file)));
});
// Create express app
const app = express();
app.use(express.static('public'));
app.use(express.urlencoded());
app.use(express.json());
const storage = multer.memoryStorage();
const upload = multer({ storage: storage });
app.post('/convert', upload.single('file'), function (req, res, next) {
const mimetype = mime.getType(req.file.originalname);
const type = mimetype.split('/')[0];
// Run file through the pipeline
middleware.run({
input: {
...req.body,
filename: req.file.originalname,
mimetype: mimetype,
type: type,
buffer: req.file.buffer
}
}, (context) => {
if (context.error) {
console.error(context.error);
}
// Send the result or error
if (context.output) {
res.writeHead(200, {
'Content-Type': mime.getType(context.input.format),
'Content-disposition': 'attachment;filename='
+ path.basename(context.input.filename, path.extname(context.input.filename))
+ '.' + context.output.format || req.body.format,
'Content-Length': context.output.buffer.length
});
res.end(context.output.buffer);
} else {
res.status(500).end();
}
});
});
app.listen(3000, function () {
console.log('Listening on port 3000');
});