1 |
43614
|
argiro.kok
|
var express = require("express");
|
2 |
|
|
var bodyParser = require("body-parser");
|
3 |
|
|
var multer = require("multer");
|
4 |
|
|
var app = express();
|
5 |
55705
|
k.triantaf
|
var cors = require('cors');
|
6 |
|
|
var http = require("http");
|
7 |
|
|
|
8 |
|
|
var auth = 'http://scoobydoo.di.uoa.gr:8080/dnet-openaire-users-1.0.0-SNAPSHOT/api/users/getUserInfo?accessToken=';
|
9 |
|
|
|
10 |
|
|
var storage = multer.diskStorage({
|
11 |
|
|
destination: function (req, file, cb) {
|
12 |
|
|
cb(null, 'uploads')
|
13 |
|
|
},
|
14 |
|
|
filename: function (req, file, cb) {
|
15 |
|
|
if(req.params.id) {
|
16 |
|
|
cb(null, req.params.id + '-' + new Date().getTime() + '.' + file.originalname.split('.').pop());
|
17 |
|
|
} else {
|
18 |
|
|
cb(null, file.originalname);
|
19 |
|
|
}
|
20 |
|
|
}
|
21 |
|
|
});
|
22 |
|
|
|
23 |
|
|
var upload = multer({ storage: storage })
|
24 |
52744
|
argiro.kok
|
app.use(cors());
|
25 |
43614
|
argiro.kok
|
|
26 |
|
|
app.use(bodyParser.json());
|
27 |
55705
|
k.triantaf
|
app.use(bodyParser.urlencoded({extended: true}));
|
28 |
43614
|
argiro.kok
|
|
29 |
55705
|
k.triantaf
|
app.use(function (req, res, next) {
|
30 |
43614
|
argiro.kok
|
res.header("Access-Control-Allow-Origin", "*");
|
31 |
|
|
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
|
32 |
|
|
next();
|
33 |
|
|
});
|
34 |
|
|
|
35 |
55705
|
k.triantaf
|
app.post("/upload", upload.array("uploads[]", 12), function (req, res) {
|
36 |
|
|
var filepath = "./" + req.files[0].path;
|
37 |
|
|
if (req.files[0].mimetype != 'text/csv') {
|
38 |
|
|
console.error("No proper file type");
|
39 |
|
|
res.status(500).send(getResponse(500, "No proper file type"));
|
40 |
|
|
} else {
|
41 |
|
|
//send file
|
42 |
|
|
res.download(filepath);
|
43 |
|
|
}
|
44 |
|
|
deleteFile(filepath);
|
45 |
43614
|
argiro.kok
|
|
46 |
55705
|
k.triantaf
|
});
|
47 |
43614
|
argiro.kok
|
|
48 |
55705
|
k.triantaf
|
app.post('/upload/:id', upload.single('photo'), (req, res) => {
|
49 |
|
|
const token = req.headers['x-xsrf-token'];
|
50 |
|
|
const file = req.file;
|
51 |
|
|
const filepath = "./" + file.path;
|
52 |
|
|
if(!token) {
|
53 |
|
|
res.status(401).send(getResponse(401, "Unauthorized"));
|
54 |
|
|
deleteFile(filepath);
|
55 |
|
|
}
|
56 |
|
|
else if (!file || (file.mimetype != 'image/jpeg' && file.mimetype != 'image/png')) {
|
57 |
|
|
res.status(500).send(getResponse(500, "No image file type"));
|
58 |
|
|
deleteFile(filepath);
|
59 |
|
|
}
|
60 |
|
|
else {
|
61 |
|
|
http.get(auth+token, function (resp) {
|
62 |
|
|
var responseString = "";
|
63 |
|
|
resp.on("data", function (data) {
|
64 |
|
|
responseString += data;
|
65 |
|
|
});
|
66 |
|
|
resp.on("end", function () {
|
67 |
|
|
var result = JSON.parse(responseString);
|
68 |
|
|
console.log(result);
|
69 |
|
|
if(result.error) {
|
70 |
|
|
res.status(401).send(getResponse(401, "Unauthorized"));
|
71 |
|
|
deleteFile(filepath);
|
72 |
|
|
} else {
|
73 |
|
|
// if user id contains id param keep file and send information, else delete it.
|
74 |
|
|
if(result.sub.indexOf(req.params.id) !== -1) {
|
75 |
|
|
file.filename = file.filename;
|
76 |
|
|
res.send(file);
|
77 |
|
|
} else {
|
78 |
|
|
res.status(401).send(getResponse(401, "Unauthorized"));
|
79 |
|
|
deleteFile(filepath);
|
80 |
|
|
}
|
81 |
|
|
}
|
82 |
|
|
});
|
83 |
|
|
});
|
84 |
|
|
}
|
85 |
|
|
|
86 |
43614
|
argiro.kok
|
});
|
87 |
|
|
|
88 |
55705
|
k.triantaf
|
app.get('/download/:filename', function (req, res) {
|
89 |
|
|
res.download('./uploads/' + req.params.filename);
|
90 |
43614
|
argiro.kok
|
});
|
91 |
55705
|
k.triantaf
|
|
92 |
|
|
app.delete('/delete/:filename', function (req, res) {
|
93 |
|
|
const token = req.headers['x-xsrf-token'];
|
94 |
|
|
if(!token) {
|
95 |
|
|
res.status(401).send(getResponse(401, "Unauthorized"));
|
96 |
|
|
} else {
|
97 |
|
|
http.get(auth+token, function (resp) {
|
98 |
|
|
var responseString = "";
|
99 |
|
|
resp.on("data", function (data) {
|
100 |
|
|
responseString += data;
|
101 |
|
|
});
|
102 |
|
|
resp.on("end", function () {
|
103 |
|
|
var result = JSON.parse(responseString);
|
104 |
|
|
console.log(result);
|
105 |
|
|
if(result.error) {
|
106 |
|
|
res.status(401).send(getResponse(401, "Unauthorized"));
|
107 |
|
|
} else {
|
108 |
|
|
// if user id is on filename delete file else unauthorized.
|
109 |
|
|
if(result.sub.indexOf(req.params.filename.split('-')[0]) !== -1) {
|
110 |
|
|
deleteFile('./uploads/' + req.params.filename);
|
111 |
|
|
return res.status(200).send(getResponse(200, "File Deleted Successfully"));
|
112 |
|
|
} else {
|
113 |
|
|
res.status(401).send(getResponse(401, "Unauthorized"));
|
114 |
|
|
}
|
115 |
|
|
}
|
116 |
|
|
});
|
117 |
|
|
});
|
118 |
|
|
}
|
119 |
|
|
});
|
120 |
|
|
|
121 |
|
|
const server = app.listen(8000, function () {
|
122 |
43614
|
argiro.kok
|
console.log("Listening on port %s...", server.address().port);
|
123 |
|
|
});
|
124 |
52744
|
argiro.kok
|
|
125 |
55705
|
k.triantaf
|
function getResponse(code, message) {
|
126 |
|
|
var response = {};
|
127 |
|
|
response["code"] = code;
|
128 |
|
|
response["message"] = message;
|
129 |
|
|
return response;
|
130 |
52744
|
argiro.kok
|
}
|
131 |
55705
|
k.triantaf
|
|
132 |
|
|
function deleteFile(filepath) {
|
133 |
|
|
const fs = require('fs');
|
134 |
|
|
fs.stat(filepath, function (err, stats) {
|
135 |
|
|
console.log(stats); //here we got all information of file in stats variable
|
136 |
|
|
|
137 |
|
|
if (err) {
|
138 |
|
|
return console.error(err);
|
139 |
|
|
}
|
140 |
|
|
|
141 |
|
|
fs.unlink(filepath, function (err) {
|
142 |
|
|
if (err) return console.log(err);
|
143 |
|
|
console.log('file deleted successfully');
|
144 |
|
|
});
|
145 |
|
|
});
|
146 |
|
|
}
|