using System.Security.Cryptography; using System.Text; using DotBased.Logging; using DotBased.Monads; using Manager.Data.Contexts; using Manager.Data.Entities.Cache; using Manager.YouTube; using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Infrastructure; namespace Manager.App.Services.System; public class CacheService(ILogger logger, IHostEnvironment environment) : ExtendedBackgroundService(nameof(CacheService), "Manages caching.", logger, TimeSpan.FromDays(1)) { private DirectoryInfo? _cacheDirectory; private PooledDbContextFactory? _dbContextFactory; private const string DataSubDir = "data"; protected override Task InitializeAsync(CancellationToken stoppingToken) { _cacheDirectory = new DirectoryInfo(Path.Combine(Directory.GetCurrentDirectory(), "cache")); _cacheDirectory.Create(); Directory.CreateDirectory(Path.Combine(_cacheDirectory.FullName, DataSubDir)); LogEvent($"Cache directory: {_cacheDirectory.FullName}"); var dbContextOptionsBuilder = new DbContextOptionsBuilder(); dbContextOptionsBuilder.UseSqlite($"Data Source={Path.Combine(_cacheDirectory.FullName, "cache_index.db")}"); _dbContextFactory = new PooledDbContextFactory(dbContextOptionsBuilder.Options); return Task.CompletedTask; } protected override async Task ExecuteServiceAsync(CancellationToken stoppingToken) { if (environment.IsDevelopment()) { LogEvent("Development mode detected, skipping cache cleaning..."); return; } try { await ClearCacheAsync(stoppingToken); } catch (Exception e) { logger.LogError(e, "Error in execution of service."); LogEvent($"Service execution failed. {e.Message}", LogSeverity.Error); throw; } } public string CreateCacheUrl(string originalUrl) => $"/api/v1/cache?url={originalUrl}"; public async Task> CacheFromUrl(string url, CancellationToken cancellationToken = default) { try { if (string.IsNullOrWhiteSpace(url)) { return ResultError.Fail("Url is empty."); } if (_cacheDirectory == null) { return ResultError.Fail("Cache directory is not initialized."); } if (_dbContextFactory == null) { return ResultError.Fail("Context factory is not initialized."); } await using var context = await _dbContextFactory.CreateDbContextAsync(cancellationToken); var urlKeyBytes = SHA1.HashData(Encoding.UTF8.GetBytes(url)); var urlKey = Convert.ToHexString(urlKeyBytes); var cacheEntity = await context.Cache.FirstOrDefaultAsync(c => c.Id == urlKey, cancellationToken: cancellationToken); if (cacheEntity == null) { var downloadResult = await NetworkService.DownloadBytesAsync(new HttpRequestMessage(HttpMethod.Get, url)); if (!downloadResult.IsSuccess) { LogEvent($"Failed to download from url: {url}"); return ResultError.Fail("Download failed."); } var download = downloadResult.Value; await using var downloadFile = File.Create(Path.Combine(_cacheDirectory.FullName, DataSubDir, $"{urlKey}.cache")); await downloadFile.WriteAsync(download.Data.AsMemory(0, download.Data.Length), cancellationToken); cacheEntity = new CacheEntity { Id = urlKey, CachedAtUtc = DateTime.UtcNow, ContentLength = download.ContentLength, ContentType = download.ContentType, OriginalFileName = download.FileName, }; context.Cache.Add(cacheEntity); var saved = await context.SaveChangesAsync(cancellationToken); if (saved <= 0) { LogEvent($"Cache entity {cacheEntity.Id} could not be saved.", LogSeverity.Error); return ResultError.Fail("Failed to save to cache db."); } return new CacheFile(download.Data, download.ContentType, download.FileName); } var filePath = Path.Combine(_cacheDirectory.FullName, DataSubDir, $"{urlKey}.cache"); var buffer = await File.ReadAllBytesAsync(filePath, cancellationToken); if (buffer.Length == 0) { LogEvent($"Failed to read data from disk. File: {filePath}", LogSeverity.Error); return ResultError.Fail($"Error reading data from disk. File: {filePath}"); } return new CacheFile(buffer.ToArray(), cacheEntity.ContentType, cacheEntity.OriginalFileName); } catch (Exception e) { return ResultError.Error(e, "Cache error."); } } private async Task ClearCacheAsync(CancellationToken cancellationToken) { if (_dbContextFactory == null) { throw new InvalidOperationException("No DbContext factory configured."); } await using var dbContext = await _dbContextFactory.CreateDbContextAsync(cancellationToken); var toRemove = dbContext.Cache.Where(c => c.CachedAtUtc >= DateTime.UtcNow.AddDays(-1)); if (!toRemove.Any()) { LogEvent("No items found to purge from cache."); return; } var totalToRemove = toRemove.Count(); LogEvent($"Found {totalToRemove} cache items that are older than 1 day(s)"); dbContext.RemoveRange(toRemove); var deleted = await dbContext.SaveChangesAsync(cancellationToken); LogEvent($"Removed {deleted}/{totalToRemove} items"); } } public record CacheFile(byte[] Data, string? ContentType, string? OriginalFileName);