@@ -8,6 +8,7 @@ const dcmjs = require('dcmjs');
8
8
const Axios = require ( 'axios' ) ;
9
9
const http = require ( 'http' ) ;
10
10
const fs = require ( 'fs' ) ;
11
+ const md5 = require ( 'md5' ) ;
11
12
12
13
const config = require ( '../config/index' ) ;
13
14
const viewsjs = require ( '../config/views' ) ;
@@ -647,24 +648,35 @@ async function couchdb(fastify, options) {
647
648
fastify . decorate ( 'saveBuffer' , ( arrayBuffer , dicomDB ) => {
648
649
// eslint-disable-next-line no-param-reassign
649
650
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 ) ;
650
654
const dicomData = dcmjs . data . DicomMessage . readFile ( arrayBuffer , { } ) ;
651
655
const couchDoc = {
652
656
_id : dicomData . dict [ '00080018' ] . Value [ 0 ] ,
653
657
dataset : dicomData . dict ,
658
+ md5hash : incomingMd5 ,
654
659
} ;
655
660
return new Promise ( ( resolve , reject ) =>
656
661
dicomDB . get ( couchDoc . _id , ( error , existing ) => {
657
662
if ( ! error ) {
658
663
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
+ }
659
673
fastify . log . info ( `Updating document for dicom ${ couchDoc . _id } ` ) ;
660
674
}
661
675
662
676
dicomDB . insert ( couchDoc , ( err , data ) => {
663
677
if ( err ) {
664
678
reject ( err ) ;
665
679
}
666
- // TODO: Check if this needs to be Buffer or not.
667
- const body = Buffer . from ( arrayBuffer ) ;
668
680
669
681
dicomDB . attachment . insert (
670
682
couchDoc . _id ,
0 commit comments