♻️ Proper folder system to index
This commit is contained in:
@@ -1,30 +1,70 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
using System.Text.Json.Serialization;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace DysonNetwork.Shared.Models;
|
||||
|
||||
[Index(nameof(Path), nameof(AccountId))]
|
||||
[Index(nameof(FolderId), nameof(AccountId))]
|
||||
[Index(nameof(FileId))]
|
||||
public class SnCloudFileIndex : ModelBase
|
||||
{
|
||||
public Guid Id { get; init; } = Guid.NewGuid();
|
||||
public Guid Id { get; set; } = Guid.NewGuid();
|
||||
|
||||
/// <summary>
|
||||
/// The path of the file,
|
||||
/// only store the parent folder.
|
||||
/// With the trailing slash.
|
||||
///
|
||||
/// Like /hello/here/ not /hello/here/text.txt or /hello/here
|
||||
/// Or the user home folder files, store as /
|
||||
///
|
||||
/// Besides, the folder name should be all query-safe, not contains % or
|
||||
/// other special characters that will mess up the pgsql query
|
||||
/// Reference to the folder containing this file
|
||||
/// </summary>
|
||||
[MaxLength(8192)]
|
||||
public string Path { get; init; } = null!;
|
||||
public Guid FolderId { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// Navigation property to the folder
|
||||
/// </summary>
|
||||
[JsonIgnore]
|
||||
public SnCloudFolder Folder { get; init; } = null!;
|
||||
|
||||
[MaxLength(32)] public string FileId { get; init; } = null!;
|
||||
public SnCloudFile File { get; init; } = null!;
|
||||
public Guid AccountId { get; init; }
|
||||
[NotMapped] public SnAccount? Account { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// Cached full path of the file (stored in database for performance)
|
||||
/// </summary>
|
||||
[MaxLength(2048)]
|
||||
public string Path { get; set; } = null!;
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new file index with the specified folder and file
|
||||
/// </summary>
|
||||
public static SnCloudFileIndex Create(SnCloudFolder folder, SnCloudFile file, Guid accountId)
|
||||
{
|
||||
// Build the full path by traversing the folder hierarchy
|
||||
var pathSegments = new List<string>();
|
||||
var currentFolder = folder;
|
||||
|
||||
while (currentFolder != null)
|
||||
{
|
||||
if (!string.IsNullOrEmpty(currentFolder.Name))
|
||||
{
|
||||
pathSegments.Insert(0, currentFolder.Name);
|
||||
}
|
||||
currentFolder = currentFolder.ParentFolder;
|
||||
}
|
||||
|
||||
// Add the file name
|
||||
if (!string.IsNullOrEmpty(file.Name))
|
||||
{
|
||||
pathSegments.Add(file.Name);
|
||||
}
|
||||
|
||||
var fullPath = "/" + string.Join("/", pathSegments);
|
||||
|
||||
return new SnCloudFileIndex
|
||||
{
|
||||
FolderId = folder.Id,
|
||||
FileId = file.Id,
|
||||
AccountId = accountId,
|
||||
Path = fullPath
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user