2026-05-22 14:29:22 +08:00
|
|
|
using FileShare_Common.Core;
|
|
|
|
|
using FileShare_Services.Core;
|
2026-05-21 16:45:56 +08:00
|
|
|
|
2026-05-22 14:29:22 +08:00
|
|
|
namespace FileShare_Services.Services.FileLibrary
|
2026-05-21 16:45:56 +08:00
|
|
|
{
|
2026-05-22 14:45:07 +08:00
|
|
|
/// <summary>
|
|
|
|
|
/// 文件库 HTTP 端点服务,将请求适配到 <see cref="IFileLibraryService"/> 并包装为 <see cref="IApiResponse"/>。
|
|
|
|
|
/// </summary>
|
2026-05-21 16:45:56 +08:00
|
|
|
public sealed class FileLibraryEndpointService(IFileLibraryService fileLibrary) : IFileLibraryEndpointService
|
|
|
|
|
{
|
2026-05-22 14:45:07 +08:00
|
|
|
/// <inheritdoc />
|
2026-05-22 11:18:47 +08:00
|
|
|
public async Task<IApiResponse> GetDrivesAsync(ServiceEndpointContext ctx)
|
2026-05-21 16:45:56 +08:00
|
|
|
{
|
|
|
|
|
return ResponseHelper.Ok(await fileLibrary.GetDrivesAsync());
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-22 14:45:07 +08:00
|
|
|
/// <inheritdoc />
|
2026-05-22 11:18:47 +08:00
|
|
|
public async Task<IApiResponse> GetDirectoriesAsync(DirectoryQueryRequest request)
|
2026-05-21 16:45:56 +08:00
|
|
|
{
|
2026-05-22 11:18:47 +08:00
|
|
|
return ResponseHelper.Ok(await fileLibrary.GetDirectoriesAsync(request.Path));
|
2026-05-21 16:45:56 +08:00
|
|
|
}
|
|
|
|
|
|
2026-05-22 14:45:07 +08:00
|
|
|
/// <inheritdoc />
|
2026-05-22 11:18:47 +08:00
|
|
|
public async Task<IApiResponse> GetRootsAsync(ServiceEndpointContext ctx)
|
2026-05-21 16:45:56 +08:00
|
|
|
{
|
|
|
|
|
return ResponseHelper.Ok(await fileLibrary.GetRootsAsync());
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-22 14:45:07 +08:00
|
|
|
/// <inheritdoc />
|
2026-05-22 11:18:47 +08:00
|
|
|
public async Task<IApiResponse> AddRootAsync(AddLibraryRootRequest request)
|
2026-05-21 16:45:56 +08:00
|
|
|
{
|
|
|
|
|
return ResponseHelper.Ok(await fileLibrary.AddRootAsync(request), "文件库目录已添加并完成扫描。");
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-22 14:45:07 +08:00
|
|
|
/// <inheritdoc />
|
2026-05-22 11:18:47 +08:00
|
|
|
public async Task<IApiResponse> SetRootEnabledAsync(UpdateLibraryRootRequest request)
|
2026-05-21 16:45:56 +08:00
|
|
|
{
|
|
|
|
|
return ResponseHelper.Ok(await fileLibrary.SetRootEnabledAsync(request), "文件库目录状态已更新。");
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-22 14:45:07 +08:00
|
|
|
/// <inheritdoc />
|
2026-05-22 11:18:47 +08:00
|
|
|
public async Task<IApiResponse> DeleteRootAsync(DeleteLibraryRootRequest request)
|
2026-05-21 16:45:56 +08:00
|
|
|
{
|
|
|
|
|
await fileLibrary.DeleteRootAsync(request);
|
|
|
|
|
return ResponseHelper.Succeed("文件库目录已删除。");
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-22 14:45:07 +08:00
|
|
|
/// <inheritdoc />
|
2026-05-22 11:18:47 +08:00
|
|
|
public async Task<IApiResponse> ScanRootAsync(ScanLibraryRootRequest request)
|
2026-05-21 16:45:56 +08:00
|
|
|
{
|
|
|
|
|
return ResponseHelper.Ok(await fileLibrary.ScanRootAsync(request.Id), "文件库目录扫描完成。");
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-22 14:45:07 +08:00
|
|
|
/// <inheritdoc />
|
2026-05-22 11:18:47 +08:00
|
|
|
public async Task<IApiResponse> SearchFilesAsync(SearchFilesRequest request)
|
2026-05-21 16:45:56 +08:00
|
|
|
{
|
2026-05-22 11:18:47 +08:00
|
|
|
return await fileLibrary.SearchFilesAsync(request);
|
2026-05-21 16:45:56 +08:00
|
|
|
}
|
|
|
|
|
|
2026-05-22 14:45:07 +08:00
|
|
|
/// <inheritdoc />
|
2026-05-22 11:18:47 +08:00
|
|
|
public async Task<IApiResponse> GetFileAsync(FileQueryRequest request)
|
2026-05-21 16:45:56 +08:00
|
|
|
{
|
2026-05-22 11:18:47 +08:00
|
|
|
ValidateFileId(request.Id);
|
|
|
|
|
var file = await fileLibrary.GetFileAsync(request.Id);
|
2026-05-21 16:45:56 +08:00
|
|
|
return file is null
|
|
|
|
|
? ResponseHelper.Failure(404, "文件不存在或尚未扫描入库。")
|
|
|
|
|
: ResponseHelper.Ok(file);
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-22 14:45:07 +08:00
|
|
|
/// <inheritdoc />
|
2026-05-22 11:18:47 +08:00
|
|
|
public async Task<IApiResponse> GetTextPreviewAsync(FileQueryRequest request)
|
2026-05-21 16:45:56 +08:00
|
|
|
{
|
2026-05-22 11:18:47 +08:00
|
|
|
ValidateFileId(request.Id);
|
|
|
|
|
var preview = await fileLibrary.GetTextPreviewAsync(request.Id);
|
2026-05-21 16:45:56 +08:00
|
|
|
return preview is null
|
|
|
|
|
? ResponseHelper.Failure(404, "文本文件不存在或无法预览。")
|
|
|
|
|
: ResponseHelper.Ok(preview);
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-22 14:45:07 +08:00
|
|
|
/// <inheritdoc />
|
2026-05-22 11:59:45 +08:00
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-22 14:45:07 +08:00
|
|
|
/// <summary>
|
|
|
|
|
/// 验证文件 ID 是否有效,无效时抛出 <see cref="ArgumentException"/>。
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="id">文件记录 ID。</param>
|
|
|
|
|
/// <exception cref="ArgumentException">ID 小于等于 0 时抛出。</exception>
|
2026-05-22 11:18:47 +08:00
|
|
|
private static void ValidateFileId(int id)
|
2026-05-21 16:45:56 +08:00
|
|
|
{
|
2026-05-22 11:18:47 +08:00
|
|
|
if (id > 0)
|
2026-05-21 16:45:56 +08:00
|
|
|
{
|
2026-05-22 11:18:47 +08:00
|
|
|
return;
|
2026-05-21 16:45:56 +08:00
|
|
|
}
|
|
|
|
|
|
2026-05-22 11:18:47 +08:00
|
|
|
throw new ArgumentException("id 参数无效。");
|
2026-05-21 16:45:56 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|