fix file cache manager issues
This commit is contained in:
		| @@ -1,4 +1,5 @@ | |||||||
| using System.IO; | using System; | ||||||
|  | using System.IO; | ||||||
| using System.Threading; | using System.Threading; | ||||||
| using MareSynchronos.FileCacheDB; | using MareSynchronos.FileCacheDB; | ||||||
| using MareSynchronos.Utils; | using MareSynchronos.Utils; | ||||||
| @@ -10,10 +11,9 @@ namespace MareSynchronos.Factories | |||||||
|         public FileCache Create(string file) |         public FileCache Create(string file) | ||||||
|         { |         { | ||||||
|             FileInfo fileInfo = new(file); |             FileInfo fileInfo = new(file); | ||||||
|             while (IsFileLocked(fileInfo)) |             if (IsFileLocked(fileInfo)) | ||||||
|             { |             { | ||||||
|                 Thread.Sleep(100); |                 throw new FileLoadException(); | ||||||
|                 Logger.Debug("File is locked, waiting for release: " + fileInfo.FullName); |  | ||||||
|             } |             } | ||||||
|             var sha1Hash = Crypto.GetFileHash(fileInfo.FullName); |             var sha1Hash = Crypto.GetFileHash(fileInfo.FullName); | ||||||
|             return new FileCache() |             return new FileCache() | ||||||
|   | |||||||
| @@ -44,25 +44,36 @@ namespace MareSynchronos.Managers | |||||||
|         { |         { | ||||||
|             var fi = new FileInfo(e.FullPath); |             var fi = new FileInfo(e.FullPath); | ||||||
|             using var db = new FileCacheContext(); |             using var db = new FileCacheContext(); | ||||||
|             if (fi.Extension.ToLower() is not ".mdl" or ".tex" or ".mtrl") |             var ext = fi.Extension.ToLower(); | ||||||
|  |             if (ext is ".mdl" or ".tex" or ".mtrl") | ||||||
|             { |             { | ||||||
|                 // this is most likely a folder |                 Logger.Debug("File created: " + e.FullPath); | ||||||
|                 Logger.Debug("Folder added: " + e.FullPath); |                 try | ||||||
|                 var newFiles = Directory.EnumerateFiles(e.FullPath, "*.*", SearchOption.AllDirectories) |  | ||||||
|                     .Where(f => f.EndsWith(".tex", StringComparison.OrdinalIgnoreCase) || |  | ||||||
|                                 f.EndsWith(".mdl", StringComparison.OrdinalIgnoreCase) || |  | ||||||
|                                 f.EndsWith(".mtrl", StringComparison.OrdinalIgnoreCase)).ToList(); |  | ||||||
|                 foreach (var file in newFiles) |  | ||||||
|                 { |                 { | ||||||
|                     Logger.Debug("Adding " + file); |                     var createdFileCache = _fileCacheFactory.Create(fi.FullName); | ||||||
|                     db.Add(_fileCacheFactory.Create(file)); |                     db.Add(createdFileCache); | ||||||
|                 } |                 } | ||||||
|  |                 catch (FileLoadException) | ||||||
|  |                 { | ||||||
|  |                     Logger.Debug("File was still being written to"); | ||||||
|  |                 } | ||||||
|  |  | ||||||
|             } |             } | ||||||
|             else |             else | ||||||
|             { |             { | ||||||
|                 Logger.Debug("File created: " + e.FullPath); |                 if (Directory.Exists(e.FullPath)) | ||||||
|                 var createdFileCache = _fileCacheFactory.Create(fi.FullName); |                 { | ||||||
|                 db.Add(createdFileCache); |                     Logger.Debug("Folder added: " + e.FullPath); | ||||||
|  |                     var newFiles = Directory.EnumerateFiles(e.FullPath, "*.*", SearchOption.AllDirectories) | ||||||
|  |                         .Where(f => f.EndsWith(".tex", StringComparison.OrdinalIgnoreCase) || | ||||||
|  |                                     f.EndsWith(".mdl", StringComparison.OrdinalIgnoreCase) || | ||||||
|  |                                     f.EndsWith(".mtrl", StringComparison.OrdinalIgnoreCase)).ToList(); | ||||||
|  |                     foreach (var file in newFiles) | ||||||
|  |                     { | ||||||
|  |                         Logger.Debug("Adding " + file); | ||||||
|  |                         db.Add(_fileCacheFactory.Create(file)); | ||||||
|  |                     } | ||||||
|  |                 } | ||||||
|             } |             } | ||||||
|  |  | ||||||
|             db.SaveChanges(); |             db.SaveChanges(); | ||||||
| @@ -72,20 +83,26 @@ namespace MareSynchronos.Managers | |||||||
|         { |         { | ||||||
|             var fi = new FileInfo(e.FullPath); |             var fi = new FileInfo(e.FullPath); | ||||||
|             using var db = new FileCacheContext(); |             using var db = new FileCacheContext(); | ||||||
|             if (fi.Extension.ToLower() is not ".mdl" or ".tex" or ".mtrl") |             var ext = fi.Extension.ToLower(); | ||||||
|             { |             if (ext is ".mdl" or ".tex" or ".mtrl") | ||||||
|                 // this is most likely a folder |  | ||||||
|                 var filesToRemove = db.FileCaches.Where(f => f.Filepath.StartsWith(e.FullPath.ToLower())).ToList(); |  | ||||||
|                 Logger.Debug($"Folder deleted: {e.FullPath}, removing {filesToRemove.Count} files"); |  | ||||||
|                 db.RemoveRange(filesToRemove); |  | ||||||
|             } |  | ||||||
|             else |  | ||||||
|             { |             { | ||||||
|                 Logger.Debug("File deleted: " + e.FullPath); |                 Logger.Debug("File deleted: " + e.FullPath); | ||||||
|                 var fileInDb = db.FileCaches.SingleOrDefault(f => f.Filepath == fi.FullName.ToLower()); |                 var fileInDb = db.FileCaches.SingleOrDefault(f => f.Filepath == fi.FullName.ToLower()); | ||||||
|                 if (fileInDb == null) return; |                 if (fileInDb == null) return; | ||||||
|                 db.Remove(fileInDb); |                 db.Remove(fileInDb); | ||||||
|  |  | ||||||
|             } |             } | ||||||
|  |             else | ||||||
|  |             { | ||||||
|  |                 if (fi.Extension == string.Empty) | ||||||
|  |                 { | ||||||
|  |                     // this is most likely a folder | ||||||
|  |                     var filesToRemove = db.FileCaches.Where(f => f.Filepath.StartsWith(e.FullPath.ToLower())).ToList(); | ||||||
|  |                     Logger.Debug($"Folder deleted: {e.FullPath}, removing {filesToRemove.Count} files"); | ||||||
|  |                     db.RemoveRange(filesToRemove); | ||||||
|  |                 } | ||||||
|  |             } | ||||||
|  |  | ||||||
|             db.SaveChanges(); |             db.SaveChanges(); | ||||||
|         } |         } | ||||||
|  |  | ||||||
| @@ -231,25 +248,31 @@ namespace MareSynchronos.Managers | |||||||
|             _penumbraDirWatcher = new FileSystemWatcher(_ipcManager.PenumbraModDirectory()!) |             _penumbraDirWatcher = new FileSystemWatcher(_ipcManager.PenumbraModDirectory()!) | ||||||
|             { |             { | ||||||
|                 EnableRaisingEvents = true, |                 EnableRaisingEvents = true, | ||||||
|                 Filter = "*.*", |  | ||||||
|                 IncludeSubdirectories = true, |                 IncludeSubdirectories = true, | ||||||
|  |                 InternalBufferSize = 65536 | ||||||
|             }; |             }; | ||||||
|             _penumbraDirWatcher.NotifyFilter = NotifyFilters.FileName | NotifyFilters.DirectoryName | NotifyFilters.Size; |             _penumbraDirWatcher.NotifyFilter = NotifyFilters.FileName | NotifyFilters.Size; | ||||||
|             _penumbraDirWatcher.Created += OnCreated; |             _penumbraDirWatcher.Created += OnCreated; | ||||||
|             _penumbraDirWatcher.Deleted += OnDeleted; |             _penumbraDirWatcher.Deleted += OnDeleted; | ||||||
|             _penumbraDirWatcher.Changed += OnModified; |             _penumbraDirWatcher.Changed += OnModified; | ||||||
|  |             _penumbraDirWatcher.Filters.Add("*.mtrl"); | ||||||
|  |             _penumbraDirWatcher.Filters.Add("*.mdl"); | ||||||
|  |             _penumbraDirWatcher.Filters.Add("*.tex"); | ||||||
|             _penumbraDirWatcher.Error += (sender, args) => PluginLog.Error(args.GetException(), "Error in Penumbra Dir Watcher"); |             _penumbraDirWatcher.Error += (sender, args) => PluginLog.Error(args.GetException(), "Error in Penumbra Dir Watcher"); | ||||||
|  |  | ||||||
|             _cacheDirWatcher = new FileSystemWatcher(_pluginConfiguration.CacheFolder) |             _cacheDirWatcher = new FileSystemWatcher(_pluginConfiguration.CacheFolder) | ||||||
|             { |             { | ||||||
|                 EnableRaisingEvents = true, |                 EnableRaisingEvents = true, | ||||||
|                 Filter = "*.*", |  | ||||||
|                 IncludeSubdirectories = true, |                 IncludeSubdirectories = true, | ||||||
|  |                 InternalBufferSize = 65536 | ||||||
|             }; |             }; | ||||||
|             _cacheDirWatcher.NotifyFilter = NotifyFilters.FileName | NotifyFilters.DirectoryName | NotifyFilters.Size; |             _cacheDirWatcher.NotifyFilter = NotifyFilters.FileName | NotifyFilters.Size; | ||||||
|             _cacheDirWatcher.Created += OnCreated; |             _cacheDirWatcher.Created += OnCreated; | ||||||
|             _cacheDirWatcher.Deleted += OnDeleted; |             _cacheDirWatcher.Deleted += OnDeleted; | ||||||
|             _cacheDirWatcher.Changed += OnModified; |             _cacheDirWatcher.Changed += OnModified; | ||||||
|  |             _cacheDirWatcher.Filters.Add("*.mtrl"); | ||||||
|  |             _cacheDirWatcher.Filters.Add("*.mdl"); | ||||||
|  |             _cacheDirWatcher.Filters.Add("*.tex"); | ||||||
|             _cacheDirWatcher.Error += |             _cacheDirWatcher.Error += | ||||||
|                 (sender, args) => PluginLog.Error(args.GetException(), "Error in Cache Dir Watcher"); |                 (sender, args) => PluginLog.Error(args.GetException(), "Error in Cache Dir Watcher"); | ||||||
|         } |         } | ||||||
|   | |||||||
| @@ -133,6 +133,17 @@ namespace MareSynchronos.WebAPI | |||||||
|                 downloadedHashes.Add(hash); |                 downloadedHashes.Add(hash); | ||||||
|             } |             } | ||||||
|  |  | ||||||
|  |             bool allFilesInDb = false; | ||||||
|  |             while (!allFilesInDb) | ||||||
|  |             { | ||||||
|  |                 await using (var db = new FileCacheContext()) | ||||||
|  |                 { | ||||||
|  |                     allFilesInDb = downloadedHashes.All(h => db.FileCaches.Any(f => f.Hash == h)); | ||||||
|  |                 } | ||||||
|  |  | ||||||
|  |                 await Task.Delay(250); | ||||||
|  |             } | ||||||
|  |  | ||||||
|             CurrentDownloads.Clear(); |             CurrentDownloads.Clear(); | ||||||
|         } |         } | ||||||
|  |  | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 Stanley Dimant
					Stanley Dimant