Is it possible to get the md5 hash value of a file being downloaded from a server?

社会主义新天地 提交于 2019-12-04 02:33:21

问题


Is it possible to get the md5 hash value of a file using mongo grid fs being downloaded from a server before storing the file to mongo databse. Now using the code below it stores direcly the data to mongo with the md5 , filename , contenttype etc because I used pipe. Now I want to check the md5 hash of the file first before storing it to the database , I want to get the md5 value of a file first cause i wanna use it to avoid duplicates. Now I did try to log the fileStorage. but i dont think it contains the md5. Any idea? thank you. btw I am using mongo grid fs

filestorage data

GridWriteStream{ 
   _writableState:WritableState{ 
      objectMode:false,
      highWaterMark:16384,
      finalCalled:false,
      needDrain:false,
      ending:false,
      ended:false,
      finished:false,
      destroyed:false,
      decodeStrings:true,
      defaultEncoding:'utf8',
      length:0,
      writing:false,
      corked:0,
      sync:true,
      bufferProcessing:false,
      onwrite:[ 
         Function:         bound onwrite
      ],
      writecb:null,
      writelen:0,
      bufferedRequest:null,
      lastBufferedRequest:null,
      pendingcb:0,
      prefinished:false,
      errorEmitted:false,
      emitClose:true,
      autoDestroy:false,
      bufferedRequestCount:0,
      corkedRequestsFree:{ 
         next:null,
         entry:null,
         finish:[ 
            Function:            bound onCorkedFinish
         ]
      }
   },
   writable:true,
   _events:[ 
      Object:      null prototype
   ]{ 

   },
   _eventsCount:0,
   _maxListeners:undefined,
   _opened:false,
   _opening:true,
   _writable:true,
   _closing:false,
   _destroyed:false,
   _errorEmitted:false,
   _grid:Grid{ 
      db:Db{ 
         _events:[ 
            Object:            null prototype
         ]{ 

         },
         _eventsCount:0,
         _maxListeners:undefined,
         s:[ 
            Object
         ],
         serverConfig:[ 
            Getter
         ],
         bufferMaxEntries:[ 
            Getter
         ],
         databaseName:[ 
            Getter
         ]
      },
      mongo:{ 
[ 
            Function
         ]         MongoError:[ 
            Function
         ],
         Admin:[ 
            Function
         ],
         MongoClient:[ 
            Function
         ],
         Db:[ 
            Function
         ],
         Collection:[ 
            Function
         ],
         Server:[ 
            Function
         ],
         ReplSet:[ 
            Function
         ],
         Mongos:[ 
            Function
         ],
         ReadPreference:[ 
            Function
         ],
         GridStore:[ 
            Function
         ],
         Chunk:[ 
            Function
         ],
         Logger:[ 
            Function
         ],
         Cursor:[ 
            Function
         ],
         GridFSBucket:[ 
            Function
         ],
         CoreServer:[ 
            Function
         ],
         CoreConnection:[ 
            Function
         ],
         Binary:[ 
            Function
         ],
         Code:[ 
            Function
         ],
         Map:[ 
            Function
         ],
         DBRef:[ 
            Function
         ],
         Double:[ 
            Function
         ],
         Int32:[ 
            Function
         ],
         Long:[ 
            Function
         ],
         MinKey:[ 
            Function
         ],
         MaxKey:[ 
            Function
         ],
         ObjectID:[ 
            Function
         ],
         ObjectId:[ 
            Function
         ],
         Symbol:[ 
            Function
         ],
         Timestamp:[ 
            Function
         ],
         Decimal128:[ 
            Function
         ],
         connect:[ 
            Circular
         ],
         instrument:[ 
            Function
         ]
      },
      curCol:'fs'
   },
   options:{ 
      filename:'f9bd6032ceae47a8b8b9be07a5dc839e.jpg',
      data:'1'
   },
   name:'f9bd6032ceae47a8b8b9be07a5dc839e.jpg',
   id:5d9eef3dde7cc73550be84d8,
   mode:'w',
   _store:GridStore{ 
      db:Db{ 
         _events:[ 
            Object:            null prototype
         ]{ 

         },
         _eventsCount:0,
         _maxListeners:undefined,
         s:[ 
            Object
         ],
         serverConfig:[ 
            Getter
         ],
         bufferMaxEntries:[ 
            Getter
         ],
         databaseName:[ 
            Getter
         ]
      },
      referenceBy:1,
      fileId:5d9eef3dde7cc73550be84d8,
      filename:'f9bd6032ceae47a8b8b9be07a5dc839e.jpg',
      mode:'w',
      options:{ 
         filename:'f9bd6032ceae47a8b8b9be07a5dc839e.jpg',
         data:'1'
      },
      isOpen:false,
      root:'fs',
      position:0,
      readPreference:'primary',
      writeConcern:{ 
         w:1
      },
      internalChunkSize:261120,
      promiseLibrary:[ 
         Function:         Promise
      ],
      chunkSize:[ 
         Getter/Setter
      ],
      md5:[ 
         Getter
      ],
      chunkNumber:[ 
         Getter
      ]
   },
   _delayedWrite:null,
   _delayedFlush:null,
   _delayedClose:null
}

Code that downloads the file

var download = function (url, dest, filename callback) {

   const file = filename
    const fileStorage = gfs.createWriteStream({ filename: file});

    // request.get(url)
                    //     .on('error', function (err) { console.log(err) })
                    //     .pipe(fileStorage)
                    //     .on('close', callback);

};

final_list.forEach(function (str) {
    var filename = str.split('/').pop();

    console.log('Downloading ' + filename);

    download(str, filename, function () { console.log('Finished Downloading' + "" + filename) });
});

来源:https://stackoverflow.com/questions/58319517/is-it-possible-to-get-the-md5-hash-value-of-a-file-being-downloaded-from-a-serve

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!