Skip to content

Commit

Permalink
added file upload
Browse files Browse the repository at this point in the history
  • Loading branch information
abarnhard committed Aug 19, 2014
1 parent 0683d1a commit f303f42
Show file tree
Hide file tree
Showing 17 changed files with 54 additions and 14 deletions.
18 changes: 15 additions & 3 deletions app/controllers/vacations.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
'use strict';

var Vacation = require('../models/vacation'),
moment = require('moment');
moment = require('moment'),
mp = require('multiparty');

exports.init = function(req, res){
res.render('vacations/init');
Expand All @@ -28,10 +29,21 @@ exports.show = function(req, res){
});
};

exports.addPhoto = function(req, res){
exports.downloadPhoto = function(req, res){
Vacation.findById(req.params.id, function(err, v){
v.addPhoto(req.body.url, function(){
v.downloadPhoto(req.body.url, function(){
res.redirect('/vacations/' + req.params.id);
});
});
};

exports.uploadPhotos = function(req, res){
Vacation.findById(req.params.id, function(err, v){
var form = new mp.Form();
form.parse(req, function(err, fields, files){
v.uploadPhotos(files, function(){
res.redirect('/vacations/' + req.params.id);
});
});
});
};
26 changes: 24 additions & 2 deletions app/models/vacation.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

var Mongo = require('mongodb'),
_ = require('lodash'),
cp = require('child_process');
cp = require('child_process'),
fs = require('fs'),
path = require('path');

function Vacation(o){
this.name = o.name.trim();
Expand Down Expand Up @@ -36,7 +38,7 @@ Vacation.findById = function(id, cb){
});
};

Vacation.prototype.addPhoto = function(url, cb){
Vacation.prototype.downloadPhoto = function(url, cb){
var exts = url.split('.'),
ext = exts[exts.length - 1],
dir = this._id.toString(),
Expand All @@ -52,6 +54,26 @@ Vacation.prototype.addPhoto = function(url, cb){
});
};

Vacation.prototype.uploadPhotos = function(files, cb){
var dir = __dirname + '/../static/img/' + this._id,
exists = fs.existsSync(dir),
self = this;

if(!exists){
fs.mkdirSync(dir);
}

files.photos.forEach(function(photo){
var ext = path.extname(photo.path),
rel = '/img/' + self._id.toString() + '/' + self.photos.length + ext,
abs = dir + '/' + self.photos.length + ext;
fs.renameSync(photo.path, abs);
self.photos.push(rel);
});

Vacation.collection.save(self, cb);
};

module.exports = Vacation;
/* Will need later:
Vacation.collection.update({_id:id}, {$set:{balance:a.balance}, $inc:{numTransacts:1}, $push:{transactions:t}}, function(){
Expand Down
3 changes: 2 additions & 1 deletion app/routes/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ module.exports = function(app, express){
app.post('/vacations', vacations.create);
app.get('/vacations', vacations.index);
app.get('/vacations/:id', vacations.show);
app.post('/vacations/:id/photos/new', vacations.addPhoto);
app.post('/vacations/:id/photos/download', vacations.downloadPhoto);
app.post('/vacations/:id/photos/upload', vacations.uploadPhotos);

console.log('Routes Loaded');
};
Expand Down
Binary file added app/static/img/000000000000000000000001/3.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/static/img/000000000000000000000001/4.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/static/img/000000000000000000000001/5.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/static/img/000000000000000000000002/0.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/static/img/000000000000000000000002/1.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/static/img/000000000000000000000002/2.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/static/img/000000000000000000000003/0.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/static/img/000000000000000000000003/1.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/static/img/000000000000000000000003/2.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/static/img/53f3705bad7aec2008b19169/0.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/static/img/53f3705bad7aec2008b19169/1.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/static/img/53f3705bad7aec2008b19169/2.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
18 changes: 11 additions & 7 deletions app/views/vacations/show.jade
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,22 @@ block content
td= vacation.photos.length
.row
.col-xs-12
form.form-horizontal(role='form', method='post', action='/vacations/#{vacation._id}/photos/new')
form(role='form', method='post', action='/vacations/#{vacation._id}/photos/download')
.form-group
label.col-sm-2.control-label(for='url') Image URL:
.col-sm-10
input.form-control#name(type='text', name='url', placeholder='http://image.com/photo.jpg')
label(for='url') Photo URL
input.form-control#url(type='text', name='url')
button.btn.btn-success(type='submit') Add Photo
.row
.col-xs-12
form(role='form', method='post', action='/vacations/#{vacation._id}/photos/upload', enctype='multipart/form-data')
.form-group
.col-sm-offset-2.col-sm-10
button.btn.btn-default(type='submit') Add Image
label(for='photos') Photos
input.form-control#photos(type='file', name='photos', multiple=true)
button.btn.btn-success(type='submit') Add Photo
.row
.col-xs-12
each photo in vacation.photos
.photo(style='background-image:url(#{photo});')
a(href='#{photo}'): .photo(style='background-image:url(#{photo});')

block scripts
script(src='/js/user/vacation-show.js')
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@
"lodash": "^2.4.1",
"moment": "^2.8.1",
"mongodb": "^1.4.8",
"morgan": "^1.2.3"
"morgan": "^1.2.3",
"multiparty": "^3.3.2"
},
"devDependencies": {
"blanket": "^1.1.6",
Expand Down

0 comments on commit f303f42

Please sign in to comment.