Skip to content

Commit 41b24d6

Browse files
committed
final all working code with upload n fetch
1 parent 80cf6b6 commit 41b24d6

189 files changed

Lines changed: 44344 additions & 0 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

app.js

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
2+
const express = require('express');
3+
const multer = require('multer');
4+
const ejs = require('ejs');
5+
const path = require('path');
6+
7+
// Set The Storage Engine
8+
const storage = multer.diskStorage({
9+
destination: './public/uploads/',
10+
filename: function(req, file, cb){
11+
global.fname = file.fieldname + '-' + Date.now() + path.extname(file.originalname)
12+
cb(null,file.fieldname + '-' + Date.now() + path.extname(file.originalname));
13+
}
14+
});
15+
16+
// Init Upload
17+
const upload = multer({
18+
storage: storage,
19+
limits:{fileSize: 1000000},
20+
fileFilter: function(req, file, cb){
21+
checkFileType(file, cb);
22+
}
23+
}).single('myFile');
24+
25+
// Check File Type
26+
function checkFileType(file, cb){
27+
// Allowed ext
28+
const filetypes = /pdf/;
29+
// Check ext
30+
const extname = filetypes.test(path.extname(file.originalname).toLowerCase());
31+
// Check mime
32+
const mimetype = filetypes.test(file.mimetype);
33+
34+
if(mimetype && extname){
35+
return cb(null,true);
36+
} else {
37+
cb('Error: pdf Only!');
38+
}
39+
}
40+
41+
// Init app
42+
const app = express();
43+
44+
// EJS
45+
app.set('view engine', 'ejs');
46+
//var name = "chintan"
47+
// Public Folder
48+
app.use(express.static('./public'));
49+
50+
app.get('/', (req, res) => res.render('index'));
51+
52+
app.post('/upload', (req, res) => {
53+
upload(req, res, (err) => {
54+
if(err){
55+
res.render('index', {
56+
msg: err
57+
});
58+
}
59+
else {
60+
if(req.file == undefined){
61+
res.render('index', {
62+
msg: 'Error: No File Selected!'
63+
});
64+
}
65+
else {
66+
67+
let {PythonShell} = require('python-shell')
68+
var options = {
69+
mode: 'text',
70+
args: [fname]
71+
};
72+
PythonShell.run('resgen.py', options, function (err,result) {
73+
if (err) throw err;
74+
75+
76+
res.render('index', {
77+
msg: 'File Uploaded!',
78+
79+
str: result
80+
});
81+
});
82+
}
83+
}
84+
});
85+
86+
87+
88+
89+
90+
91+
92+
93+
});
94+
95+
96+
97+
98+
const port = 3000;
99+
100+
app.listen(port, () => console.log(`Server started on port ${port}`));

0 commit comments

Comments
 (0)