From 9a78d34571e5bfb9a0e3c7ad07df35fb1fc7b5ae Mon Sep 17 00:00:00 2001 From: Jason Dove <1695733+jasongdove@users.noreply.github.com> Date: Mon, 24 Nov 2025 10:15:51 -0600 Subject: [PATCH] remove folderexists --- ErsatzTV.Core.Tests/Fakes/FakeFileEntry.cs | 8 -- ErsatzTV.Core.Tests/Fakes/FakeFolderEntry.cs | 3 - .../Fakes/FakeLocalFileSystem.cs | 89 ------------------- .../Interfaces/Metadata/ILocalFileSystem.cs | 1 - ErsatzTV.Core/Metadata/LocalFileSystem.cs | 2 - .../Data/Repositories/LibraryRepository.cs | 46 ++++------ .../Core/Fakes/FakeLocalFileSystem.cs | 1 - .../Services/RunOnce/CacheCleanerService.cs | 6 +- 8 files changed, 22 insertions(+), 134 deletions(-) delete mode 100644 ErsatzTV.Core.Tests/Fakes/FakeFileEntry.cs delete mode 100644 ErsatzTV.Core.Tests/Fakes/FakeFolderEntry.cs delete mode 100644 ErsatzTV.Core.Tests/Fakes/FakeLocalFileSystem.cs diff --git a/ErsatzTV.Core.Tests/Fakes/FakeFileEntry.cs b/ErsatzTV.Core.Tests/Fakes/FakeFileEntry.cs deleted file mode 100644 index bb4427c5e..000000000 --- a/ErsatzTV.Core.Tests/Fakes/FakeFileEntry.cs +++ /dev/null @@ -1,8 +0,0 @@ -namespace ErsatzTV.Core.Tests.Fakes; - -public record FakeFileEntry(string Path) -{ - public DateTime LastWriteTime { get; set; } = SystemTime.MinValueUtc; - - public string Contents { get; set; } -} diff --git a/ErsatzTV.Core.Tests/Fakes/FakeFolderEntry.cs b/ErsatzTV.Core.Tests/Fakes/FakeFolderEntry.cs deleted file mode 100644 index 6b3efc223..000000000 --- a/ErsatzTV.Core.Tests/Fakes/FakeFolderEntry.cs +++ /dev/null @@ -1,3 +0,0 @@ -namespace ErsatzTV.Core.Tests.Fakes; - -public record FakeFolderEntry(string Path); diff --git a/ErsatzTV.Core.Tests/Fakes/FakeLocalFileSystem.cs b/ErsatzTV.Core.Tests/Fakes/FakeLocalFileSystem.cs deleted file mode 100644 index 3fb148eef..000000000 --- a/ErsatzTV.Core.Tests/Fakes/FakeLocalFileSystem.cs +++ /dev/null @@ -1,89 +0,0 @@ -using ErsatzTV.Core.Interfaces.Metadata; - -namespace ErsatzTV.Core.Tests.Fakes; - -public class FakeLocalFileSystem : ILocalFileSystem -{ - private readonly List _files; - private readonly List _folders; - - public FakeLocalFileSystem(List files) : this(files, new List()) - { - } - - public FakeLocalFileSystem(List files, List folders) - { - _files = files; - - var allFolders = new List(folders.Map(f => f.Path)); - foreach (FakeFileEntry file in _files) - { - List moreFolders = - Split(new DirectoryInfo(Path.GetDirectoryName(file.Path) ?? string.Empty)); - allFolders.AddRange(moreFolders.Map(i => i.FullName)); - } - - _folders = allFolders.Distinct().Map(f => new FakeFolderEntry(f)).ToList(); - } - - public Unit EnsureFolderExists(string folder) => Unit.Default; - - public DateTime GetLastWriteTime(string path) => - Optional(_files.SingleOrDefault(f => f.Path == path)) - .Map(f => f.LastWriteTime) - .IfNone(SystemTime.MinValueUtc); - - public IEnumerable ListSubdirectories(string folder) => - _folders.Map(f => f.Path).Filter(f => f.StartsWith(folder) && Directory.GetParent(f)?.FullName == folder); - - public IEnumerable ListFiles(string folder) => - _files.Map(f => f.Path).Filter(f => Path.GetDirectoryName(f) == folder); - - // TODO: this isn't accurate, need to use search pattern - public IEnumerable ListFiles(string folder, string searchPattern) => - _files.Map(f => f.Path).Filter(f => Path.GetDirectoryName(f) == folder); - - public IEnumerable ListFiles(string folder, params string[] searchPatterns) => - _files.Map(f => f.Path).Filter(f => Path.GetDirectoryName(f) == folder); - - public bool FileExists(string path) => _files.Any(f => f.Path == path); - public bool FolderExists(string folder) => false; - - public Task> CopyFile(string source, string destination) => - Task.FromResult(Right(Unit.Default)); - - public Unit EmptyFolder(string folder) => Unit.Default; - - public async Task ReadAllLines(string path) => await _files - .Filter(f => f.Path == path) - .HeadOrNone() - .Select(f => f.Contents) - .IfNoneAsync(string.Empty) - .Map(s => s.Split(Environment.NewLine)); - - public Task GetHash(string path) => throw new NotSupportedException(); - - public string GetCustomOrDefaultFile(string folder, string file) - { - string path = Path.Combine(folder, file); - return FileExists(path) ? path : Path.Combine(folder, $"_{file}"); - } - - private static List Split(DirectoryInfo path) - { - var result = new List(); - if (path == null || string.IsNullOrWhiteSpace(path.FullName)) - { - return result; - } - - if (path.Parent != null) - { - result.AddRange(Split(path.Parent)); - } - - result.Add(path); - - return result; - } -} diff --git a/ErsatzTV.Core/Interfaces/Metadata/ILocalFileSystem.cs b/ErsatzTV.Core/Interfaces/Metadata/ILocalFileSystem.cs index 434770c25..82a2c1821 100644 --- a/ErsatzTV.Core/Interfaces/Metadata/ILocalFileSystem.cs +++ b/ErsatzTV.Core/Interfaces/Metadata/ILocalFileSystem.cs @@ -8,7 +8,6 @@ public interface ILocalFileSystem IEnumerable ListFiles(string folder); IEnumerable ListFiles(string folder, string searchPattern); IEnumerable ListFiles(string folder, params string[] searchPatterns); - bool FolderExists(string folder); Task> CopyFile(string source, string destination); Unit EmptyFolder(string folder); Task ReadAllLines(string path); diff --git a/ErsatzTV.Core/Metadata/LocalFileSystem.cs b/ErsatzTV.Core/Metadata/LocalFileSystem.cs index 6a9826434..040da855c 100644 --- a/ErsatzTV.Core/Metadata/LocalFileSystem.cs +++ b/ErsatzTV.Core/Metadata/LocalFileSystem.cs @@ -133,8 +133,6 @@ public class LocalFileSystem(IFileSystem fileSystem, IClient client, ILogger(); } - public bool FolderExists(string folder) => Directory.Exists(folder); - public async Task> CopyFile(string source, string destination) { try diff --git a/ErsatzTV.Infrastructure/Data/Repositories/LibraryRepository.cs b/ErsatzTV.Infrastructure/Data/Repositories/LibraryRepository.cs index 95954776b..cdf5aa9ed 100644 --- a/ErsatzTV.Infrastructure/Data/Repositories/LibraryRepository.cs +++ b/ErsatzTV.Infrastructure/Data/Repositories/LibraryRepository.cs @@ -1,26 +1,18 @@ -using Dapper; +using System.IO.Abstractions; +using Dapper; using ErsatzTV.Core.Domain; -using ErsatzTV.Core.Interfaces.Metadata; using ErsatzTV.Core.Interfaces.Repositories; using ErsatzTV.Infrastructure.Extensions; using Microsoft.EntityFrameworkCore; namespace ErsatzTV.Infrastructure.Data.Repositories; -public class LibraryRepository : ILibraryRepository +public class LibraryRepository(IFileSystem fileSystem, IDbContextFactory dbContextFactory) + : ILibraryRepository { - private readonly IDbContextFactory _dbContextFactory; - private readonly ILocalFileSystem _localFileSystem; - - public LibraryRepository(ILocalFileSystem localFileSystem, IDbContextFactory dbContextFactory) - { - _localFileSystem = localFileSystem; - _dbContextFactory = dbContextFactory; - } - public async Task Add(LibraryPath libraryPath) { - await using TvContext dbContext = await _dbContextFactory.CreateDbContextAsync(); + await using TvContext dbContext = await dbContextFactory.CreateDbContextAsync(); await dbContext.LibraryPaths.AddAsync(libraryPath); await dbContext.SaveChangesAsync(); return libraryPath; @@ -28,7 +20,7 @@ public class LibraryRepository : ILibraryRepository public async Task> GetLibrary(int libraryId) { - await using TvContext dbContext = await _dbContextFactory.CreateDbContextAsync(); + await using TvContext dbContext = await dbContextFactory.CreateDbContextAsync(); return await dbContext.Libraries .Include(l => l.Paths) .ThenInclude(p => p.LibraryFolders) @@ -40,7 +32,7 @@ public class LibraryRepository : ILibraryRepository public async Task> GetLocal(int libraryId) { - await using TvContext dbContext = await _dbContextFactory.CreateDbContextAsync(); + await using TvContext dbContext = await dbContextFactory.CreateDbContextAsync(); return await dbContext.LocalLibraries .OrderBy(l => l.Id) .SingleOrDefaultAsync(l => l.Id == libraryId) @@ -49,7 +41,7 @@ public class LibraryRepository : ILibraryRepository public async Task> GetAll() { - await using TvContext dbContext = await _dbContextFactory.CreateDbContextAsync(); + await using TvContext dbContext = await dbContextFactory.CreateDbContextAsync(); return await dbContext.Libraries .AsNoTracking() .Include(l => l.MediaSource) @@ -59,7 +51,7 @@ public class LibraryRepository : ILibraryRepository public async Task UpdateLastScan(Library library) { - await using TvContext dbContext = await _dbContextFactory.CreateDbContextAsync(); + await using TvContext dbContext = await dbContextFactory.CreateDbContextAsync(); return await dbContext.Connection.ExecuteAsync( "UPDATE Library SET LastScan = @LastScan WHERE Id = @Id", new { library.LastScan, library.Id }).ToUnit(); @@ -67,7 +59,7 @@ public class LibraryRepository : ILibraryRepository public async Task UpdateLastScan(LibraryPath libraryPath) { - await using TvContext dbContext = await _dbContextFactory.CreateDbContextAsync(); + await using TvContext dbContext = await dbContextFactory.CreateDbContextAsync(); return await dbContext.Connection.ExecuteAsync( "UPDATE LibraryPath SET LastScan = @LastScan WHERE Id = @Id", new { libraryPath.LastScan, libraryPath.Id }).ToUnit(); @@ -75,7 +67,7 @@ public class LibraryRepository : ILibraryRepository public async Task> GetLocalPaths(int libraryId) { - await using TvContext dbContext = await _dbContextFactory.CreateDbContextAsync(); + await using TvContext dbContext = await dbContextFactory.CreateDbContextAsync(); return await dbContext.LocalLibraries .Include(l => l.Paths) .OrderBy(l => l.Id) @@ -86,7 +78,7 @@ public class LibraryRepository : ILibraryRepository public async Task CountMediaItemsByPath(int libraryPathId) { - await using TvContext dbContext = await _dbContextFactory.CreateDbContextAsync(); + await using TvContext dbContext = await dbContextFactory.CreateDbContextAsync(); return await dbContext.Connection.QuerySingleAsync( @"SELECT COUNT(*) FROM MediaItem WHERE LibraryPathId = @LibraryPathId", new { LibraryPathId = libraryPathId }); @@ -98,7 +90,7 @@ public class LibraryRepository : ILibraryRepository string path, string etag) { - await using TvContext dbContext = await _dbContextFactory.CreateDbContextAsync(); + await using TvContext dbContext = await dbContextFactory.CreateDbContextAsync(); foreach (LibraryFolder folder in knownFolder) { @@ -123,10 +115,10 @@ public class LibraryRepository : ILibraryRepository public async Task CleanEtagsForLibraryPath(LibraryPath libraryPath) { - await using TvContext dbContext = await _dbContextFactory.CreateDbContextAsync(); + await using TvContext dbContext = await dbContextFactory.CreateDbContextAsync(); IOrderedEnumerable orderedFolders = libraryPath.LibraryFolders - .Where(f => !_localFileSystem.FolderExists(f.Path)) + .Where(f => !fileSystem.Directory.Exists(f.Path)) .OrderByDescending(lp => lp.Path.Length); foreach (LibraryFolder folder in orderedFolders) @@ -152,7 +144,7 @@ public class LibraryRepository : ILibraryRepository return Option.None; } - await using TvContext dbContext = await _dbContextFactory.CreateDbContextAsync(cancellationToken); + await using TvContext dbContext = await dbContextFactory.CreateDbContextAsync(cancellationToken); return await dbContext.LibraryFolders .AsNoTracking() @@ -166,7 +158,7 @@ public class LibraryRepository : ILibraryRepository Option maybeParentFolder, string folder) { - await using TvContext dbContext = await _dbContextFactory.CreateDbContextAsync(); + await using TvContext dbContext = await dbContextFactory.CreateDbContextAsync(); // load from db or create new folder LibraryFolder knownFolder = await libraryPath.LibraryFolders @@ -199,7 +191,7 @@ public class LibraryRepository : ILibraryRepository public async Task UpdateLibraryFolderId(MediaFile mediaFile, int libraryFolderId) { - await using TvContext dbContext = await _dbContextFactory.CreateDbContextAsync(); + await using TvContext dbContext = await dbContextFactory.CreateDbContextAsync(); mediaFile.LibraryFolderId = libraryFolderId; await dbContext.Connection.ExecuteAsync( "UPDATE MediaFile SET LibraryFolderId = @LibraryFolderId WHERE Id = @Id", @@ -208,7 +200,7 @@ public class LibraryRepository : ILibraryRepository public async Task UpdatePath(LibraryPath libraryPath, string normalizedLibraryPath) { - await using TvContext dbContext = await _dbContextFactory.CreateDbContextAsync(); + await using TvContext dbContext = await dbContextFactory.CreateDbContextAsync(); libraryPath.Path = normalizedLibraryPath; await dbContext.Connection.ExecuteAsync( "UPDATE LibraryPath SET Path = @Path WHERE Id = @Id", diff --git a/ErsatzTV.Scanner.Tests/Core/Fakes/FakeLocalFileSystem.cs b/ErsatzTV.Scanner.Tests/Core/Fakes/FakeLocalFileSystem.cs index 605d944e6..281cc5a3a 100644 --- a/ErsatzTV.Scanner.Tests/Core/Fakes/FakeLocalFileSystem.cs +++ b/ErsatzTV.Scanner.Tests/Core/Fakes/FakeLocalFileSystem.cs @@ -48,7 +48,6 @@ public class FakeLocalFileSystem : ILocalFileSystem _files.Map(f => f.Path).Filter(f => Path.GetDirectoryName(f) == folder); public bool FileExists(string path) => _files.Any(f => f.Path == path); - public bool FolderExists(string folder) => false; public Task> CopyFile(string source, string destination) => Task.FromResult(Right(Unit.Default)); diff --git a/ErsatzTV/Services/RunOnce/CacheCleanerService.cs b/ErsatzTV/Services/RunOnce/CacheCleanerService.cs index a873e7ec6..3bb768cdf 100644 --- a/ErsatzTV/Services/RunOnce/CacheCleanerService.cs +++ b/ErsatzTV/Services/RunOnce/CacheCleanerService.cs @@ -28,7 +28,7 @@ public class CacheCleanerService( ILocalFileSystem localFileSystem = scope.ServiceProvider.GetRequiredService(); IFileSystem fileSystem = scope.ServiceProvider.GetRequiredService(); - if (localFileSystem.FolderExists(FileSystemLayout.LegacyImageCacheFolder)) + if (fileSystem.Directory.Exists(FileSystemLayout.LegacyImageCacheFolder)) { logger.LogInformation("Migrating channel logos from legacy image cache folder"); @@ -54,14 +54,14 @@ public class CacheCleanerService( Directory.Delete(FileSystemLayout.LegacyImageCacheFolder, true); } - if (localFileSystem.FolderExists(FileSystemLayout.TranscodeFolder)) + if (fileSystem.Directory.Exists(FileSystemLayout.TranscodeFolder)) { logger.LogInformation("Emptying transcode cache folder"); localFileSystem.EmptyFolder(FileSystemLayout.TranscodeFolder); logger.LogInformation("Done emptying transcode cache folder"); } - if (localFileSystem.FolderExists(FileSystemLayout.ChannelGuideCacheFolder)) + if (fileSystem.Directory.Exists(FileSystemLayout.ChannelGuideCacheFolder)) { logger.LogInformation("Cleaning channel cache");