-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
66 lines (49 loc) · 1.64 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
'use strict';
const libre = require('libreoffice-convert');
libre.convertAsync = require('util').promisify(libre.convert);
const {Storage} = require('@google-cloud/storage');
const storage = new Storage({keyFilename: 'service-account.json'});
const crypto = require('crypto');
const mmm = require('mmmagic');
const Magic = mmm.Magic;
const magic = new Magic(mmm.MAGIC_MIME_TYPE);
const express = require('express');
const app = express();
const port = process.env.PORT || 3000;
const fileUpload = require('express-fileupload');
app.use(fileUpload());
app.use(express.static('public'));
async function detectType(buffer) {
return new Promise((resolve, reject) => {
magic.detect(buffer, (err, result) => {
if (err) {
reject(err);
} else {
resolve(result);
}
});
});
}
app.post('/upload', async function(req, res) {
if (!req.files || Object.keys(req.files).length === 0) {
return res.status(400).send('No files were uploaded.');
}
const inputFile = req.files.document;
const inputBuffer = inputFile.data;
let outputBuffer;
const type = await detectType(inputBuffer);
if (type !== 'application/pdf') {
outputBuffer = await libre.convertAsync(inputBuffer, '.pdf', undefined);
} else {
outputBuffer = inputBuffer;
}
const filename = crypto.randomUUID() + ".pdf";
const remoteFile = storage.bucket('odr-eu-converter').file(filename);
await remoteFile.save(outputBuffer);
const fileUrl = await remoteFile.getSignedUrl({
action: 'read',
expires: Date.now() + 60 * 60 * 1000,
});
res.redirect("/viewer.html?file=" + encodeURIComponent(fileUrl.pop()));
});
app.listen(port)