Files
Febe/src/Febe.Core/Library/LibraryLayout.cs
T
Claude 3aeaa17da7 M1 Dati e libreria: modello, archivi SQLite con scrittore unico, nomi dei file, scrittura sicura, miniature, impronte, _febe.jsonl, copie, eliminazione e ricostruzione
Modello del dominio e interfacce degli archivi in Febe.Core; implementazione SQLite in Febe.Data (WAL, chiavi esterne, migrazioni numerate in transazione, coda di scrittura su un solo thread); logica della libreria in Febe.Core/Library; 164 prove MSTest, compresa la ricostruzione del database da una libreria di prova e la scala a 200.000 elementi.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

Claude-Session: https://claude.ai/code/session_01Mzbb6zeFTjT6Rp8b92AVkA
2026-10-13 16:06:15 +00:00

134 lines
5.4 KiB
C#

using Febe.Core.Domain;
namespace Febe.Core.Library;
/// <summary>
/// Where things live in the library (spec §4.7, §8.2): one folder per account, file paths relative to it.
/// Relative paths are stored with '/' separators and converted to the platform separator here.
/// </summary>
public sealed class LibraryLayout
{
public LibraryLayout(string libraryRoot, int maxPathLength = WindowsFileNames.MaxPathLength)
{
ArgumentException.ThrowIfNullOrWhiteSpace(libraryRoot);
Root = Path.GetFullPath(libraryRoot);
MaxPathLength = maxPathLength;
}
/// <summary>Full path of the library folder.</summary>
public string Root { get; }
public int MaxPathLength { get; }
/// <summary>
/// Folder of an account: <see cref="Account.Folder"/> when absolute; relative to the library when relative;
/// otherwise <c>&lt;Libreria&gt;/&lt;username&gt;</c> (spec §4.4).
/// </summary>
public string AccountFolder(Account account)
{
ArgumentNullException.ThrowIfNull(account);
return AccountFolder(account.Folder, account.Username);
}
public string AccountFolder(string? folder, string username)
{
if (!string.IsNullOrWhiteSpace(folder))
{
return Path.IsPathFullyQualified(folder)
? Path.GetFullPath(folder)
: Path.GetFullPath(Path.Combine(Root, ToPlatform(folder)));
}
return Path.Combine(Root, WindowsFileNames.SanitizeSegment(username));
}
/// <summary>
/// The value to store in <see cref="Account.Folder"/> for a folder chosen or found on disk: null for the default
/// folder of that user name, a relative path inside the library, an absolute path outside.
/// </summary>
public string? FolderSetting(string folder, string username)
{
ArgumentException.ThrowIfNullOrWhiteSpace(folder);
var full = Path.TrimEndingDirectorySeparator(Path.GetFullPath(folder));
if (PathsEqual(full, Path.Combine(Root, WindowsFileNames.SanitizeSegment(username))))
{
return null;
}
var relative = Path.GetRelativePath(Root, full);
return relative.StartsWith("..", StringComparison.Ordinal) || Path.IsPathFullyQualified(relative)
? full
: relative.Replace(Path.DirectorySeparatorChar, '/');
}
/// <summary>Full path of a file of an account from its stored relative path.</summary>
public string FullPath(Account account, string relativePath) => FullPath(AccountFolder(account), relativePath);
public static string FullPath(string accountFolder, string relativePath)
{
ArgumentNullException.ThrowIfNull(accountFolder);
ArgumentException.ThrowIfNullOrWhiteSpace(relativePath);
var full = Path.GetFullPath(Path.Combine(accountFolder, ToPlatform(relativePath)));
var root = Path.TrimEndingDirectorySeparator(Path.GetFullPath(accountFolder)) + Path.DirectorySeparatorChar;
if (!full.StartsWith(root, OperatingSystem.IsWindows() ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal))
{
throw new ArgumentException("Il percorso esce dalla cartella dell'account.", nameof(relativePath));
}
return full;
}
/// <summary>
/// The relative path (with '/') of a new file, rendered with the naming scheme and fitted to the path length limit
/// inside the account folder.
/// </summary>
public string RelativePathFor(string accountFolder, NamingScheme scheme, NamingValues values, TimeZoneInfo zone)
{
ArgumentNullException.ThrowIfNull(scheme);
var segments = scheme.Render(values, zone);
return string.Join('/', WindowsFileNames.FitPath(accountFolder, segments, MaxPathLength));
}
/// <summary>'/'-separated relative path → platform separators.</summary>
public static string ToPlatform(string relativePath) =>
relativePath.Replace('\\', '/').Replace('/', Path.DirectorySeparatorChar);
/// <summary>Platform relative path → '/'-separated (the stored form).</summary>
public static string ToStored(string relativePath) => relativePath.Replace('\\', '/');
private static bool PathsEqual(string a, string b) => string.Equals(
Path.TrimEndingDirectorySeparator(a),
Path.TrimEndingDirectorySeparator(b),
OperatingSystem.IsWindows() ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal);
}
/// <summary>The Data folder (spec §8.2): database, browser profile, thumbnails, backups, logs.</summary>
public sealed record DataFolderLayout
{
public DataFolderLayout(string root)
{
ArgumentException.ThrowIfNullOrWhiteSpace(root);
Root = Path.GetFullPath(root);
}
public string Root { get; }
/// <summary>Dati/febe.sqlite.</summary>
public string Database => Path.Combine(Root, "febe.sqlite");
/// <summary>Dati/browser: holds the Instagram session.</summary>
public string BrowserProfile => Path.Combine(Root, "browser");
/// <summary>Dati/miniature.</summary>
public string Thumbnails => Path.Combine(Root, "miniature");
/// <summary>Dati/copie: daily database backups.</summary>
public string Backups => Path.Combine(Root, "copie");
/// <summary>Dati/registro: rotating logs.</summary>
public string Logs => Path.Combine(Root, "registro");
/// <summary>Dati/segnali.json.</summary>
public string SignalsFile => Path.Combine(Root, "segnali.json");
}