-
Notifications
You must be signed in to change notification settings - Fork 4
/
app.js
58 lines (40 loc) · 1.5 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
import express from 'express';
import * as bodyParser from 'body-parser';
import * as path from 'path';
import multer from 'multer';
import { Worker,isMainThread,workerData } from 'worker_threads';
require('dotenv').config();
const storage = multer.diskStorage({
destination: './uploads/',
filename: function(req, file, cb){
cb(null,file.fieldname + '-' + Date.now() + path.extname(file.originalname));
}
});
const upload = multer({ dest : 'uploads',storage :storage });
const app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended : false }))
app.set("views",path.join(__dirname,"views"));
app.set("view engine","hbs");
app.get("/",(req,res) => {
res.render("index");
})
app.post("/upload-video",upload.single("ssvideo"),(req,res) => {
if(isMainThread){
let thread = new Worker('./threads/threaderone.js',{ workerData : { file : req.file.path,filename : req.file.filename,watermark_image_url : image_url } })
thread.on('message',(data) => {
res.download(data.file,req.file.filename);
})
thread.on("error",(err) => {
console.error("thread",err);
})
thread.on('exit',(code) => {
if(code != 0)
console.error(`Worker stopped with exit code ${code}`)
})
}
})
const PORT = process.env.PORT;
app.listen(PORT,() => {
console.log(`Server is running on PORT ${PORT}`);
})