[CHANGE] Fixed cache service && Download channel images from cache

This commit is contained in:
max
2025-09-18 00:30:35 +02:00
parent 8a64d6fc64
commit ab532ac6dc
2 changed files with 52 additions and 17 deletions

View File

@@ -3,9 +3,9 @@ using Manager.App.Constants;
using Manager.App.Models.Library; using Manager.App.Models.Library;
using Manager.App.Models.Settings; using Manager.App.Models.Settings;
using Manager.App.Models.System; using Manager.App.Models.System;
using Manager.App.Services.System;
using Manager.Data.Contexts; using Manager.Data.Contexts;
using Manager.Data.Entities.LibraryContext; using Manager.Data.Entities.LibraryContext;
using Manager.YouTube;
using Manager.YouTube.Models.Innertube; using Manager.YouTube.Models.Innertube;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;
@@ -18,15 +18,16 @@ public class LibraryService : ILibraryService
private readonly LibrarySettings _librarySettings; private readonly LibrarySettings _librarySettings;
private readonly IDbContextFactory<LibraryDbContext> _dbContextFactory; private readonly IDbContextFactory<LibraryDbContext> _dbContextFactory;
private readonly DirectoryInfo _libraryDirectory; private readonly DirectoryInfo _libraryDirectory;
private readonly CacheService _cacheService;
public LibraryService(ILogger<LibraryService> logger, IOptions<LibrarySettings> librarySettings, IDbContextFactory<LibraryDbContext> contextFactory, CacheService cacheService)
public LibraryService(ILogger<LibraryService> logger, IOptions<LibrarySettings> librarySettings, IDbContextFactory<LibraryDbContext> contextFactory)
{ {
_logger = logger; _logger = logger;
_librarySettings = librarySettings.Value; _librarySettings = librarySettings.Value;
_dbContextFactory = contextFactory; _dbContextFactory = contextFactory;
_cacheService = cacheService;
_libraryDirectory = Directory.CreateDirectory(_librarySettings.Path); _libraryDirectory = Directory.CreateDirectory(_librarySettings.Path);
logger.LogDebug("Working dir for library: {LibraryWorkingDir}", _libraryDirectory.FullName); logger.LogDebug("Library directory: {LibraryWorkingDir}", _libraryDirectory.FullName);
Directory.CreateDirectory(Path.Combine(_librarySettings.Path, LibraryConstants.Directories.SubDirMedia)); Directory.CreateDirectory(Path.Combine(_librarySettings.Path, LibraryConstants.Directories.SubDirMedia));
Directory.CreateDirectory(Path.Combine(_librarySettings.Path, LibraryConstants.Directories.SubDirChannels)); Directory.CreateDirectory(Path.Combine(_librarySettings.Path, LibraryConstants.Directories.SubDirChannels));
} }
@@ -65,32 +66,33 @@ public class LibraryService : ILibraryService
continue; continue;
} }
var downloadResult = await NetworkService.DownloadBytesAsync(new HttpRequestMessage(HttpMethod.Get, image.Url)); var cacheResult = await _cacheService.CacheFromUrl(image.Url);
if (!downloadResult.IsSuccess) if (!cacheResult.IsSuccess)
{ {
_logger.LogWarning("Failed to get image {ImageUrl}", image.Url);
continue; continue;
} }
var download = downloadResult.Value; var cachedFile = cacheResult.Value;
var fileId = Guid.NewGuid(); var fileId = Guid.NewGuid();
var fileName = download.FileName ?? $"{fileId}.{download.ContentType?.Split('/').Last() ?? "unknown"}"; var fileName = cachedFile.OriginalFileName ?? $"{fileId}.{cachedFile.ContentType?.Split('/').Last() ?? "unknown"}";
var relativePath = Path.Combine(foreignKey, libSubDir, $"{DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()}_{fileName}"); var relativePath = Path.Combine(foreignKey, libSubDir, $"{DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()}_{fileName}");
var savePath = Path.Combine(_libraryDirectory.FullName, subDir, relativePath); var savePath = Path.Combine(_libraryDirectory.FullName, subDir, relativePath);
Directory.CreateDirectory(Path.GetDirectoryName(savePath) ?? savePath); Directory.CreateDirectory(Path.GetDirectoryName(savePath) ?? savePath);
await using var fileStream = File.Create(savePath); await using var fileStream = File.Create(savePath);
await fileStream.WriteAsync(download.Data.AsMemory(0, download.Data.Length)); await fileStream.WriteAsync(cachedFile.Data.AsMemory(0, cachedFile.Data.Length));
var file = new FileEntity var file = new FileEntity
{ {
Id = fileId, Id = fileId,
OriginalUrl = image.Url, OriginalUrl = image.Url,
OriginalFileName = download.FileName, OriginalFileName = cachedFile.OriginalFileName,
ForeignKey = foreignKey, ForeignKey = foreignKey,
FileType = fileType, FileType = fileType,
RelativePath = relativePath.Replace('\\', '/'), RelativePath = relativePath.Replace('\\', '/'),
MimeType = download.ContentType, MimeType = cachedFile.ContentType,
SizeBytes = download.ContentLength, SizeBytes = cachedFile.Data.Length,
Height = image.Height, Height = image.Height,
Width = image.Width Width = image.Width
}; };

View File

@@ -10,7 +10,7 @@ using Microsoft.EntityFrameworkCore.Infrastructure;
namespace Manager.App.Services.System; namespace Manager.App.Services.System;
public class CacheService(ILogger<CacheService> logger, IHostEnvironment environment) : ExtendedBackgroundService(nameof(CacheService), "Manages caching.", logger, TimeSpan.FromDays(1)) public class CacheService(ILogger<CacheService> logger, IHostEnvironment environment) : ExtendedBackgroundService(nameof(CacheService), "Manages caching.", logger, TimeSpan.FromHours(5))
{ {
private DirectoryInfo? _cacheDirectory; private DirectoryInfo? _cacheDirectory;
private PooledDbContextFactory<CacheDbContext>? _dbContextFactory; private PooledDbContextFactory<CacheDbContext>? _dbContextFactory;
@@ -131,9 +131,14 @@ public class CacheService(ILogger<CacheService> logger, IHostEnvironment environ
throw new InvalidOperationException("No DbContext factory configured."); throw new InvalidOperationException("No DbContext factory configured.");
} }
if (_cacheDirectory == null)
{
throw new InvalidOperationException("No cache directory configured.");
}
await using var dbContext = await _dbContextFactory.CreateDbContextAsync(cancellationToken); await using var dbContext = await _dbContextFactory.CreateDbContextAsync(cancellationToken);
var toRemove = dbContext.Cache.Where(c => c.CachedAtUtc >= DateTime.UtcNow.AddDays(-1)); var toRemove = dbContext.Cache.Where(c => c.CachedAtUtc < DateTime.UtcNow.AddDays(-1));
if (!toRemove.Any()) if (!toRemove.Any())
{ {
LogEvent("No items found to purge from cache."); LogEvent("No items found to purge from cache.");
@@ -142,9 +147,37 @@ public class CacheService(ILogger<CacheService> logger, IHostEnvironment environ
var totalToRemove = toRemove.Count(); var totalToRemove = toRemove.Count();
LogEvent($"Found {totalToRemove} cache items that are older than 1 day(s)"); LogEvent($"Found {totalToRemove} cache items that are older than 1 day(s)");
dbContext.RemoveRange(toRemove);
var deleted = await dbContext.SaveChangesAsync(cancellationToken); var deleted = new List<CacheEntity>();
LogEvent($"Removed {deleted}/{totalToRemove} items"); foreach (var entity in toRemove)
{
var pathToFile = Path.Combine(_cacheDirectory.FullName, DataSubDir, $"{entity.Id}.cache");
if (!File.Exists(pathToFile))
{
deleted.Add(entity);
continue;
}
try
{
File.Delete(pathToFile);
}
catch (Exception e)
{
logger.LogError(e, "Failed to delete cache entity with id: {EntityId}. Skipping cache entity...", entity.Id);
continue;
}
deleted.Add(entity);
}
dbContext.RemoveRange(deleted);
var dbDeleted = await dbContext.SaveChangesAsync(cancellationToken);
if (dbDeleted < deleted.Count)
{
LogEvent("Could not delete all files from cache.", LogSeverity.Warning);
}
LogEvent($"Removed {dbDeleted}/{totalToRemove} items");
} }
} }