-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
61 lines (51 loc) · 1.66 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
const Readable = require('stream').Readable;
const express = require('express');
const NodeClam = require('clamscan');
require('dotenv').config();
const app = express();
const cors = require('cors');
const fileUpload = require('express-fileupload');
const config = require('./config')
const port = process.env.PORT && process.env.PORT !== '' ? parseInt(process.env.PORT) : 3500;
app.use(express.json());
app.use(cors())
// Middleware for attaching clamscan with the express request
app.use(async (req, _, next) => {
req.clamscan = await new NodeClam().init({ ...config.clamscanConfig })
next()
})
// Middleware for attaching files to req.files
app.use(fileUpload({ ...config.fileUploadConfig }))
const scanFile = async (file, clamscan) => {
const fileStream = Readable()
fileStream.push(file.data)
fileStream.push(null)
// const result = await clamscan.isInfected(__dirname + '/tmp/filename');
const result = await clamscan.scanStream(fileStream);
return {
filename: file.name,
is_infected: result.isInfected,
viruses: result.viruses,
}
}
app.post('/virus-scan', async (req, res) => {
if (!req.files || ! req.files.energuide) {
return res.status(409).json({
message: 'No energuide file provided for scan'
})
}
// await req.files.energuide.mv('./tmp/' + req.files.energuide.name);
const scanResult = await scanFile(req.files.energuide, req.clamscan);
console.log(scanResult);
if (scanResult.is_infected === true || scanFile.is_infected === null) {
return res.status(502).json({
clean: false
})
}
return res.status(200).json({
clean: true
});
});
app.listen(port, () => {
console.log(`Virus scan listening on port ${port}`);
});