* Check that `hashedStream.completedHash` matches header `stream.contentMD5` * and delete old data or remove 'hashed' listeners, if applicable. * @param {object} stream - stream containing the data * @param {object} hashedStream - instance of MD5Sum * @param {object} dataRetrievalInfo - object c
(stream, hashedStream, dataRetrievalInfo, log, cb)
| 17 | * error, dataRetrievalInfo, and completedHash (if any) |
| 18 | */ |
| 19 | function checkHashMatchMD5(stream, hashedStream, dataRetrievalInfo, log, cb) { |
| 20 | const contentMD5 = stream.contentMD5; |
| 21 | const completedHash = hashedStream.completedHash; |
| 22 | if (contentMD5 && completedHash && contentMD5 !== completedHash) { |
| 23 | log.debug('contentMD5 and completedHash do not match, deleting data', { |
| 24 | method: 'storeObject::dataStore', |
| 25 | completedHash, |
| 26 | contentMD5, |
| 27 | }); |
| 28 | const dataToDelete = []; |
| 29 | dataToDelete.push(dataRetrievalInfo); |
| 30 | return data.batchDelete(dataToDelete, null, null, log, err => { |
| 31 | if (err) { |
| 32 | // failure of batch delete is only logged, client gets the |
| 33 | // error code about the md mismatch |
| 34 | log.error('error deleting old data', { error: err }); |
| 35 | } |
| 36 | return cb(errors.BadDigest); |
| 37 | }); |
| 38 | } |
| 39 | return cb(null, dataRetrievalInfo, completedHash); |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Stores object and responds back with key and storage type |