Created
November 10, 2015 01:12
-
-
Save bastianwegge/2aa2ac457a0d918b54c9 to your computer and use it in GitHub Desktop.
an implementation of the gridfs-stream module by the awesome @mleanos
This file contains 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 mongoose = require('mongoose'), | |
path = require('path'), | |
errorHandler = require(path.resolve('./modules/core/server/controllers/errors.server.controller')), | |
_ = require('lodash'); | |
var Grid = require('gridfs-stream'); | |
Grid.mongo = mongoose.mongo; | |
var gfs = new Grid(mongoose.connection.db); | |
/* | |
* Use MongoDb's built in GridFS feature for file system object storage. | |
* // testing this | |
* We will merely use GridFS to upload a file, and to retrieve it. Every other interactions should be | |
* at the application level (domain-specific). | |
* | |
*/ | |
exports.create = function(req, res) { | |
console.log('inside upload.create()'); | |
var part = req.files.file; | |
console.log(part); | |
var writeStream = gfs.createWriteStream({ | |
filename: part.originalname, | |
mode: 'w', | |
content_type: part.mimetype | |
}); | |
writeStream.on('close', function(file) { | |
// remove md5 hash from file data, for security reasons | |
delete file.md5; | |
console.log(file); | |
return res.status(200).send({ | |
message: 'Success', | |
// return necessary file data with the response | |
data: file | |
}); | |
}); | |
writeStream.write(part.buffer); | |
writeStream.end(); | |
}; | |
exports.read = function(req, res) { | |
console.log('inside uploads.read()'); | |
console.log(req.params.uploadId.toString()); | |
gfs.files.find({ | |
_id: mongoose.Types.ObjectId.createFromHexString(req.params.uploadId) | |
}).toArray(function(err, files) { | |
if (files.length === 0) { | |
return res.status(400).send({ | |
message: 'File not found' | |
}); | |
} | |
res.writeHead(200, { | |
'Content-Type': files[0].contentType | |
}); | |
var readstream = gfs.createReadStream({ | |
filename: files[0].filename | |
}); | |
readstream.on('data', function(data) { | |
res.write(data); | |
}); | |
readstream.on('end', function() { | |
res.end(); | |
}); | |
readstream.on('error', function(err) { | |
console.log('An error occurred!', err); | |
throw err; | |
}); | |
}); | |
}; | |
exports.remove = function(req, res) { | |
gfs.remove({ | |
_id: mongoose.Types.ObjectId.createFromHexString(req.params.uploadId.toString()) | |
}, function(err) { | |
if (err) { | |
console.log('Error removing file'); | |
console.log(err); | |
return res.status(400).send({ | |
message: errorHandler.getErrorMessage(err) | |
}); | |
} | |
return res.status(200).send({ | |
message: 'file remove ' + req.params.id | |
}); | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment