From 5bbac2963f8722fc1811cdb4a8f3d5f33037002f Mon Sep 17 00:00:00 2001 From: David Baldwynn Date: Thu, 2 Jul 2015 14:49:21 -0700 Subject: [PATCH] got pdf upload to work --- app/controllers/forms.server.controller.js | 124 ++++++++++++------ app/routes/forms.server.routes.js | 2 +- config/express.js | 32 +++-- .../directives/table.client.directive.js | 31 +++++ 4 files changed, 137 insertions(+), 52 deletions(-) create mode 100644 public/modules/forms/directives/table.client.directive.js diff --git a/app/controllers/forms.server.controller.js b/app/controllers/forms.server.controller.js index 6350409a..a0aaadc4 100644 --- a/app/controllers/forms.server.controller.js +++ b/app/controllers/forms.server.controller.js @@ -36,55 +36,99 @@ exports.create = function(req, res) { /** * Upload PDF */ -exports.uploadPDF = function(files, user, cb) { - var _user = JSON.parse(''+user); - console.log(_user.username); - console.log(config.tmpUploadPath); +exports.uploadPDF = function(req, res, next) { - if(files) { + console.log('inside uploadPDF'); + if(req.files){ + var pdfFile = req.files.file; + var _user = req.user; + if (req.files.size === 0) { + next(new Error('File uploaded is EMPTY')); + }else if(req.files.size > 200000000){ + next(new Error('File uploaded exceeds MAX SIZE of 200MB')); + }else { + fs.exists(pdfFile.path, function(exists) { + //If file exists move to user's tmp directory + if(exists) { - console.log('inside uploadPDF'); - console.log(files.file[0]); - var pdfFile = files.file[0]; + var newDestination = config.tmpUploadPath+_user.username; + var newFilename = String(Date.now() % 987598 * 32 % (a - Date.now()))+'.pdf'; + var stat = null; + try { + stat = fs.statSync(newDestination); + } catch (err) { + fs.mkdirSync(newDestination); + } + if (stat && !stat.isDirectory()) { + console.log('Directory cannot be created'); + next(new Error('Directory cannot be created because an inode of a different type exists at "' + newDestination + '"')); + } + + fs.move(pdfFile.path, path.join(newDestination, pdfFile.name), function (err) { + if (err) { + next(new Error(err.message)); + } + pdfFile.name = newFilename; + pdfFile.path = path.join(newDestination, pdfFile.name); + console.log(pdfFile.name + ' uploaded to ' + pdfFile.path); + res.status(200).send(pdfFile); + }); - if (pdfFile.size === 0) { - throw new Error('Files uploaded are EMPTY'); + } else { + next(new Error('Did NOT get your file!')); + } + }); } - fs.exists(pdfFile.path, function(exists) { - //If file exists move to user's tmp directory - if(exists) { - - var newDestination = path.join(config.tmpUploadPath, _user.username); - var stat = null; - try { - stat = fs.statSync(newDestination); - } catch (err) { - fs.mkdirSync(newDestination); - } - if (stat && !stat.isDirectory()) { - console.log('Directory cannot be created'); - throw new Error('Directory cannot be created because an inode of a different type exists at "' + newDestination + '"'); - } - - fs.move(pdfFile.path, path.join(newDestination, pdfFile.name), function (err) { - if (err) { - throw new Error(err.message); - } - pdfFile.path = path.join(newDestination, pdfFile.name); - - return cb(pdfFile); - }); - - } else { - throw new Error('Did NOT get your file!'); - } - }); }else { - throw new Error('File NOT uploaded'); + next(new Error('Uploaded files were NOT detected')); } }; +// exports.uploadPDF = function(pdfFile, _user, cb) { +// // console.log(_user.username); +// // console.log(config.tmpUploadPath); + + +// console.log('inside uploadPDF'); +// // console.log(pdfFile); + +// if (pdfFile.size === 0) { +// cb(new Error('Files uploaded are EMPTY'), null); +// }else { +// fs.exists(pdfFile.path, function(exists) { +// //If file exists move to user's tmp directory +// if(exists) { + +// var newDestination = config.tmpUploadPath+""+_user.username; +// var stat = null; +// try { +// stat = fs.statSync(newDestination); +// } catch (err) { +// fs.mkdirSync(newDestination); +// } +// if (stat && !stat.isDirectory()) { +// console.log('Directory cannot be created'); +// cb(new Error('Directory cannot be created because an inode of a different type exists at "' + newDestination + '"'), null); +// } + +// fs.move(pdfFile.path, path.join(newDestination, pdfFile.name), function (err) { +// if (err) { +// return cb(new Error(err.message), null); +// } +// pdfFile.path = path.join(newDestination, pdfFile.name); + +// return cb(null, pdfFile); +// }); + +// } else { +// cb(new Error('Did NOT get your file!'), null); +// } +// }); +// } + +// }; + /** * Show the current form */ diff --git a/app/routes/forms.server.routes.js b/app/routes/forms.server.routes.js index d5340659..a49b79b8 100644 --- a/app/routes/forms.server.routes.js +++ b/app/routes/forms.server.routes.js @@ -9,7 +9,7 @@ var users = require('../../app/controllers/users.server.controller'), module.exports = function(app) { // Form Routes app.route('/upload/pdf') - .post(forms.uploadPDF); + .all(forms.uploadPDF); app.route('/forms') .get(users.requiresLogin, forms.list) diff --git a/config/express.js b/config/express.js index f36c9863..a0e65785 100755 --- a/config/express.js +++ b/config/express.js @@ -106,19 +106,29 @@ module.exports = function(db) { console.log(file.originalname + ' is starting ...'); }, onFileUploadComplete: function (file, req, res) { + console.log(file.originalname + ' uploaded to ' + file.path); // console.log('\n\nheadersSent in onFileUploadComplete: ', res.headersSent); - console.log(req.body.user); - try{ - formCtrl.uploadPDF(req.files, req.body.user, function(_file){ - console.log(_file.filename + ' uploaded to ' + _file.path); - res.status(200).send(_file); - }); - }catch(err) { - res.status(500).send({ - message: err.message - }); - } + // res.status(200).send(file); } + // console.log('\n\nheadersSent in onFileUploadComplete: ', res.headersSent); + // console.log(req.body.user); + // console.log(req.user); + // var _user = JSON.parse(req.body.user); + // console.log(file) + + // formCtrl.uploadPDF(file, _user, function(err, _file){ + // if(err){ + // console.log('\n\n ERROR: '+err.message) + // res.status(500).send({ + // message: err.message + // }); + // }else { + // console.log(_file.filename + ' uploaded to ' + _file.path); + // res.status(200).send(_file); + // } + + // }); + // } })); // CookieParser should be above session diff --git a/public/modules/forms/directives/table.client.directive.js b/public/modules/forms/directives/table.client.directive.js new file mode 100644 index 00000000..745db428 --- /dev/null +++ b/public/modules/forms/directives/table.client.directive.js @@ -0,0 +1,31 @@ +'use strict'; + +angular.module('forms').directive('tableDirective', ['$http', '$timeout', 'Auth', + function ($http, $timeout, Auth) { + return { + controller: function($scope){ + + $scope.toggleChecker = function(checked) { + var rows = $scope.gridOptions.$gridScope.renderedRows, + allChecked = true; + + for (var r = 0; r < rows.length; r++) { + if (rows[r].entity.checker !== true) { + allChecked = false; + break; + } + } + + $scope.gridOptions.$gridScope.checker = allChecked; + }; + + }, + templateUrl: './modules/forms/views/directiveViews/table/table.html', + restrict: 'E', + scope: { + rows:'=', + extras:'=', + } + }; + } +]); \ No newline at end of file