New and moved files Upsert themselves in FileManager.FilePathCache

This commit is contained in:
Robert McRackan 2021-10-08 11:47:54 -04:00
parent 18cca53968
commit d0b78cc501
11 changed files with 149 additions and 144 deletions

View file

@ -8,7 +8,7 @@ using Dinah.Core.Collections.Generic;
namespace FileManager
{
public enum FileType { Unknown, Audio, AAXC, PDF }
public enum FileType { Unknown, Audio, AAXC, PDF, Zip }
public abstract class AudibleFileStorage : Enumeration<AudibleFileStorage>
{
@ -66,16 +66,9 @@ namespace FileManager
if (StorageDirectory == BooksDirectory)
{
//If user changed the BooksDirectory, reinitialize.
if (StorageDirectory != BookDirectoryFiles.RootDirectory)
{
lock (bookDirectoryFilesLocker)
{
if (StorageDirectory != BookDirectoryFiles.RootDirectory)
{
BookDirectoryFiles = new BackgroundFileSystem(StorageDirectory, "*.*", SearchOption.AllDirectories);
}
}
}
lock (bookDirectoryFilesLocker)
if (StorageDirectory != BookDirectoryFiles.RootDirectory)
BookDirectoryFiles = new BackgroundFileSystem(StorageDirectory, "*.*", SearchOption.AllDirectories);
firstOrNull = BookDirectoryFiles.FindFile(regex);
}
@ -87,10 +80,9 @@ namespace FileManager
.FirstOrDefault(s => regex.IsMatch(s));
}
if (firstOrNull is null)
return null;
if (firstOrNull is not null)
FilePathCache.Upsert(productId, FileType, firstOrNull);
FilePathCache.Upsert(productId, FileType, firstOrNull);
return firstOrNull;
}
#endregion

View file

@ -56,9 +56,15 @@ namespace FileManager
}
public static void Upsert(string id, FileType type, string path)
{
if (!File.Exists(path))
throw new FileNotFoundException("Cannot add path to cache. File not found");
{
if (!File.Exists(path))
{
// file not found can happen after rapid move
System.Threading.Thread.Sleep(100);
if (!File.Exists(path))
throw new FileNotFoundException($"Cannot add path to cache. File not found. Id={id} FileType={type}", path);
}
var entry = cache.SingleOrDefault(i => i.Id == id && i.FileType == type);