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
/// </summary>
/// <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>
[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)
return new ObjectResult(ApiError.Unauthorized()) { StatusCode = 401 };
@@ -36,6 +37,13 @@ public class FileIndexController(
{
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
var allFileIndexes = await fileIndexService.GetByAccountIdAsync(accountId);
@@ -100,9 +108,10 @@ public class FileIndexController(
/// <summary>
/// Gets all files for the current user (across all paths)
/// </summary>
/// <param name="query">Optional query to filter files by name</param>
/// <returns>List of all files for the user</returns>
[HttpGet("all")]
public async Task<IActionResult> GetAllFiles()
public async Task<IActionResult> GetAllFiles([FromQuery] string? query = null)
{
if (HttpContext.Items["CurrentUser"] is not Account currentUser)
return new ObjectResult(ApiError.Unauthorized()) { StatusCode = 401 };
@@ -113,6 +122,13 @@ public class FileIndexController(
{
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
{
Files = fileIndexes,
@@ -144,7 +160,8 @@ public class FileIndexController(
[FromQuery] Guid? pool,
[FromQuery] bool recycled = false,
[FromQuery] int offset = 0,
[FromQuery] int take = 20
[FromQuery] int take = 20,
[FromQuery] string? query = null
)
{
if (HttpContext.Items["CurrentUser"] is not Account currentUser)
@@ -154,7 +171,7 @@ public class FileIndexController(
try
{
var query = db.Files
var filesQuery = db.Files
.Where(f => f.AccountId == accountId
&& f.IsMarkedRecycle == recycled
&& !db.FileIndexes.Any(fi => fi.FileId == f.Id && fi.AccountId == accountId)
@@ -162,13 +179,18 @@ public class FileIndexController(
.OrderByDescending(f => f.CreatedAt)
.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());
var unindexedFiles = await query
var unindexedFiles = await filesQuery
.Skip(offset)
.Take(take)
.ToListAsync();
@@ -510,4 +532,4 @@ public class CreateFileIndexRequest
{
[MaxLength(32)] public string FileId { get; set; } = null!;
public string Path { get; set; } = null!;
}
}

View File

@@ -278,25 +278,31 @@ public class FileController(
[FromQuery] Guid? pool,
[FromQuery] bool recycled = false,
[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();
var accountId = Guid.Parse(currentUser.Id);
var query = db.Files
var filesQuery = db.Files
.Where(e => e.IsMarkedRecycle == recycled)
.Where(e => e.AccountId == accountId)
.Include(e => e.Pool)
.OrderByDescending(e => e.CreatedAt)
.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());
var files = await query
var files = await filesQuery
.Skip(offset)
.Take(take)
.ToListAsync();
@@ -448,4 +454,4 @@ public class FileController(
throw;
}
}
}
}