Skip to content

Commit 89045aa

Browse files
authored
Merge pull request #45 from dcmjs-org/feature/md5hash
adds md5hast to documents and checks before updating
2 parents 0742cda + ae2fb4f commit 89045aa

File tree

3 files changed

+35
-2
lines changed

3 files changed

+35
-2
lines changed

package-lock.json

Lines changed: 20 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"fs-extra": "^8.1.0",
1919
"http": "0.0.0",
2020
"keycloak-backend": "^2.0.0",
21+
"md5": "^2.2.1",
2122
"nano": "^8.1.0",
2223
"p-queue": "^6.2.1",
2324
"split2": "^3.1.1",

plugins/CouchDB.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const dcmjs = require('dcmjs');
88
const Axios = require('axios');
99
const http = require('http');
1010
const fs = require('fs');
11+
const md5 = require('md5');
1112

1213
const config = require('../config/index');
1314
const viewsjs = require('../config/views');
@@ -647,24 +648,35 @@ async function couchdb(fastify, options) {
647648
fastify.decorate('saveBuffer', (arrayBuffer, dicomDB) => {
648649
// eslint-disable-next-line no-param-reassign
649650
if (dicomDB === undefined) dicomDB = fastify.couch.db.use(config.db);
651+
// TODO: Check if this needs to be Buffer or not.
652+
const body = Buffer.from(arrayBuffer);
653+
const incomingMd5 = md5(body);
650654
const dicomData = dcmjs.data.DicomMessage.readFile(arrayBuffer, {});
651655
const couchDoc = {
652656
_id: dicomData.dict['00080018'].Value[0],
653657
dataset: dicomData.dict,
658+
md5hash: incomingMd5,
654659
};
655660
return new Promise((resolve, reject) =>
656661
dicomDB.get(couchDoc._id, (error, existing) => {
657662
if (!error) {
658663
couchDoc._rev = existing._rev;
664+
// old documents won't have md5
665+
if (existing.md5hash) {
666+
// get the md5 of the buffer
667+
if (existing.md5hash === incomingMd5) {
668+
fastify.log.info(`${couchDoc._id} is already in the system with same hash`);
669+
resolve('File already in system');
670+
return;
671+
}
672+
}
659673
fastify.log.info(`Updating document for dicom ${couchDoc._id}`);
660674
}
661675

662676
dicomDB.insert(couchDoc, (err, data) => {
663677
if (err) {
664678
reject(err);
665679
}
666-
// TODO: Check if this needs to be Buffer or not.
667-
const body = Buffer.from(arrayBuffer);
668680

669681
dicomDB.attachment.insert(
670682
couchDoc._id,

0 commit comments

Comments
 (0)