39 lines
1.2 KiB
C#
39 lines
1.2 KiB
C#
|
|
using FileShare_EFCore.Database;
|
||
|
|
using FileShare_Services.Core;
|
||
|
|
using Microsoft.EntityFrameworkCore;
|
||
|
|
|
||
|
|
namespace FileShare_Services.Services.FileLibrary
|
||
|
|
{
|
||
|
|
public interface IThumbnailStreamService
|
||
|
|
{
|
||
|
|
Task<FileStreamResponse?> GetThumbnailAsync(int id, CancellationToken cancellationToken = default);
|
||
|
|
}
|
||
|
|
|
||
|
|
public sealed class ThumbnailStreamService(AppDataContext db, IVideoThumbnailService thumbnails) : IThumbnailStreamService
|
||
|
|
{
|
||
|
|
public async Task<FileStreamResponse?> GetThumbnailAsync(int id, CancellationToken cancellationToken = default)
|
||
|
|
{
|
||
|
|
var thumbnail = await db.ManagedThumbnailMaps
|
||
|
|
.AsNoTracking()
|
||
|
|
.FirstOrDefaultAsync(item => item.Id == id, cancellationToken);
|
||
|
|
|
||
|
|
if (thumbnail is null)
|
||
|
|
{
|
||
|
|
return null;
|
||
|
|
}
|
||
|
|
|
||
|
|
var absolutePath = thumbnails.GetAbsolutePath(thumbnail.RelativePath);
|
||
|
|
if (!File.Exists(absolutePath))
|
||
|
|
{
|
||
|
|
return null;
|
||
|
|
}
|
||
|
|
|
||
|
|
return new FileStreamResponse(
|
||
|
|
absolutePath,
|
||
|
|
Path.GetFileName(absolutePath),
|
||
|
|
thumbnail.ContentType,
|
||
|
|
File.GetLastWriteTimeUtc(absolutePath));
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|