Index: modules/uoa-services-portal/branches/angular7/services/upload/uploadService.js
===================================================================
--- modules/uoa-services-portal/branches/angular7/services/upload/uploadService.js	(revision 55704)
+++ modules/uoa-services-portal/branches/angular7/services/upload/uploadService.js	(revision 55705)
@@ -2,53 +2,146 @@
 var bodyParser = require("body-parser");
 var multer = require("multer");
 var app = express();
-var cors = require('cors')
+var cors = require('cors');
+var http = require("http");
+
+var auth = 'http://scoobydoo.di.uoa.gr:8080/dnet-openaire-users-1.0.0-SNAPSHOT/api/users/getUserInfo?accessToken=';
+
+var storage = multer.diskStorage({
+    destination: function (req, file, cb) {
+        cb(null, 'uploads')
+    },
+    filename: function (req, file, cb) {
+        if(req.params.id) {
+            cb(null, req.params.id + '-' + new Date().getTime() + '.' + file.originalname.split('.').pop());
+        } else {
+            cb(null, file.originalname);
+        }
+    }
+});
+
+var upload = multer({ storage: storage })
 app.use(cors());
 
 app.use(bodyParser.json());
-app.use(bodyParser.urlencoded({ extended: true }));
+app.use(bodyParser.urlencoded({extended: true}));
 
-app.use(function(req, res, next) {
+app.use(function (req, res, next) {
     res.header("Access-Control-Allow-Origin", "*");
     res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
     next();
 });
 
-app.post("/upload", multer({dest: "/home/argirok/projects/openaire/uoa-services-portal/trunk/services/upload/uploads/"}).array("uploads[]", 12), function(req, res) {
-  var filepath ="/"+req.files[0].path;
-  console.log(req.files[0]);
-  if (req.files[0].mimetype != 'text/csv') {
-      console.error("No proper file type");
-      res.status(500).send(getResponse(500,"No proper file type")) ;
-  }else{
-    //send file
-    res.sendFile(filepath);
-  }
-  var fs = require('fs');
-  fs.stat(filepath, function (err, stats) {
-  console.log(stats);//here we got all information of file in stats variable
+app.post("/upload", upload.array("uploads[]", 12), function (req, res) {
+    var filepath = "./" + req.files[0].path;
+    if (req.files[0].mimetype != 'text/csv') {
+        console.error("No proper file type");
+        res.status(500).send(getResponse(500, "No proper file type"));
+    } else {
+        //send file
+        res.download(filepath);
+    }
+    deleteFile(filepath);
 
-  if (err) {
-      return console.error(err);
-  }
+});
 
-  fs.unlink(filepath,function(err){
-       if(err) return console.log(err);
-       console.log('file deleted successfully');
-  });
+app.post('/upload/:id', upload.single('photo'), (req, res) => {
+    const token = req.headers['x-xsrf-token'];
+    const file = req.file;
+    const filepath = "./" + 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 {
+        http.get(auth+token, function (resp) {
+            var responseString = "";
+            resp.on("data", function (data) {
+                responseString += data;
+            });
+            resp.on("end", function () {
+                var result = JSON.parse(responseString);
+                console.log(result);
+                if(result.error) {
+                    res.status(401).send(getResponse(401, "Unauthorized"));
+                    deleteFile(filepath);
+                } else {
+                    // if user id contains id param keep file and send information, else delete it.
+                    if(result.sub.indexOf(req.params.id) !== -1) {
+                        file.filename = file.filename;
+                        res.send(file);
+                    } else {
+                        res.status(401).send(getResponse(401, "Unauthorized"));
+                        deleteFile(filepath);
+                    }
+                }
+            });
+        });
+    }
+
 });
 
+app.get('/download/:filename', function (req, res) {
+   res.download('./uploads/' + req.params.filename);
 });
-app.get('/get/d6bd36a06762e2f0da891f9670fcb2b0', function(req, res) {
-  res.sendFile('uploads/d6bd36a06762e2f0da891f9670fcb2b0')
-})
-var server = app.listen(8000, function() {
+
+app.delete('/delete/:filename', function (req, res) {
+    const token = req.headers['x-xsrf-token'];
+    if(!token) {
+        res.status(401).send(getResponse(401, "Unauthorized"));
+    } 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);
+                console.log(result);
+                if(result.error) {
+                    res.status(401).send(getResponse(401, "Unauthorized"));
+                } else {
+                    // if user id is on filename delete file else unauthorized.
+                    if(result.sub.indexOf(req.params.filename.split('-')[0]) !== -1) {
+                        deleteFile('./uploads/' + req.params.filename);
+                        return res.status(200).send(getResponse(200, "File Deleted Successfully"));
+                    } else {
+                        res.status(401).send(getResponse(401, "Unauthorized"));
+                    }
+                }
+            });
+        });
+    }
+});
+
+const server = app.listen(8000, function () {
     console.log("Listening on port %s...", server.address().port);
 });
-function getResponse(code, message){
-  var response ={};
-  response["code"]=code;
-  response["message"]=message;
-  return response;
 
+function getResponse(code, message) {
+    var response = {};
+    response["code"] = code;
+    response["message"] = message;
+    return response;
 }
+
+function deleteFile(filepath) {
+    const fs = require('fs');
+    fs.stat(filepath, function (err, stats) {
+        console.log(stats); //here we got all information of file in stats variable
+
+        if (err) {
+            return console.error(err);
+        }
+
+        fs.unlink(filepath, function (err) {
+            if (err) return console.log(err);
+            console.log('file deleted successfully');
+        });
+    });
+}
+
