Last active
August 29, 2015 14:06
-
-
Save DapperFox/b754efa70701c0e9f346 to your computer and use it in GitHub Desktop.
fs issues
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 'use strict'; | |
| var ImageUpload = require('./models/images.js'); | |
| module.exports = function(app, fs) { | |
| app.post('/upload', isLoggedIn, function(req, res) { | |
| req.pipe(req.busboy); | |
| req.busboy.on('file', function(fieldname, file, filename) { | |
| var image = new ImageUpload({ | |
| userID: req.user._id, | |
| imageLocation: '/uploads/' + req.user._id + '/' + filename | |
| }); | |
| image.save(function(err, log) { | |
| if (err) { | |
| console.error(err); | |
| } | |
| }); | |
| fs.mkdir(__dirname + '/uploads/' + req.user._id, function(err) { | |
| console.log(err); | |
| var fstream = fs.createWriteStream(__dirname + '/uploads/' + req.user._id + '/' + filename); | |
| file.pipe(fstream); | |
| fstream.on('close', function() { | |
| res.redirect('back'); | |
| }); | |
| }); | |
| }); | |
| }); | |
| }; | |
| function isLoggedIn(req, res, next) { | |
| if (req.isAuthenticated()) { | |
| return next(); | |
| } | |
| res.redirect('back'); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment