38 lines
1.2 KiB
C#
Raw Normal View History

2026-05-22 14:29:22 +08:00
using FileShare_EFCore.Database;
using FileShare_EFCore.Models;
using FileShare_Services.Core;
using Microsoft.EntityFrameworkCore;
2026-05-22 14:29:22 +08:00
namespace FileShare_Services.Services.FileLibrary
{
public interface IFileStreamService
{
Task<FileStreamResponse?> GetFileStreamAsync(int id, CancellationToken cancellationToken = default);
}
public sealed class FileStreamService(AppDataContext db) : IFileStreamService
{
public async Task<FileStreamResponse?> GetFileStreamAsync(int id, CancellationToken cancellationToken = default)
{
var file = await db.ManagedFileRecords
.AsNoTracking()
.Include(item => item.LibraryRoot)
.FirstOrDefaultAsync(item =>
item.Id == id
&& item.Exists
&& item.LibraryRoot != null
&& item.LibraryRoot.IsAvailable,
cancellationToken);
if (file is null || !System.IO.File.Exists(file.AbsolutePath))
return null;
return new FileStreamResponse(
file.AbsolutePath,
file.FileName,
file.ContentType,
file.LastWriteTimeUtc);
}
}
}