Drive file name query

This commit is contained in:
2025-11-18 00:48:35 +08:00
parent 1fa6c893a5
commit 92b28d830d
2 changed files with 42 additions and 14 deletions

View File

@@ -23,9 +23,10 @@ public class FileIndexController(
/// Gets files in a specific path for the current user /// Gets files in a specific path for the current user
/// </summary> /// </summary>
/// <param name="path">The path to browse (defaults to root "/")</param> /// <param name="path">The path to browse (defaults to root "/")</param>
/// <param name="query">Optional query to filter files by name</param>
/// <returns>List of files in the specified path</returns> /// <returns>List of files in the specified path</returns>
[HttpGet("browse")] [HttpGet("browse")]
public async Task<IActionResult> BrowseFiles([FromQuery] string path = "/") public async Task<IActionResult> BrowseFiles([FromQuery] string path = "/", [FromQuery] string? query = null)
{ {
if (HttpContext.Items["CurrentUser"] is not Account currentUser) if (HttpContext.Items["CurrentUser"] is not Account currentUser)
return new ObjectResult(ApiError.Unauthorized()) { StatusCode = 401 }; return new ObjectResult(ApiError.Unauthorized()) { StatusCode = 401 };
@@ -36,6 +37,13 @@ public class FileIndexController(
{ {
var fileIndexes = await fileIndexService.GetByPathAsync(accountId, path); var fileIndexes = await fileIndexService.GetByPathAsync(accountId, path);
if (!string.IsNullOrWhiteSpace(query))
{
fileIndexes = fileIndexes
.Where(fi => fi.File.Name.Contains(query, StringComparison.OrdinalIgnoreCase))
.ToList();
}
// Get all file indexes for this account to extract child folders // Get all file indexes for this account to extract child folders
var allFileIndexes = await fileIndexService.GetByAccountIdAsync(accountId); var allFileIndexes = await fileIndexService.GetByAccountIdAsync(accountId);
@@ -100,9 +108,10 @@ public class FileIndexController(
/// <summary> /// <summary>
/// Gets all files for the current user (across all paths) /// Gets all files for the current user (across all paths)
/// </summary> /// </summary>
/// <param name="query">Optional query to filter files by name</param>
/// <returns>List of all files for the user</returns> /// <returns>List of all files for the user</returns>
[HttpGet("all")] [HttpGet("all")]
public async Task<IActionResult> GetAllFiles() public async Task<IActionResult> GetAllFiles([FromQuery] string? query = null)
{ {
if (HttpContext.Items["CurrentUser"] is not Account currentUser) if (HttpContext.Items["CurrentUser"] is not Account currentUser)
return new ObjectResult(ApiError.Unauthorized()) { StatusCode = 401 }; return new ObjectResult(ApiError.Unauthorized()) { StatusCode = 401 };
@@ -113,6 +122,13 @@ public class FileIndexController(
{ {
var fileIndexes = await fileIndexService.GetByAccountIdAsync(accountId); var fileIndexes = await fileIndexService.GetByAccountIdAsync(accountId);
if (!string.IsNullOrWhiteSpace(query))
{
fileIndexes = fileIndexes
.Where(fi => fi.File.Name.Contains(query, StringComparison.OrdinalIgnoreCase))
.ToList();
}
return Ok(new return Ok(new
{ {
Files = fileIndexes, Files = fileIndexes,
@@ -144,7 +160,8 @@ public class FileIndexController(
[FromQuery] Guid? pool, [FromQuery] Guid? pool,
[FromQuery] bool recycled = false, [FromQuery] bool recycled = false,
[FromQuery] int offset = 0, [FromQuery] int offset = 0,
[FromQuery] int take = 20 [FromQuery] int take = 20,
[FromQuery] string? query = null
) )
{ {
if (HttpContext.Items["CurrentUser"] is not Account currentUser) if (HttpContext.Items["CurrentUser"] is not Account currentUser)
@@ -154,7 +171,7 @@ public class FileIndexController(
try try
{ {
var query = db.Files var filesQuery = db.Files
.Where(f => f.AccountId == accountId .Where(f => f.AccountId == accountId
&& f.IsMarkedRecycle == recycled && f.IsMarkedRecycle == recycled
&& !db.FileIndexes.Any(fi => fi.FileId == f.Id && fi.AccountId == accountId) && !db.FileIndexes.Any(fi => fi.FileId == f.Id && fi.AccountId == accountId)
@@ -162,13 +179,18 @@ public class FileIndexController(
.OrderByDescending(f => f.CreatedAt) .OrderByDescending(f => f.CreatedAt)
.AsQueryable(); .AsQueryable();
if (pool.HasValue) query = query.Where(f => f.PoolId == pool); if (pool.HasValue) filesQuery = filesQuery.Where(f => f.PoolId == pool);
var totalCount = await query.CountAsync(); if (!string.IsNullOrWhiteSpace(query))
{
filesQuery = filesQuery.Where(f => f.Name.Contains(query));
}
var totalCount = await filesQuery.CountAsync();
Response.Headers.Append("X-Total", totalCount.ToString()); Response.Headers.Append("X-Total", totalCount.ToString());
var unindexedFiles = await query var unindexedFiles = await filesQuery
.Skip(offset) .Skip(offset)
.Take(take) .Take(take)
.ToListAsync(); .ToListAsync();
@@ -510,4 +532,4 @@ public class CreateFileIndexRequest
{ {
[MaxLength(32)] public string FileId { get; set; } = null!; [MaxLength(32)] public string FileId { get; set; } = null!;
public string Path { get; set; } = null!; public string Path { get; set; } = null!;
} }

View File

@@ -278,25 +278,31 @@ public class FileController(
[FromQuery] Guid? pool, [FromQuery] Guid? pool,
[FromQuery] bool recycled = false, [FromQuery] bool recycled = false,
[FromQuery] int offset = 0, [FromQuery] int offset = 0,
[FromQuery] int take = 20 [FromQuery] int take = 20,
[FromQuery] string? query = null
) )
{ {
if (HttpContext.Items["CurrentUser"] is not Account currentUser) return Unauthorized(); if (HttpContext.Items["CurrentUser"] is not Account currentUser) return Unauthorized();
var accountId = Guid.Parse(currentUser.Id); var accountId = Guid.Parse(currentUser.Id);
var query = db.Files var filesQuery = db.Files
.Where(e => e.IsMarkedRecycle == recycled) .Where(e => e.IsMarkedRecycle == recycled)
.Where(e => e.AccountId == accountId) .Where(e => e.AccountId == accountId)
.Include(e => e.Pool) .Include(e => e.Pool)
.OrderByDescending(e => e.CreatedAt) .OrderByDescending(e => e.CreatedAt)
.AsQueryable(); .AsQueryable();
if (pool.HasValue) query = query.Where(e => e.PoolId == pool); if (pool.HasValue) filesQuery = filesQuery.Where(e => e.PoolId == pool);
var total = await query.CountAsync(); if (!string.IsNullOrWhiteSpace(query))
{
filesQuery = filesQuery.Where(e => e.Name.Contains(query));
}
var total = await filesQuery.CountAsync();
Response.Headers.Append("X-Total", total.ToString()); Response.Headers.Append("X-Total", total.ToString());
var files = await query var files = await filesQuery
.Skip(offset) .Skip(offset)
.Take(take) .Take(take)
.ToListAsync(); .ToListAsync();
@@ -448,4 +454,4 @@ public class FileController(
throw; throw;
} }
} }
} }