Skip to content
This repository has been archived by the owner on Aug 18, 2021. It is now read-only.

Commit

Permalink
create custom storage engine to get hash of file
Browse files Browse the repository at this point in the history
  • Loading branch information
tdjsnelling committed Jan 31, 2018
1 parent bb6f9b4 commit 5b2df52
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 1 deletion.
71 changes: 71 additions & 0 deletions customstorage.js
@@ -0,0 +1,71 @@
var fs = require('fs');
var os = require('os');
var path = require('path');
var crypto = require('crypto');
var mkdirp = require('mkdirp');

function getFilename (req, file, cb) {
crypto.pseudoRandomBytes(16, function (err, raw) {
cb(err, err ? undefined : raw.toString('hex'));
});
}

function getDestination (req, file, cb) {
cb(null, os.tmpdir());
}

function customStorage (opts) {
this.getFilename = (opts.filename || getFilename);

if (typeof opts.destination === 'string') {
mkdirp.sync(opts.destination);
this.getDestination = function ($0, $1, cb) { cb(null, opts.destination) };
} else {
this.getDestination = (opts.destination || getDestination);
}
}

customStorage.prototype._handleFile = function _handleFile (req, file, cb) {
var that = this;
var hash = crypto.createHash('sha256');

that.getDestination(req, file, function (err, destination) {
if (err) return cb(err);

that.getFilename(req, file, function (err, filename) {
if (err) return cb(err);

var finalPath = path.join(destination, filename);
var outStream = fs.createWriteStream(finalPath);

file.stream.pipe(outStream);
outStream.on('error', cb);
file.stream.on('data', function (chunk) {
hash.update(chunk);
});
outStream.on('finish', function () {
cb(null, {
destination: destination,
filename: filename,
path: finalPath,
size: outStream.bytesWritten,
hash: hash.digest('hex')
});
});
});
});
}

customStorage.prototype._removeFile = function _removeFile (req, file, cb) {
var path = file.path;

delete file.destination;
delete file.filename;
delete file.path;

fs.unlink(path, cb);
}

module.exports = function (opts) {
return new customStorage(opts);
}
5 changes: 4 additions & 1 deletion index.js
Expand Up @@ -10,6 +10,7 @@ var consolidate = require('consolidate');
var useragent = require('express-useragent'); var useragent = require('express-useragent');
var mongoose = require('mongoose'); var mongoose = require('mongoose');


var customstorage = require('./customstorage');
var config = require('./config'); var config = require('./config');


var port = config.port; var port = config.port;
Expand All @@ -26,10 +27,11 @@ var uploadRecord = mongoose.model('upload', {
ip: String, ip: String,
originalFilename: String, originalFilename: String,
filename: String, filename: String,
hash: String,
timestamp: Number timestamp: Number
}); });


var storage = multer.diskStorage({ var storage = customstorage({
destination: function (req, file, cb) { destination: function (req, file, cb) {
cb(null, uploadPath) cb(null, uploadPath)
}, },
Expand Down Expand Up @@ -93,6 +95,7 @@ app.post('/', (req, res) => {
ip: ip, ip: ip,
originalFilename: req.file.originalname, originalFilename: req.file.originalname,
filename: req.file.filename, filename: req.file.filename,
hash: req.file.hash,
timestamp: + new Date() timestamp: + new Date()
}); });


Expand Down

0 comments on commit 5b2df52

Please sign in to comment.