-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathapp.js
85 lines (76 loc) · 2.43 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
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
const express = require('express');
var app = express();
var upload = require('express-fileupload');
var docxConverter = require('docx-pdf');
var path = require('path');
var fs = require('fs');
const extend_pdf = '.pdf'
const extend_docx = '.docx'
var down_name
app.use(upload());
app.get('/',function(req,res){
res.sendFile(__dirname+'/index.html');
})
app.post('/upload',function(req,res){
console.log(req.files);
if(req.files.upfile){
var file = req.files.upfile,
name = file.name,
type = file.mimetype;
//File where .docx will be downloaded
var uploadpath = __dirname + '/uploads/' + name;
//Name of the file --ex test,example
const First_name = name.split('.')[0];
//Name to download the file
down_name = First_name;
file.mv(uploadpath,function(err){
if(err){
console.log(err);
}else{
//Path of the downloaded or uploaded file
var initialPath = path.join(__dirname, `./uploads/${First_name}${extend_docx}`);
//Path where the converted pdf will be placed/uploaded
var upload_path = path.join(__dirname, `./uploads/${First_name}${extend_pdf}`);
//Converter to convert docx to pdf -->docx-pdf is used
//If you want you can use any other converter
//For example -- libreoffice-convert or --awesome-unoconv
docxConverter(initialPath,upload_path,function(err,result){
if(err){
console.log(err);
}
console.log('result'+result);
res.sendFile(__dirname+'/down_html.html')
});
}
});
}else{
res.send("No File selected !");
res.end();
}
});
app.get('/download', (req,res) =>{
//This will be used to download the converted file
res.download(__dirname +`/uploads/${down_name}${extend_pdf}`,`${down_name}${extend_pdf}`,(err) =>{
if(err){
res.send(err);
}else{
//Delete the files from directory after the use
console.log('Files deleted');
const delete_path_doc = process.cwd() + `/uploads/${down_name}${extend_docx}`;
const delete_path_pdf = process.cwd() + `/uploads/${down_name}${extend_pdf}`;
try {
fs.unlinkSync(delete_path_doc)
fs.unlinkSync(delete_path_pdf)
//file removed
} catch(err) {
console.error(err)
}
}
})
})
app.get('/thankyou',(req,res) => {
res.sendFile(__dirname+'/thankyou.html')
})
app.listen(3000,() => {
console.log("Server Started at port 3000...");
})