[Utils Service | Trunk]: Add upload method for stakeholders
git-svn-id: https://svn.driver.research-infrastructures.eu/driver/dnet40/modules/uoa-services-portal/trunk@59488 d315682c-612b-4755-9ff5-7f18f6832af3
This commit is contained in:
parent
451430694d
commit
2af5bb33e4
|
@ -1,4 +1,4 @@
|
||||||
userInfoUrl = http://scoobydoo.di.uoa.gr:8080/dnet-openaire-users-1.0.0-SNAPSHOT/api/users/getUserInfo?accessToken=
|
userInfoUrl = http://dl170.madgik.di.uoa.gr:8180/dnet-openaire-users-1.0.0-SNAPSHOT/api/users/getUserInfo?accessToken=
|
||||||
originServer = .di.uoa.gr
|
originServer = .di.uoa.gr
|
||||||
post.allowed = /upload,/upload/
|
post.allowed = /upload,/upload/
|
||||||
ssl = false
|
ssl = false
|
||||||
|
|
|
@ -8,7 +8,7 @@ var TinyURL = require('tinyurl');
|
||||||
var app = express();
|
var app = express();
|
||||||
var http = null;
|
var http = null;
|
||||||
|
|
||||||
if(properties.get('ssl')) {
|
if (properties.get('ssl')) {
|
||||||
http = require("https");
|
http = require("https");
|
||||||
} else {
|
} else {
|
||||||
http = require("http");
|
http = require("http");
|
||||||
|
@ -20,14 +20,14 @@ var auth = properties.get('userInfoUrl');
|
||||||
var originServer = properties.get('originServer');
|
var originServer = properties.get('originServer');
|
||||||
var allowPostRequests = properties.get('post.allowed').split(',');
|
var allowPostRequests = properties.get('post.allowed').split(',');
|
||||||
var localPath = properties.get('localPath');
|
var localPath = properties.get('localPath');
|
||||||
var maxsize = properties.get('photo.size')*1024;
|
var maxsize = properties.get('photo.size') * 1024;
|
||||||
|
|
||||||
var storage = multer.diskStorage({
|
var storage = multer.diskStorage({
|
||||||
destination: function (req, file, cb) {
|
destination: function (req, file, cb) {
|
||||||
cb(null, 'uploads')
|
cb(null, 'uploads')
|
||||||
},
|
},
|
||||||
filename: function (req, file, cb) {
|
filename: function (req, file, cb) {
|
||||||
if(req.params.id) {
|
if (req.params.id) {
|
||||||
cb(null, req.params.id + '-' + new Date().getTime() + '.' + file.originalname.split('.').pop());
|
cb(null, req.params.id + '-' + new Date().getTime() + '.' + file.originalname.split('.').pop());
|
||||||
} else {
|
} else {
|
||||||
cb(null, file.originalname);
|
cb(null, file.originalname);
|
||||||
|
@ -35,7 +35,7 @@ var storage = multer.diskStorage({
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
var upload = multer({ storage: storage })
|
var upload = multer({storage: storage})
|
||||||
|
|
||||||
app.use(bodyParser.json());
|
app.use(bodyParser.json());
|
||||||
app.use(bodyParser.urlencoded({extended: true}));
|
app.use(bodyParser.urlencoded({extended: true}));
|
||||||
|
@ -69,7 +69,7 @@ app.use(function (req, res, next) {
|
||||||
});
|
});
|
||||||
|
|
||||||
app.post("/upload", upload.array("uploads[]", 12), function (req, res) {
|
app.post("/upload", upload.array("uploads[]", 12), function (req, res) {
|
||||||
var filepath = (localPath?".":__dirname)+"/" + req.files[0].path;
|
var filepath = (localPath ? "." : __dirname) + "/" + req.files[0].path;
|
||||||
let type = req.query.type;
|
let type = req.query.type;
|
||||||
console.log(type);
|
console.log(type);
|
||||||
if(type == 'json' && req.files[0].mimetype !== 'application/json'){
|
if(type == 'json' && req.files[0].mimetype !== 'application/json'){
|
||||||
|
@ -80,66 +80,35 @@ app.post("/upload", upload.array("uploads[]", 12), function (req, res) {
|
||||||
res.status(500).send(getResponse(500, "No proper file type"));
|
res.status(500).send(getResponse(500, "No proper file type"));
|
||||||
} else {
|
} else {
|
||||||
res.download(filepath);
|
res.download(filepath);
|
||||||
setTimeout(function() {
|
setTimeout(function () {
|
||||||
deleteFile(filepath);
|
deleteFile(filepath);
|
||||||
}, 3000);
|
}, 3000);
|
||||||
// deleteFile(filepath);
|
// deleteFile(filepath);
|
||||||
}
|
}
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
app.post('/upload/:id', upload.single('photo'), (req, res) => {
|
app.post('/upload/stakeholder/:id', upload.single('photo'), (req, res) => {
|
||||||
const token = req.headers['x-xsrf-token'];
|
sendFile(req, res, (result) => {
|
||||||
const file = req.file;
|
return isAdminOrCurator(result.edu_person_entitlements);
|
||||||
var filepath = (localPath?".":__dirname)+"/" + file.path;
|
});
|
||||||
console.log(filepath);
|
});
|
||||||
if(!token) {
|
|
||||||
res.status(401).send(getResponse(401, "Unauthorized"));
|
|
||||||
deleteFile(filepath);
|
|
||||||
}
|
|
||||||
else if (!file || (file.mimetype !== 'image/jpeg' && file.mimetype !== 'image/png')) {
|
|
||||||
res.status(500).send(getResponse(500, "No image file type"));
|
|
||||||
deleteFile(filepath);
|
|
||||||
}
|
|
||||||
else if (file.size > maxsize) {
|
|
||||||
res.status(500).send(getResponse(500, "Exceeds file size limit"));
|
|
||||||
deleteFile(filepath);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
http.get(auth+token, function (resp) {
|
|
||||||
var responseString = "";
|
|
||||||
resp.on("data", function (data) {
|
|
||||||
responseString += data;
|
|
||||||
});
|
|
||||||
resp.on("end", function () {
|
|
||||||
var result = JSON.parse(responseString);
|
|
||||||
if(result.error) {
|
|
||||||
res.status(401).send(getResponse(401, "Unauthorized"));
|
|
||||||
deleteFile(filepath);
|
|
||||||
} else {
|
|
||||||
// if user id contains id param or is Admin or Curator keep file and send information, else delete it.
|
|
||||||
if(result.sub.indexOf(req.params.id) !== -1 || isAdminOrCurator(result.edu_person_entitlements)) {
|
|
||||||
res.send(file);
|
|
||||||
} else {
|
|
||||||
res.status(401).send(getResponse(401, "Unauthorized"));
|
|
||||||
deleteFile(filepath);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
|
app.post('/upload/:id', upload.single('photo'), (req, res) => {
|
||||||
|
sendFile(req, res, (result) => {
|
||||||
|
return result.sub.indexOf(req.params.id) !== -1 || isAdminOrCurator(result.edu_person_entitlements);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
app.get('/download/:filename', function (req, res) {
|
app.get('/download/:filename', function (req, res) {
|
||||||
res.download('./uploads/' + req.params.filename);
|
res.download('./uploads/' + req.params.filename);
|
||||||
});
|
});
|
||||||
|
|
||||||
app.get('/tiny', function (req, res) {
|
app.get('/tiny', function (req, res) {
|
||||||
TinyURL.shorten(req.query.url, function(res1, err) {
|
TinyURL.shorten(req.query.url, function (res1, err) {
|
||||||
if (err)
|
if (err)
|
||||||
console.log(err)
|
console.log(err)
|
||||||
if(res1 !== 'Error') {
|
if (res1 !== 'Error') {
|
||||||
res.send(res1);
|
res.send(res1);
|
||||||
} else {
|
} else {
|
||||||
res.send(req.query.url);
|
res.send(req.query.url);
|
||||||
|
@ -148,22 +117,22 @@ app.get('/tiny', function (req, res) {
|
||||||
});
|
});
|
||||||
|
|
||||||
app.delete('/delete/:filename', function (req, res) {
|
app.delete('/delete/:filename', function (req, res) {
|
||||||
const token = req.headers['x-xsrf-token'];
|
const token = req.cookies['AccessToken'];
|
||||||
if(!token) {
|
if (!token) {
|
||||||
res.status(401).send(getResponse(401, "Unauthorized"));
|
res.status(401).send(getResponse(401, "Unauthorized"));
|
||||||
} else {
|
} else {
|
||||||
http.get(auth+token, function (resp) {
|
http.get(auth + token, function (resp) {
|
||||||
var responseString = "";
|
var responseString = "";
|
||||||
resp.on("data", function (data) {
|
resp.on("data", function (data) {
|
||||||
responseString += data;
|
responseString += data;
|
||||||
});
|
});
|
||||||
resp.on("end", function () {
|
resp.on("end", function () {
|
||||||
var result = JSON.parse(responseString);
|
var result = JSON.parse(responseString);
|
||||||
if(result.error) {
|
if (result.error) {
|
||||||
res.status(401).send(getResponse(401, "Unauthorized"));
|
res.status(401).send(getResponse(401, "Unauthorized"));
|
||||||
} else {
|
} else {
|
||||||
// if user id is on filename or is Admin delete file else unauthorized.
|
// if user id is on filename or is Admin delete file else unauthorized.
|
||||||
if(result.sub.indexOf(req.params.filename.split('-')[0]) !== -1 || isAdminOrCurator(result.edu_person_entitlements)) {
|
if (result.sub.indexOf(req.params.filename.split('-')[0]) !== -1 || isAdminOrCurator(result.edu_person_entitlements)) {
|
||||||
deleteFile('./uploads/' + req.params.filename);
|
deleteFile('./uploads/' + req.params.filename);
|
||||||
return res.status(200).send(getResponse(200, "File Deleted Successfully"));
|
return res.status(200).send(getResponse(200, "File Deleted Successfully"));
|
||||||
} else {
|
} else {
|
||||||
|
@ -179,6 +148,43 @@ const server = app.listen(8000, function () {
|
||||||
console.log("Listening on port %s...", server.address().port);
|
console.log("Listening on port %s...", server.address().port);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
function sendFile(req, res, authorized) {
|
||||||
|
const token = req.cookies['AccessToken'];
|
||||||
|
const file = req.file;
|
||||||
|
var filepath = (localPath ? "." : __dirname) + "/" + file.path;
|
||||||
|
if (!token) {
|
||||||
|
res.status(401).send(getResponse(401, "Unauthorized"));
|
||||||
|
deleteFile(filepath);
|
||||||
|
} else if (!file || (file.mimetype !== 'image/jpeg' && file.mimetype !== 'image/png')) {
|
||||||
|
res.status(500).send(getResponse(500, "No image file type"));
|
||||||
|
deleteFile(filepath);
|
||||||
|
} else if (file.size > maxsize) {
|
||||||
|
res.status(500).send(getResponse(500, "Exceeds file size limit"));
|
||||||
|
deleteFile(filepath);
|
||||||
|
} else {
|
||||||
|
http.get(auth + token, function (resp) {
|
||||||
|
var responseString = "";
|
||||||
|
resp.on("data", function (data) {
|
||||||
|
responseString += data;
|
||||||
|
});
|
||||||
|
resp.on("end", function () {
|
||||||
|
var result = JSON.parse(responseString);
|
||||||
|
if (result.error) {
|
||||||
|
res.status(401).send(getResponse(401, "Unauthorized"));
|
||||||
|
deleteFile(filepath);
|
||||||
|
} else {
|
||||||
|
if (authorized(result)) {
|
||||||
|
res.send(file);
|
||||||
|
} else {
|
||||||
|
res.status(401).send(getResponse(401, "Unauthorized"));
|
||||||
|
deleteFile(filepath);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function getResponse(code, message) {
|
function getResponse(code, message) {
|
||||||
var response = {};
|
var response = {};
|
||||||
response["code"] = code;
|
response["code"] = code;
|
||||||
|
@ -205,44 +211,33 @@ function deleteFile(filepath) {
|
||||||
function isAdminOrCurator(roles) {
|
function isAdminOrCurator(roles) {
|
||||||
var isAdmin = false;
|
var isAdmin = false;
|
||||||
var isCurator = false;
|
var isCurator = false;
|
||||||
for(var i = 0; i < roles.length; i++) {
|
for (var i = 0; i < roles.length; i++) {
|
||||||
if(roles[i] === 'urn:geant:openaire.eu:group:Portal+Administrator#aai.openaire.eu') {
|
if (roles[i] === 'urn:geant:openaire.eu:group:Portal+Administrator#aai.openaire.eu') {
|
||||||
isAdmin = true;
|
isAdmin = true;
|
||||||
}
|
}
|
||||||
if(roles[i] === 'urn:geant:openaire.eu:group:Curator+-+Community#aai.openaire.eu') {
|
if (roles[i] === 'urn:geant:openaire.eu:group:Curator+-+Community#aai.openaire.eu') {
|
||||||
isCurator = true;
|
isCurator = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return isAdmin || isCurator;
|
return isAdmin || isCurator;
|
||||||
}
|
}
|
||||||
|
|
||||||
function checkCookies(request){
|
function checkCookies(request) {
|
||||||
var valid = true;
|
var valid = true;
|
||||||
var cookieValue = request.cookies.AccessToken;
|
var cookieValue = request.cookies.AccessToken;
|
||||||
if(cookieValue === undefined || cookieValue === ''){
|
if (cookieValue === undefined || cookieValue === '') {
|
||||||
console.log("no cookie available");
|
console.log("no cookie available");
|
||||||
valid = false;
|
valid = false;
|
||||||
} else {
|
} else if (!hasValidOrigin(request.headers.origin)) {
|
||||||
const headerValue = request.headers['x-xsrf-token'];
|
console.log("no proper origin");
|
||||||
if(headerValue === undefined || headerValue === ''){
|
valid = false;
|
||||||
console.log("no header available");
|
|
||||||
valid = false;
|
|
||||||
} else{
|
|
||||||
if(cookieValue !== headerValue){
|
|
||||||
console.log("no proper header or cookie");
|
|
||||||
valid = false;
|
|
||||||
} else if(!hasValidOrigin(request.headers.origin)){
|
|
||||||
console.log("no proper origin");
|
|
||||||
valid = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return valid;
|
return valid;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
function hasValidOrigin(origin) {
|
function hasValidOrigin(origin) {
|
||||||
if(origin !== undefined && origin.indexOf(originServer) !== -1) {
|
if (origin !== undefined && origin.indexOf(originServer) !== -1) {
|
||||||
return true;
|
return true;
|
||||||
} else {
|
} else {
|
||||||
console.log("Not valid origin. Origin server is \"" + origin +
|
console.log("Not valid origin. Origin server is \"" + origin +
|
||||||
|
|
Loading…
Reference in New Issue