@@ -265,35 +265,32 @@ async function uploadDir(localDir, remoteDir){
265265
266266 console . log ( ':: Checking created file...' ) ;
267267 const rmeta = await app . getFileMeta ( [ upload_info . path ] ) ;
268+ const fmeta = rmeta . info [ 0 ] ;
268269
269- // hash check
270-
271- let hashmatch ;
272- if ( data . hash . chunks . length > 1 ) {
273- const hashStr = JSON . stringify ( data . hash . chunks ) ;
274- const tbServerHash = crypto . createHash ( 'md5' ) . update ( hashStr ) . digest ( 'hex' ) ;
275- hashmatch = tbServerHash == upload_info . md5 ;
276- }
277- else {
278- hashmatch = data . hash . file == upload_info . md5 ;
279- }
270+ // build weak etag
271+ const chunksJSON = JSON . stringify ( data . hash . chunks ) ;
272+ const chunksETAG = crypto . createHash ( 'md5' ) . update ( chunksJSON ) . digest ( 'hex' ) ;
273+ const weakEtag = data . hash . chunks . length > 1 ? chunksETAG : data . hash . file ;
280274
281- const hashMatchMsg = hashmatch ? 'MATCH' : 'MISMATCH' ;
282- const logHashMatch = hashmatch ? console . log : console . error ;
283- logHashMatch ( ':: HASH:' , upload_info . md5 , `(${ hashMatchMsg } )` ) ;
284-
285- // file size check
286-
287- const fsizeMatchMsg = data . size == rmeta . info [ 0 ] . size ? 'MATCH' : 'MISMATCH' ;
288- const logFSize = data . size == rmeta . info [ 0 ] . size ? console . log : console . error ;
289- logFSize ( ':: SIZE:' , rmeta . info [ 0 ] . size , `(${ fsizeMatchMsg } )` ) ;
275+ // hash check
276+ const hashMatch = weakEtag === fmeta . md5 ;
277+ const hashMatchMsg = hashMatch ? 'MATCH' : 'MISMATCH' ;
278+ const logHashMatch = hashMatch ? console . log : console . error ;
279+ logHashMatch ( ':: HASH:' , fmeta . md5 , `(${ hashMatchMsg } )` ) ;
290280
291- // skip deleting tbtemp file...
281+ // size check
282+ const sizeMatch = data . size === fmeta . size ;
283+ const sizeMatchMsg = sizeMatch ? 'MATCH' : 'MISMATCH' ;
284+ const logSizeMatch = sizeMatch ? console . log : console . error ;
285+ logSizeMatch ( ':: SIZE:' , fmeta . size , `(${ sizeMatchMsg } )` ) ;
292286
293- if ( data . size != rmeta . info [ 0 ] . size || ! hashmatch ) {
287+ // skip deleting tbtemp file if mismatch...
288+ if ( ! sizeMatch || ! hashMatch ) {
289+ console . error ( ':: File is BAD!' ) ;
294290 continue ;
295291 }
296292
293+ // remove tbtemp file if everything is ok
297294 console . log ( ':: File is OK!' ) ;
298295 removeTbTemp ( tbtempfile ) ;
299296 continue ;
0 commit comments