-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
86 lines (72 loc) · 2.41 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
// Import packages
const express = require('express');
const multer = require('multer');
const { findImageLabels } = require('./findImageLabels');
const { isImage, isWordDoc, isTextFile } = require('./identifyFileTypes');
const path = require('path');
const textract = require('textract');
const fs = require('fs');
// // Initiate the express server
const app = express()
const port = 8080
// Configure the multer storage
const storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, 'files/');
},
filename: function (req, file, cb) {
cb(null, file.fieldname + '-' + Date.now() + path.extname(file.originalname));
}
});
const fileFilter = function (req, file, cb) {
if (!file.originalname.match(/\.(jpg|JPG|jpeg|JPEG|png|PNG|docx|DOCX|doc|DOCX|txt|TXT)$/)) {
const errorMessage = 'Only accept image files (.jpg / .jpeg / .png) / text files (.doc / .docx / .txt)';
req.fileValidationError = errorMessage;
return cb(new Error(errorMessage), false);
}
cb(null, true);
};
// Handle post request to the route of '/upload'
app.post('/upload', (req, res) => {
let upload = multer({ storage: storage, fileFilter: fileFilter }).single('uploadFile');
upload(req, res, async function (err) {
const errorMessageToClients = "Sorry. We only support image files and text files.";
if (req.fileValidationError) {
res.status(400).json({ labels: [errorMessageToClients]});
return;
}
else if (!req.file) {
res.status(400).json({ labels: [errorMessageToClients]});
return;
}
else if (err instanceof multer.MulterError) {
res.status(400).json({ labels: [errorMessageToClients]});
return;
}
else if (err) {
res.status(400).json({ labels: [errorMessageToClients]});
return;
}
const filename = req.file.path
if (isImage(filename)) {
const labels = await findImageLabels(req.file.path);
res.status(200).json({ labels });
return
}
if (isWordDoc(filename)) {
textract.fromFileWithPath(filename, function (error, text) {
res.status(200).json({ labels: [text] });
})
return
}
if (isTextFile(filename)) {
const text = fs.readFileSync('test.txt', 'utf8');
res.status(200).json({ labels: [text] });
return
}
});
});
app.use(express.static(path.join(__dirname, 'public')));
app.listen(port, () => {
console.log(`Listening at PORT ${port}`);
})