1212using Syncfusion . EJ2 . Linq ;
1313using Syncfusion . EJ2 . Notifications ;
1414using System . Dynamic ;
15+ using System . IO ;
1516using System . IO . Compression ;
17+ using System . IO . Hashing ;
1618using System . Security . Cryptography ;
1719using System . Text . Json ;
1820using System . Xml . Linq ;
@@ -382,6 +384,67 @@ public async Task<List<BsonFileManagerModel>> ShareFile(string dbName, string bs
382384 return await GetFilesPath ( dbName , args . Path ) ;
383385 }
384386
387+ private async Task itemDeleteAsync ( string dbName , string filterPath , string name , string parentId , string path )
388+ {
389+
390+ BsonFileManagerModel entry = await _db . getEntry ( dbName , filterPath , name ) ;
391+ if ( entry == null )
392+ throw new Exception ( $ "File { System . IO . Path . Combine ( filterPath , name ) } not found") ;
393+ if ( ! entry . IsFile )
394+ {
395+ List < BsonFileManagerModel > allChilds = await _db . getAllChildFilesInDirectory ( dbName , filterPath + name + "/" ) ;
396+ foreach ( BsonFileManagerModel child in allChilds )
397+ {
398+ await _db . deleteEntry ( dbName , child . Id ) ;
399+ if ( child . IsFile )
400+ {
401+ if ( child . isSplit )
402+ {
403+ foreach ( int id in child . ListMessageId )
404+ {
405+ if ( ! await _db . existItemByTelegramId ( dbName , id ) )
406+ await _ts . deleteFile ( dbName , id ) ;
407+ }
408+ }
409+ else
410+ {
411+ if ( ! await _db . existItemByTelegramId ( dbName , ( int ) child . MessageId ) )
412+ await _ts . deleteFile ( dbName , ( int ) child . MessageId ) ;
413+ }
414+ }
415+
416+ //if (item.IsFile)
417+ // await _db.subBytesToFolder(dbName, item.ParentId, item.Size);
418+ }
419+ await _db . deleteEntry ( dbName , entry . Id ) ;
420+ // await _db.subBytesToFolder(dbName, entry.ParentId, entry.Size);
421+ }
422+
423+ if ( entry . IsFile )
424+ {
425+ await _db . deleteEntry ( dbName , entry . Id ) ;
426+ if ( entry . isSplit )
427+ {
428+ foreach ( int id in entry . ListMessageId )
429+ {
430+ if ( ! await _db . existItemByTelegramId ( dbName , id ) )
431+ await _ts . deleteFile ( dbName , id ) ;
432+ }
433+ }
434+ else
435+ {
436+ if ( ! await _db . existItemByTelegramId ( dbName , ( int ) entry . MessageId ) )
437+ await _ts . deleteFile ( dbName , ( int ) entry . MessageId ) ;
438+ }
439+ }
440+
441+
442+
443+ await _db . subBytesToFolder ( dbName , entry . ParentId , entry . Size ) ;
444+
445+ await _db . checkAndSetDirectoryHasChild ( dbName , parentId ) ;
446+ }
447+
385448 public async Task oneItemDeleteAsync ( string dbName , Syncfusion . Blazor . FileManager . FileManagerDirectoryContent File )
386449 {
387450
@@ -1163,15 +1226,19 @@ public async Task UploadFileFromServer(string dbName, string currentPath, List<S
11631226 continue ;
11641227 }
11651228 idt . currentUpload ++ ;
1166- if ( file . Size == 0 || ( await _db . getFileByPath ( dbName , System . IO . Path . Combine ( currentPath , file . Name ) ) ) != null )
1229+ BsonFileManagerModel savedFile = await _db . getFileByPath ( dbName , System . IO . Path . Combine ( currentPath , file . Name ) ) ;
1230+ if ( file . Size == 0 || ( savedFile != null && savedFile . DateModified > file . DateModified ) )
11671231 {
11681232 if ( idt != null ) idt . AddOne ( file . Size ) ;
11691233 continue ;
11701234 } ;
11711235 model . Size = fileInfo . Length ;
1172- _logger . LogInformation ( $ "Calculating MD5 of file { currentFilePath } ") ;
1173- model . MD5Hash = GetMd5HashFromFile ( currentFilePath ) ;
1174- _logger . LogInformation ( $ "Calculated MD5 of file { currentFilePath } : { model . MD5Hash } ") ;
1236+ if ( GeneralConfigStatic . config . CheckHash )
1237+ {
1238+ _logger . LogInformation ( $ "Calculating HASH of file { currentFilePath } ") ;
1239+ model . XXHash = ComputeXXHash64 ( currentFilePath ) ;
1240+ _logger . LogInformation ( $ "Calculated HASH of file { currentFilePath } : { model . XXHash } ") ;
1241+ }
11751242 TL . Message m = null ;
11761243 long max = ( long ) MaxSize * ( long ) TelegramService . splitSizeGB ;
11771244 if ( fileInfo . Length > max )
@@ -1187,6 +1254,8 @@ public async Task UploadFileFromServer(string dbName, string currentPath, List<S
11871254 int attempts = 3 ;
11881255 int waitForNextAttempt = 1000 ;
11891256 UploadModel um = new UploadModel ( ) ;
1257+ um . path = currentFilePath ;
1258+ um . chatName = _ts . getChatName ( Convert . ToInt64 ( dbName ) ) ;
11901259 // add upload to task list
11911260 idt . addUpload ( um ) ;
11921261 um . thread = Thread . CurrentThread ;
@@ -1234,6 +1303,8 @@ public async Task UploadFileFromServer(string dbName, string currentPath, List<S
12341303 int attempts = 3 ;
12351304 int waitForNextAttempt = 60000 ;
12361305 UploadModel um = new UploadModel ( ) ;
1306+ um . path = currentFilePath ;
1307+ um . chatName = _ts . getChatName ( Convert . ToInt64 ( dbName ) ) ;
12371308 // add upload to task list
12381309 idt . addUpload ( um ) ;
12391310 while ( attempts != 0 || um . state == StateTask . Canceled )
@@ -1294,7 +1365,11 @@ public async Task UploadFileFromServer(string dbName, string currentPath, List<S
12941365
12951366 model . MessageId = m . ID ;
12961367 }
1297-
1368+ // delete previous file, when the new modified date file is newest
1369+ if ( savedFile != null )
1370+ {
1371+ await itemDeleteAsync ( dbName , savedFile . FilterPath , savedFile . Name , savedFile . ParentId , savedFile . FilePath ) ;
1372+ }
12981373 }
12991374 BsonFileManagerModel parent = await _db . getParentDirectoryByPath ( dbName , currentPath ) ;
13001375 model . Name = file . IsFile ? fileInfo . Name : file . Name ;
@@ -1700,6 +1775,26 @@ private string GetMd5HashFromFile(string filePath)
17001775 }
17011776 }
17021777
1778+ private string ComputeXXHash64 ( string filePath )
1779+ {
1780+ XxHashModel _xxHash = new XxHashModel ( ) ;
1781+ _xxHash . path = filePath ;
1782+ var hashAlgorithm = new XxHash64 ( ) ;
1783+ var buffer = new Span < byte > ( new byte [ 512 ] ) ;
1784+ using ( Stream entryStream = File . OpenRead ( filePath ) )
1785+ {
1786+ _tis . addToUploadList ( _xxHash ) ;
1787+ _xxHash . Init ( entryStream . Length , Path . GetFileName ( filePath ) ) ;
1788+ while ( entryStream . Read ( buffer ) > 0 )
1789+ {
1790+ hashAlgorithm . Append ( buffer ) ;
1791+ }
1792+ }
1793+
1794+ return BitConverter . ToString ( hashAlgorithm . GetHashAndReset ( ) ) . Replace ( "-" , string . Empty ) ;
1795+
1796+ }
1797+
17031798 private async Task < List < Stream > > splitFileAsync ( Stream s , int size )
17041799 {
17051800 List < Stream > ls = new List < Stream > ( ) ;
0 commit comments