Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added 'hashAlgorithm' options to use another algorithm for hashing file content other than md5 #245

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/fileFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ module.exports = (options, fileUploadOptions = {}) => {
// firefox uploads empty file in case of cache miss when f5ing page.
// resulting in unexpected behavior. if there is no file data, the file is invalid.
// if (!fileUploadOptions.useTempFiles && !options.buffer.length) return;

// Create and return file object.
return {
name: options.name,
Expand All @@ -50,7 +50,7 @@ module.exports = (options, fileUploadOptions = {}) => {
tempFilePath: options.tempFilePath,
truncated: options.truncated,
mimetype: options.mimetype,
md5: options.hash,
[fileUploadOptions.hashAlgorithm || 'md5']: options.hash,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

couldn't we just use hash here as the object key?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, but that a breaking change require a major version bump.
doing its this way make it backward compatiable with existing code expecting the md5 field

mv: (filePath, callback) => {
// Define a propper move function.
const moveFunc = fileUploadOptions.useTempFiles
Expand Down
1 change: 1 addition & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const DEFAULT_OPTIONS = {
createParentPath: false,
parseNested: false,
useTempFiles: false,
hashAlgorithm: 'md5',
tempFileDir: path.join(process.cwd(), 'tmp')
};

Expand Down
2 changes: 1 addition & 1 deletion lib/memHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const { debugLog } = require('./utilities');
*/
module.exports = (options, fieldname, filename) => {
const buffers = [];
const hash = crypto.createHash('md5');
const hash = crypto.createHash(options.hashAlgorithm);
let fileSize = 0;
let completed = false;

Expand Down
6 changes: 3 additions & 3 deletions lib/tempFileHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ module.exports = (options, fieldname, filename) => {
checkAndMakeDir({ createParentPath: true }, tempFilePath);

debugLog(options, `Temporary file path is ${tempFilePath}`);
const hash = crypto.createHash('md5');

const hash = crypto.createHash(options.hashAlgorithm);
let fileSize = 0;
let completed = false;

Expand Down Expand Up @@ -54,7 +54,7 @@ module.exports = (options, fieldname, filename) => {
completed = true;
debugLog(options, `Cleaning up temporary file ${tempFilePath}...`);
writeStream.end();
deleteFile(tempFilePath, err => (err
deleteFile(tempFilePath, err => (err
? debugLog(options, `Cleaning up temporary file ${tempFilePath} failed: ${err}`)
: debugLog(options, `Cleaning up temporary file ${tempFilePath} done.`)
));
Expand Down