From 92b28d830d421f4edf8ced736a5bb4e900505ec1 Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Tue, 18 Nov 2025 00:48:35 +0800 Subject: [PATCH] :sparkles: Drive file name query --- .../Index/FileIndexController.cs | 38 +++++++++++++++---- DysonNetwork.Drive/Storage/FileController.cs | 18 ++++++--- 2 files changed, 42 insertions(+), 14 deletions(-) diff --git a/DysonNetwork.Drive/Index/FileIndexController.cs b/DysonNetwork.Drive/Index/FileIndexController.cs index 06b6e73..4f6fe22 100644 --- a/DysonNetwork.Drive/Index/FileIndexController.cs +++ b/DysonNetwork.Drive/Index/FileIndexController.cs @@ -23,9 +23,10 @@ public class FileIndexController( /// Gets files in a specific path for the current user /// /// The path to browse (defaults to root "/") + /// Optional query to filter files by name /// List of files in the specified path [HttpGet("browse")] - public async Task BrowseFiles([FromQuery] string path = "/") + public async Task 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( /// /// Gets all files for the current user (across all paths) /// + /// Optional query to filter files by name /// List of all files for the user [HttpGet("all")] - public async Task GetAllFiles() + public async Task 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!; -} \ No newline at end of file +} diff --git a/DysonNetwork.Drive/Storage/FileController.cs b/DysonNetwork.Drive/Storage/FileController.cs index be129b2..27720f2 100644 --- a/DysonNetwork.Drive/Storage/FileController.cs +++ b/DysonNetwork.Drive/Storage/FileController.cs @@ -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; } } -} \ No newline at end of file +}