1 /*******************************************************************************
2 * Copyright (c) 2017 Kumar Rishabh and others.
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 *******************************************************************************/
10 var express = require('express');
11 var router = express.Router();
12 var multer = require('multer');
14 var max_size = 1 * 1000 * 1000; // image size_limit
16 var storage = multer.diskStorage({
17 destination: function (req, file, callback) {
18 callback(null, './public/uploads');
20 filename: function (req, file, callback) {
22 console.log(req.body);
23 callback(null, file.fieldname + '-' + Date.now() + '.png');
27 var fileFilter = function (req, file, cb) {
28 if (file.mimetype !== 'image/png') {
29 //req.fileValidationError = 'goes wrong on the mimetype';
36 var upload = multer({ fileFilter: fileFilter, limits: { fileSize: max_size }, storage : storage}).single('file_upload');
39 router.post('/', function(req, res) {
40 upload(req,res,function(err) {
41 console.log(req.body);
43 if(req.file == null && req.body['file_url'] != '') {
44 response = 'File Upload error: wrong Filetype, only png supported as of now';
46 res.end(JSON.stringify({'error': response}));
51 response = 'File Upload error: ' + err;
52 console.log(response);
53 //return res.end(req.fileValidationError);
55 res.end(JSON.stringify({'error': response}));
59 console.log(req.file);
60 req.body['photo_url'] = (req.file) ? req.file['filename'] : 'logo.png';
61 console.log(req.body);
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.*');
69 var errors = req.validationErrors();
72 var response = ''; for(var i = 0; i < errors.length; i++) {
73 console.log(errors[i]['msg']);
74 response = response + errors[i]['msg'] + '; ';
77 if(errors) { res.status(500);
78 res.send({'error': response});
82 var vnf_details = req.body;
83 delete vnf_details.file_url;
85 db_pool.getConnection(function(err, connection) {
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
91 console.log(sql_query);
92 connection.query(sql_query, function (error, results, fields) {
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;
104 // Handle error after the release.
105 res.end('{"success" : "Updated Successfully", "status" : 200}');
107 // Don't use the connection here, it has been returned to the pool.
117 module.exports = router;