public async Task StartAsync(CancellationToken cancellationToken) { using IServiceScope scope = _serviceScopeFactory.CreateScope(); await using TvContext dbContext = scope.ServiceProvider.GetRequiredService <TvContext>(); if (Directory.Exists(FileSystemLayout.LegacyImageCacheFolder)) { _logger.LogInformation("Migrating channel logos from legacy image cache folder"); List <string> logos = await dbContext.Channels .SelectMany(c => c.Artwork) .Where(a => a.ArtworkKind == ArtworkKind.Logo) .Map(a => a.Path) .ToListAsync(cancellationToken); ILocalFileSystem localFileSystem = scope.ServiceProvider.GetRequiredService <ILocalFileSystem>(); foreach (string logo in logos) { string legacyPath = Path.Combine(FileSystemLayout.LegacyImageCacheFolder, logo); if (File.Exists(legacyPath)) { string subfolder = logo.Substring(0, 2); string newPath = Path.Combine(FileSystemLayout.LogoCacheFolder, subfolder, logo); await localFileSystem.CopyFile(legacyPath, newPath); } } _logger.LogInformation("Deleting legacy image cache folder"); Directory.Delete(FileSystemLayout.LegacyImageCacheFolder, true); } }
private static Unit CleanCache(TvContext context, IServiceProvider provider) { if (Directory.Exists(FileSystemLayout.LegacyImageCacheFolder)) { ILogger <Program> logger = provider.GetRequiredService <ILogger <Program> >(); logger.LogInformation("Migrating channel logos from legacy image cache folder"); var logos = context.Channels .SelectMany(c => c.Artwork) .Where(a => a.ArtworkKind == ArtworkKind.Logo) .Map(a => a.Path) .ToList(); ILocalFileSystem localFileSystem = provider.GetRequiredService <ILocalFileSystem>(); foreach (string logo in logos) { string legacyPath = Path.Combine(FileSystemLayout.LegacyImageCacheFolder, logo); if (File.Exists(legacyPath)) { string subfolder = logo.Substring(0, 2); string newPath = Path.Combine(FileSystemLayout.LogoCacheFolder, subfolder, logo); localFileSystem.CopyFile(legacyPath, newPath); } } logger.LogInformation("Deleting legacy image cache folder"); Directory.Delete(FileSystemLayout.LegacyImageCacheFolder, true); } return(Unit.Default); }