Jobs on Compass often timeout
[releng.git] / utils / test / vnfcatalogue / VNF_Catalogue / routes / add_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
15 var storage =   multer.diskStorage({
16   destination: function (req, file, callback) {
17     callback(null, './public/uploads');
18   },
19   filename: function (req, file, callback) {
20     console.log(file);
21     console.log(req.body);
22     callback(null, file.fieldname + '-' + Date.now() + '.jpg');
23   }
24 });
25
26 var fileFilter = function (req, file, cb) {
27   if (file.mimetype !== 'image/png') {
28     //req.fileValidationError = 'goes wrong on the mimetype';
29     cb(null, false);
30   } else {
31     cb(null, true);
32   }
33 }
34
35 var upload = multer({ fileFilter: fileFilter, storage : storage}).single('file_upload');
36
37
38 router.post('/', function(req, res) {
39   upload(req,res,function(err) {
40         console.log(req.body);
41         console.log(req.file)
42         if(req.file == null && req.body['file_url'] != '') {
43             response = 'File Upload error: wrong Filetype';
44             res.status(500);
45             res.end(JSON.stringify({'error': response}));
46
47         }
48         if(err) {
49             console.log(err);
50             response = 'File Upload error: ' + err;
51             console.log(response);
52             //return res.end(req.fileValidationError);
53             res.status(500);
54             res.send({'error': response});
55             return;
56         }
57
58         console.log(req.file);
59         req.body['photo_url'] = (req.file) ? req.file['filename'] : 'logo.png';
60         console.log(req.body);
61
62         req.checkBody("vnf_name", "VNF Name must not be empty").notEmpty();
63         req.checkBody("repo_url", "Repository URL must not be empty").notEmpty();
64         req.checkBody("license", "Please select a License").notEmpty();
65         req.checkBody("opnfv_indicator", "Please select an OPNFV Indicator").notEmpty();
66         req.checkBody("repo_url", "Must be a Github URL").matches('.*github\.com.*');
67
68         var errors = req.validationErrors();
69         console.log(errors);
70
71         var response = '';  for(var i = 0; i < errors.length; i++) {
72             console.log(errors[i]['msg']);
73             response = response + errors[i]['msg'] + '; ';
74         }
75
76         if(errors) {    res.status(500);
77             res.send({'error': response});
78             return;
79         }
80
81         var vnf_details = req.body;
82         delete vnf_details.file_url;
83
84         db_pool.getConnection(function(err, connection) {
85             // Use the connection
86
87           sql_query = 'INSERT INTO photo(photo_url) values(\'' + req.body['photo_url'] + '\')\;SELECT LAST_INSERT_ID() photo_id';
88           // TODO look above query prone to sql_injections
89
90           console.log(sql_query);
91           connection.query(sql_query, function (error, results, fields) {
92              console.log('hola');
93              console.log(results[1][0].photo_id);
94               //connection.query(sql_query, vnf_details, function (error, results, fields) {
95              delete vnf_details.photo_url;
96              vnf_details['photo_id'] = results[1][0].photo_id;
97              sql_query = 'INSERT INTO vnf SET ?'
98                connection.query(sql_query, vnf_details, function (error, results, fields) {
99              // And done with the connection.
100              connection.release();
101              if (error) throw error;
102
103              // Handle error after the release.
104              res.end('{"success" : "Updated Successfully", "status" : 200}');
105              return;
106                // Don't use the connection here, it has been returned to the pool.
107                });
108           });
109         });
110
111
112   });
113
114 });
115
116 module.exports = router;