38 lines
1.2 KiB
C#
38 lines
1.2 KiB
C#
using FileShare_EFCore.Database;
|
|
using FileShare_EFCore.Models;
|
|
using FileShare_Services.Core;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
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);
|
|
}
|
|
}
|
|
}
|