22
33import fs from 'node:fs' ;
44import path from 'node:path' ;
5+ import crypto from 'node:crypto' ;
56import { fileURLToPath } from 'url' ;
67
78import { request } from 'undici' ;
@@ -246,7 +247,7 @@ async function uploadDir(localDir, remoteDir){
246247 }
247248
248249 // const maxTasks = data.size <= 4 * Math.pow(1024, 3) ? 10 : 5;
249- const upload_status = await uploadChunks ( app , data , filePath , maxTasks ) ;
250+ const upload_status = await uploadChunks ( app , data , filePath ) ;
250251 delete data . uploaded ;
251252
252253 if ( upload_status . ok ) {
@@ -263,11 +264,33 @@ async function uploadDir(localDir, remoteDir){
263264 remoteFsList . push ( { server_filename : remoteFile , size : data . size } ) ;
264265
265266 console . log ( ':: Checking created file...' ) ;
267+ const rmeta = await app . getFileMeta ( [ upload_info . path ] ) ;
268+
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+ }
280+
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+
266287 const fsizeMatchMsg = data . size == rmeta . info [ 0 ] . size ? 'MATCH' : 'MISMATCH' ;
267288 const logFSize = data . size == rmeta . info [ 0 ] . size ? console . log : console . error ;
268289 logFSize ( ':: SIZE:' , rmeta . info [ 0 ] . size , `(${ fsizeMatchMsg } )` ) ;
269290
270- if ( data . size != rmeta . info [ 0 ] . size ) {
291+ // skip deleting tbtemp file...
292+
293+ if ( data . size != rmeta . info [ 0 ] . size || ! hashmatch ) {
271294 continue ;
272295 }
273296
0 commit comments