- 将播放器收敛为单个共享实例,避免列表/网格内多个 video ref 导致进度保存失效 - 恢复视频播放器上方的继续播放提示 - 在播放、暂停、拖动、结束、切换页面和离开页面时保存播放位置 - 保留文件浏览分页和排序参数的前端调用
132 lines
4.9 KiB
C#
132 lines
4.9 KiB
C#
using FileShare_Common.Core;
|
|
using FileShare_Services.Core;
|
|
|
|
namespace FileShare_Services.Services.FileLibrary
|
|
{
|
|
/// <summary>
|
|
/// 文件库 HTTP 端点服务,将请求适配到 <see cref="IFileLibraryService"/> 并包装为 <see cref="IApiResponse"/>。
|
|
/// </summary>
|
|
public sealed class FileLibraryEndpointService(IFileLibraryService fileLibrary) : IFileLibraryEndpointService
|
|
{
|
|
/// <inheritdoc />
|
|
public async Task<IApiResponse> GetDrivesAsync(ServiceEndpointContext ctx)
|
|
{
|
|
return ResponseHelper.Ok(await fileLibrary.GetDrivesAsync());
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public async Task<IApiResponse> GetDirectoriesAsync(DirectoryQueryRequest request)
|
|
{
|
|
return ResponseHelper.Ok(await fileLibrary.GetDirectoriesAsync(request.Path));
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public async Task<IApiResponse> GetRootsAsync(ServiceEndpointContext ctx)
|
|
{
|
|
return ResponseHelper.Ok(await fileLibrary.GetRootsAsync());
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public async Task<IApiResponse> AddRootAsync(AddLibraryRootRequest request)
|
|
{
|
|
return ResponseHelper.Ok(await fileLibrary.AddRootAsync(request), "文件库目录已添加,后续扫描将自动入库。");
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public async Task<IApiResponse> SetRootEnabledAsync(UpdateLibraryRootRequest request)
|
|
{
|
|
return ResponseHelper.Ok(await fileLibrary.SetRootEnabledAsync(request), "文件库目录状态已更新。");
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public async Task<IApiResponse> DeleteRootAsync(DeleteLibraryRootRequest request)
|
|
{
|
|
await fileLibrary.DeleteRootAsync(request);
|
|
return ResponseHelper.Succeed("文件库目录已删除。");
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public async Task<IApiResponse> ScanRootAsync(ScanLibraryRootRequest request)
|
|
{
|
|
return ResponseHelper.Ok(await fileLibrary.ScanRootAsync(request.Id), "文件库目录扫描完成。");
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public async Task<IApiResponse> SearchFilesAsync(SearchFilesRequest request)
|
|
{
|
|
return await fileLibrary.SearchFilesAsync(request);
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public async Task<IApiResponse> GetFileAsync(FileQueryRequest request)
|
|
{
|
|
ValidateFileId(request.Id);
|
|
var file = await fileLibrary.GetFileAsync(request.Id);
|
|
return file is null
|
|
? ResponseHelper.Failure(404, "文件不存在或尚未扫描入库。")
|
|
: ResponseHelper.Ok(file);
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public async Task<IApiResponse> GetTextPreviewAsync(FileQueryRequest request)
|
|
{
|
|
ValidateFileId(request.Id);
|
|
var preview = await fileLibrary.GetTextPreviewAsync(request.Id);
|
|
return preview is null
|
|
? ResponseHelper.Failure(404, "文本文件不存在或无法预览。")
|
|
: ResponseHelper.Ok(preview);
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public async Task<IApiResponse> BrowseDirectoryAsync(BrowseDirectoryRequest request)
|
|
{
|
|
if (request.RootId <= 0)
|
|
return ResponseHelper.Failure(400, "rootId 参数无效。");
|
|
|
|
var result = await fileLibrary.BrowseDirectoryAsync(request);
|
|
return ResponseHelper.Ok(result);
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public async Task<IApiResponse> GetRecentFilesAsync(RecentFilesRequest request)
|
|
{
|
|
var items = await fileLibrary.GetRecentFilesAsync(request.Type, request.Count);
|
|
return ResponseHelper.Ok(items);
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public async Task<IApiResponse> MarkFilePlayedAsync(MarkFilePlayedRequest request)
|
|
{
|
|
await fileLibrary.MarkFilePlayedAsync(request.Id);
|
|
return ResponseHelper.Succeed();
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public async Task<IApiResponse> SaveFileProgressAsync(SaveFileProgressRequest request)
|
|
{
|
|
if (request.Id <= 0 || request.Position is null || double.IsNaN(request.Position.Value) || double.IsInfinity(request.Position.Value))
|
|
{
|
|
return ResponseHelper.Failure(400, "参数错误");
|
|
}
|
|
|
|
await fileLibrary.SaveFileProgressAsync(request.Id, request.Position.Value);
|
|
return ResponseHelper.Succeed();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 验证文件 ID 是否有效,无效时抛出 <see cref="ArgumentException"/>。
|
|
/// </summary>
|
|
/// <param name="id">文件记录 ID。</param>
|
|
/// <exception cref="ArgumentException">ID 小于等于 0 时抛出。</exception>
|
|
private static void ValidateFileId(int id)
|
|
{
|
|
if (id > 0)
|
|
{
|
|
return;
|
|
}
|
|
|
|
throw new ArgumentException("id 参数无效。");
|
|
}
|
|
}
|
|
}
|