[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.Settings;
using Manager.App.Models.System;
using Manager.App.Services.System;
using Manager.Data.Contexts;
using Manager.Data.Entities.LibraryContext;
using Manager.YouTube;
using Manager.YouTube.Models.Innertube;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Options;
@@ -18,15 +18,16 @@ public class LibraryService : ILibraryService
private readonly LibrarySettings _librarySettings;
private readonly IDbContextFactory<LibraryDbContext> _dbContextFactory;
private readonly DirectoryInfo _libraryDirectory;
private readonly CacheService _cacheService;
public LibraryService(ILogger<LibraryService> logger, IOptions<LibrarySettings> librarySettings, IDbContextFactory<LibraryDbContext> contextFactory)
public LibraryService(ILogger<LibraryService> logger, IOptions<LibrarySettings> librarySettings, IDbContextFactory<LibraryDbContext> contextFactory, CacheService cacheService)
{
_logger = logger;
_librarySettings = librarySettings.Value;
_dbContextFactory = contextFactory;
_cacheService = cacheService;
_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.SubDirChannels));
}
@@ -65,32 +66,33 @@ public class LibraryService : ILibraryService
continue;
}
var downloadResult = await NetworkService.DownloadBytesAsync(new HttpRequestMessage(HttpMethod.Get, image.Url));
if (!downloadResult.IsSuccess)
var cacheResult = await _cacheService.CacheFromUrl(image.Url);
if (!cacheResult.IsSuccess)
{
_logger.LogWarning("Failed to get image {ImageUrl}", image.Url);
continue;
}
var download = downloadResult.Value;
var cachedFile = cacheResult.Value;
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 savePath = Path.Combine(_libraryDirectory.FullName, subDir, relativePath);
Directory.CreateDirectory(Path.GetDirectoryName(savePath) ?? 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
{
Id = fileId,
OriginalUrl = image.Url,
OriginalFileName = download.FileName,
OriginalFileName = cachedFile.OriginalFileName,
ForeignKey = foreignKey,
FileType = fileType,
RelativePath = relativePath.Replace('\\', '/'),
MimeType = download.ContentType,
SizeBytes = download.ContentLength,
MimeType = cachedFile.ContentType,
SizeBytes = cachedFile.Data.Length,
Height = image.Height,
Width = image.Width
};