✨ Unindexed files has similar filter to the list file API
This commit is contained in:
@@ -134,11 +134,18 @@ public class FileIndexController(
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets files that have not been indexed for the current user.
|
/// Gets files that have not been indexed for the current user.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
/// <param name="recycled">Shows recycled files or not</param>
|
||||||
/// <param name="offset">The number of files to skip</param>
|
/// <param name="offset">The number of files to skip</param>
|
||||||
/// <param name="take">The number of files to return</param>
|
/// <param name="take">The number of files to return</param>
|
||||||
|
/// <param name="pool">The pool ID of those files</param>
|
||||||
/// <returns>List of unindexed files</returns>
|
/// <returns>List of unindexed files</returns>
|
||||||
[HttpGet("unindexed")]
|
[HttpGet("unindexed")]
|
||||||
public async Task<IActionResult> GetUnindexedFiles([FromQuery] int offset = 0, [FromQuery] int take = 20)
|
public async Task<IActionResult> GetUnindexedFiles(
|
||||||
|
[FromQuery] Guid? pool,
|
||||||
|
[FromQuery] bool recycled = false,
|
||||||
|
[FromQuery] int offset = 0,
|
||||||
|
[FromQuery] int take = 20
|
||||||
|
)
|
||||||
{
|
{
|
||||||
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 };
|
||||||
@@ -149,9 +156,12 @@ public class FileIndexController(
|
|||||||
{
|
{
|
||||||
var query = db.Files
|
var query = db.Files
|
||||||
.Where(f => f.AccountId == accountId
|
.Where(f => f.AccountId == accountId
|
||||||
&& !f.IsMarkedRecycle
|
&& 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))
|
||||||
.OrderByDescending(f => f.CreatedAt);
|
.OrderByDescending(f => f.CreatedAt)
|
||||||
|
.AsQueryable();
|
||||||
|
|
||||||
|
if (pool.HasValue) query = query.Where(f => f.PoolId == pool);
|
||||||
|
|
||||||
var totalCount = await query.CountAsync();
|
var totalCount = await query.CountAsync();
|
||||||
|
|
||||||
@@ -296,7 +306,9 @@ public class FileIndexController(
|
|||||||
|
|
||||||
return Ok(new
|
return Ok(new
|
||||||
{
|
{
|
||||||
Message = deleteFile ? "File index and file data removed successfully" : "File index removed successfully",
|
Message = deleteFile
|
||||||
|
? "File index and file data removed successfully"
|
||||||
|
: "File index removed successfully",
|
||||||
FileId = fileId,
|
FileId = fileId,
|
||||||
FileName = fileName,
|
FileName = fileName,
|
||||||
Path = filePath,
|
Path = filePath,
|
||||||
@@ -357,13 +369,14 @@ public class FileIndexController(
|
|||||||
db.Files.Remove(file);
|
db.Files.Remove(file);
|
||||||
logger.LogInformation("Deleted orphaned file {FileId} after clearing path {Path}", fileId, path);
|
logger.LogInformation("Deleted orphaned file {FileId} after clearing path {Path}", fileId, path);
|
||||||
}
|
}
|
||||||
|
|
||||||
await db.SaveChangesAsync();
|
await db.SaveChangesAsync();
|
||||||
|
|
||||||
return Ok(new
|
return Ok(new
|
||||||
{
|
{
|
||||||
Message = deleteFiles ?
|
Message = deleteFiles
|
||||||
$"Cleared {removedCount} file indexes from path and deleted orphaned files" :
|
? $"Cleared {removedCount} file indexes from path and deleted orphaned files"
|
||||||
$"Cleared {removedCount} file indexes from path",
|
: $"Cleared {removedCount} file indexes from path",
|
||||||
Path = path,
|
Path = path,
|
||||||
RemovedCount = removedCount,
|
RemovedCount = removedCount,
|
||||||
FilesDeleted = deleteFiles
|
FilesDeleted = deleteFiles
|
||||||
@@ -406,7 +419,8 @@ public class FileIndexController(
|
|||||||
|
|
||||||
// Check if index already exists for this file and path
|
// Check if index already exists for this file and path
|
||||||
var existingIndex = await db.FileIndexes
|
var existingIndex = await db.FileIndexes
|
||||||
.FirstOrDefaultAsync(fi => fi.FileId == request.FileId && fi.Path == request.Path && fi.AccountId == accountId);
|
.FirstOrDefaultAsync(fi =>
|
||||||
|
fi.FileId == request.FileId && fi.Path == request.Path && fi.AccountId == accountId);
|
||||||
|
|
||||||
if (existingIndex != null)
|
if (existingIndex != null)
|
||||||
return new ObjectResult(ApiError.Validation(new Dictionary<string, string[]>
|
return new ObjectResult(ApiError.Validation(new Dictionary<string, string[]>
|
||||||
|
|||||||
@@ -63,30 +63,31 @@ public class FileController(
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task<ActionResult> ServeLocalFile(SnCloudFile file)
|
private Task<ActionResult> ServeLocalFile(SnCloudFile file)
|
||||||
{
|
{
|
||||||
// Try temp storage first
|
// Try temp storage first
|
||||||
var tempFilePath = Path.Combine(Path.GetTempPath(), file.Id);
|
var tempFilePath = Path.Combine(Path.GetTempPath(), file.Id);
|
||||||
if (System.IO.File.Exists(tempFilePath))
|
if (System.IO.File.Exists(tempFilePath))
|
||||||
{
|
{
|
||||||
if (file.IsEncrypted)
|
if (file.IsEncrypted)
|
||||||
return StatusCode(StatusCodes.Status403Forbidden, "Encrypted files cannot be accessed before they are processed and stored.");
|
return Task.FromResult<ActionResult>(StatusCode(StatusCodes.Status403Forbidden,
|
||||||
|
"Encrypted files cannot be accessed before they are processed and stored."));
|
||||||
|
|
||||||
return PhysicalFile(tempFilePath, file.MimeType ?? "application/octet-stream", file.Name, enableRangeProcessing: true);
|
return Task.FromResult<ActionResult>(PhysicalFile(tempFilePath, file.MimeType ?? "application/octet-stream",
|
||||||
|
file.Name, enableRangeProcessing: true));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fallback for tus uploads
|
// Fallback for tus uploads
|
||||||
var tusStorePath = configuration.GetValue<string>("Storage:Uploads");
|
var tusStorePath = configuration.GetValue<string>("Storage:Uploads");
|
||||||
if (!string.IsNullOrEmpty(tusStorePath))
|
if (string.IsNullOrEmpty(tusStorePath))
|
||||||
{
|
return Task.FromResult<ActionResult>(StatusCode(StatusCodes.Status400BadRequest,
|
||||||
|
"File is being processed. Please try again later."));
|
||||||
var tusFilePath = Path.Combine(env.ContentRootPath, tusStorePath, file.Id);
|
var tusFilePath = Path.Combine(env.ContentRootPath, tusStorePath, file.Id);
|
||||||
if (System.IO.File.Exists(tusFilePath))
|
return System.IO.File.Exists(tusFilePath)
|
||||||
{
|
? Task.FromResult<ActionResult>(PhysicalFile(tusFilePath, file.MimeType ?? "application/octet-stream",
|
||||||
return PhysicalFile(tusFilePath, file.MimeType ?? "application/octet-stream", file.Name, enableRangeProcessing: true);
|
file.Name, enableRangeProcessing: true))
|
||||||
}
|
: Task.FromResult<ActionResult>(StatusCode(StatusCodes.Status400BadRequest,
|
||||||
}
|
"File is being processed. Please try again later."));
|
||||||
|
|
||||||
return StatusCode(StatusCodes.Status400BadRequest, "File is being processed. Please try again later.");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task<ActionResult> ServeRemoteFile(
|
private async Task<ActionResult> ServeRemoteFile(
|
||||||
@@ -99,7 +100,8 @@ public class FileController(
|
|||||||
)
|
)
|
||||||
{
|
{
|
||||||
if (!file.PoolId.HasValue)
|
if (!file.PoolId.HasValue)
|
||||||
return StatusCode(StatusCodes.Status500InternalServerError, "File is in an inconsistent state: uploaded but no pool ID.");
|
return StatusCode(StatusCodes.Status500InternalServerError,
|
||||||
|
"File is in an inconsistent state: uploaded but no pool ID.");
|
||||||
|
|
||||||
var pool = await fs.GetPoolAsync(file.PoolId.Value);
|
var pool = await fs.GetPoolAsync(file.PoolId.Value);
|
||||||
if (pool is null)
|
if (pool is null)
|
||||||
@@ -141,22 +143,17 @@ public class FileController(
|
|||||||
return fileName;
|
return fileName;
|
||||||
}
|
}
|
||||||
|
|
||||||
private ActionResult? TryProxyRedirect(SnCloudFile file, RemoteStorageConfig dest, string fileName)
|
public ActionResult? TryProxyRedirect(SnCloudFile file, RemoteStorageConfig dest, string fileName)
|
||||||
{
|
{
|
||||||
if (dest.ImageProxy is not null && (file.MimeType?.StartsWith("image/") ?? false))
|
if (dest.ImageProxy is not null && (file.MimeType?.StartsWith("image/") ?? false))
|
||||||
{
|
{
|
||||||
return Redirect(BuildProxyUrl(dest.ImageProxy, fileName));
|
return Redirect(BuildProxyUrl(dest.ImageProxy, fileName));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (dest.AccessProxy is not null)
|
return dest.AccessProxy is not null ? Redirect(BuildProxyUrl(dest.AccessProxy, fileName)) : null;
|
||||||
{
|
|
||||||
return Redirect(BuildProxyUrl(dest.AccessProxy, fileName));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return null;
|
private static string BuildProxyUrl(string proxyUrl, string fileName)
|
||||||
}
|
|
||||||
|
|
||||||
private string BuildProxyUrl(string proxyUrl, string fileName)
|
|
||||||
{
|
{
|
||||||
var baseUri = new Uri(proxyUrl.EndsWith('/') ? proxyUrl : $"{proxyUrl}/");
|
var baseUri = new Uri(proxyUrl.EndsWith('/') ? proxyUrl : $"{proxyUrl}/");
|
||||||
var fullUri = new Uri(baseUri, fileName);
|
var fullUri = new Uri(baseUri, fileName);
|
||||||
@@ -189,7 +186,7 @@ public class FileController(
|
|||||||
return Redirect(openUrl);
|
return Redirect(openUrl);
|
||||||
}
|
}
|
||||||
|
|
||||||
private Dictionary<string, string> BuildSignedUrlHeaders(
|
private static Dictionary<string, string> BuildSignedUrlHeaders(
|
||||||
SnCloudFile file,
|
SnCloudFile file,
|
||||||
string? fileExtension,
|
string? fileExtension,
|
||||||
string? overrideMimeType,
|
string? overrideMimeType,
|
||||||
|
|||||||
Reference in New Issue
Block a user