-
-
Notifications
You must be signed in to change notification settings - Fork 268
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(api-attachment): Calculate file content hash when uploading attac…
…hment ZMS-172 (#733) * Added submission api endpoint to api docs generation * on attachment upload calculate file content hash * make stream into separate file, refactor * create stream during the try to store. otherwise getting stuck * refactor file content hash update * safer file content hash handling * refactor code. Fix possible race condition * refactor function. Pass callback as last function param
- Loading branch information
Showing
2 changed files
with
79 additions
and
4 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
'use strict'; | ||
|
||
const Transform = require('stream').Transform; | ||
const crypto = require('crypto'); | ||
|
||
class FileHashCalculatorStream extends Transform { | ||
constructor(options) { | ||
super(options); | ||
this.bodyHash = crypto.createHash('sha256'); | ||
this.hash = null; | ||
} | ||
|
||
updateHash(chunk) { | ||
this.bodyHash.update(chunk); | ||
} | ||
|
||
_transform(chunk, encoding, callback) { | ||
if (!chunk || !chunk.length) { | ||
return callback(); | ||
} | ||
|
||
if (typeof chunk === 'string') { | ||
chunk = Buffer.from(chunk, encoding); | ||
} | ||
|
||
this.updateHash(chunk); | ||
this.push(chunk); | ||
|
||
callback(); | ||
} | ||
|
||
_flush(done) { | ||
this.hash = this.bodyHash.digest('base64'); | ||
done(); | ||
} | ||
} | ||
|
||
module.exports = FileHashCalculatorStream; |