got pdf upload to work

This commit is contained in:
David Baldwynn 2015-07-02 14:49:21 -07:00
parent 09ce94152a
commit 5bbac2963f
4 changed files with 137 additions and 52 deletions

View file

@ -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
*/

View file

@ -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)

View file

@ -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

View file

@ -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:'=',
}
};
}
]);