♻️ Proper folder system to index

This commit is contained in:
2025-11-14 01:03:59 +08:00
parent b137021b1f
commit 1647aa2f1e
11 changed files with 2334 additions and 127 deletions

View File

@@ -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
};
}
}

View File

@@ -0,0 +1,103 @@
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using Microsoft.EntityFrameworkCore;
namespace DysonNetwork.Shared.Models;
[Index(nameof(Path), nameof(AccountId))]
[Index(nameof(ParentFolderId))]
public class SnCloudFolder : ModelBase
{
public Guid Id { get; set; } = Guid.NewGuid();
/// <summary>
/// The name of the folder
/// </summary>
[MaxLength(256)]
public string Name { get; init; } = null!;
/// <summary>
/// The full path of the folder (for querying purposes)
/// With trailing slash, e.g., /documents/work/
/// </summary>
[MaxLength(8192)]
public string Path { get; set; } = null!;
/// <summary>
/// Reference to the parent folder (null for root folders)
/// </summary>
public Guid? ParentFolderId { get; set; }
/// <summary>
/// Navigation property to the parent folder
/// </summary>
[ForeignKey(nameof(ParentFolderId))]
public SnCloudFolder? ParentFolder { get; init; }
/// <summary>
/// Navigation property to child folders
/// </summary>
[InverseProperty(nameof(ParentFolder))]
public List<SnCloudFolder> ChildFolders { get; init; } = [];
/// <summary>
/// Navigation property to files in this folder
/// </summary>
[InverseProperty(nameof(SnCloudFileIndex.Folder))]
public List<SnCloudFileIndex> Files { get; init; } = [];
/// <summary>
/// The account that owns this folder
/// </summary>
public Guid AccountId { get; init; }
/// <summary>
/// Navigation property to the account
/// </summary>
[NotMapped]
public SnAccount? Account { get; init; }
/// <summary>
/// Optional description for the folder
/// </summary>
[MaxLength(4096)]
public string? Description { get; init; }
/// <summary>
/// Custom metadata for the folder
/// </summary>
[Column(TypeName = "jsonb")]
public Dictionary<string, object?>? FolderMeta { get; init; }
/// <summary>
/// Creates a new folder with proper path normalization
/// </summary>
public static SnCloudFolder Create(string name, Guid accountId, SnCloudFolder? parentFolder = null)
{
var normalizedPath = parentFolder != null
? $"{parentFolder.Path.TrimEnd('/')}/{name}/"
: $"/{name}/";
return new SnCloudFolder
{
Name = name,
Path = normalizedPath,
ParentFolderId = parentFolder?.Id,
AccountId = accountId
};
}
/// <summary>
/// Creates the root folder for an account
/// </summary>
public static SnCloudFolder CreateRoot(Guid accountId)
{
return new SnCloudFolder
{
Name = "Root",
Path = "/",
ParentFolderId = null,
AccountId = accountId
};
}
}