Fix warning when compiling with ninja
[samplevnf.git] / VNF_Catalogue / routes / edit_project.js
1 /*******************************************************************************
2  * Copyright (c) 2017 Kumar Rishabh and others.
3  *
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Apache License, Version 2.0
6  * which accompanies this distribution, and is available at
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *******************************************************************************/
9
10 var express = require('express');
11 var router = express.Router();
12 var multer = require('multer');
13
14 var max_size = 1 * 1000 * 1000; // image size_limit
15
16 var storage =   multer.diskStorage({
17   destination: function (req, file, callback) {
18     callback(null, './public/uploads');
19   },
20   filename: function (req, file, callback) {
21     console.log(file);
22     console.log(req.body);
23     callback(null, file.fieldname + '-' + Date.now() + '.png');
24   }
25 });
26
27 var fileFilter = function (req, file, cb) {
28   if (file.mimetype !== 'image/png') {
29     //req.fileValidationError = 'goes wrong on the mimetype';
30     cb(null, false);
31   } else {
32     cb(null, true);
33   }
34 }
35
36 var upload = multer({ fileFilter: fileFilter, limits: { fileSize: max_size }, storage : storage}).single('file_upload');
37
38
39 router.post('/', function(req, res) {
40   upload(req,res,function(err) {
41         console.log(req.body);
42         console.log(req.file)
43         if(req.file == null && req.body['file_url'] != '') {
44             response = 'File Upload error: wrong Filetype, only png supported as of now';
45             res.status(500);
46             res.end(JSON.stringify({'error': response}));
47
48         }
49         if(err) {
50             console.log(err);
51             response = 'File Upload error: ' + err;
52             console.log(response);
53             //return res.end(req.fileValidationError);
54             res.status(500);
55             res.end(JSON.stringify({'error': response}));
56             return;
57         }
58
59         console.log(req.file);
60         req.body['photo_url'] = (req.file) ? req.file['filename'] : 'logo.png';
61         console.log(req.body);
62
63         req.checkBody("vnf_name", "VNF Name must not be empty").notEmpty();
64         req.checkBody("repo_url", "Repository URL must not be empty").notEmpty();
65         req.checkBody("license", "Please select a License").notEmpty();
66         req.checkBody("opnfv_indicator", "Please select an OPNFV Indicator").notEmpty();
67         req.checkBody("repo_url", "Must be a Github URL").matches('.*github\.com.*');
68
69         var errors = req.validationErrors();
70         console.log(errors);
71
72         var response = '';  for(var i = 0; i < errors.length; i++) {
73             console.log(errors[i]['msg']);
74             response = response + errors[i]['msg'] + '; ';
75         }
76
77         if(errors) {    res.status(500);
78             res.send({'error': response});
79             return;
80         }
81
82         var vnf_details = req.body;
83         delete vnf_details.file_url;
84
85         db_pool.getConnection(function(err, connection) {
86             // Use the connection
87
88           sql_query = 'INSERT INTO photo(photo_url) values(\'' + req.body['photo_url'] + '\')\;SELECT LAST_INSERT_ID() photo_id';
89           // TODO look above query prone to sql_injections
90
91           console.log(sql_query);
92           connection.query(sql_query, function (error, results, fields) {
93              console.log('hola');
94              console.log(results[1][0].photo_id);
95               //connection.query(sql_query, vnf_details, function (error, results, fields) {
96              delete vnf_details.photo_url;
97              vnf_details['photo_id'] = results[1][0].photo_id;
98              sql_query = 'INSERT INTO vnf SET ?'
99                connection.query(sql_query, vnf_details, function (error, results, fields) {
100              // And done with the connection.
101              connection.release();
102              if (error) throw error;
103
104              // Handle error after the release.
105              res.end('{"success" : "Updated Successfully", "status" : 200}');
106              return;
107                // Don't use the connection here, it has been returned to the pool.
108                });
109           });
110         });
111
112
113   });
114
115 });
116
117 module.exports = router;