Files
Febe/src/Febe.Core/Library/LibraryLayout.cs
T
Alby96andClaude Opus 5.5 badd1681a0
verifica / controlli (push) Successful in 18s
verifica / prove (push) Successful in 13m5s
verifica / installatore (push) Successful in 4m11s
verifica / immagine (push) Successful in 14m4s
Rapporto di diagnostica della raccolta (#39)
Dopo una prova dal vivo andata male, Alberto deve poter mandare un solo file che
spieghi perché, senza che nessuno veda dati di altre persone. Attività ha ora il
pulsante «Diagnostica», che scarica il rapporto delle sessioni degli ultimi 14 giorni:
pagine aperte e forma degli indirizzi, risposte per tipo, stato e dimensione con ciò
che i lettori ne hanno fatto, struttura stretta delle forme sconosciute, ricette,
segnali, permessi negati, file in coda e scaricati, e il motivo di ogni arresto.

Nel rapporto entrano solo valori da elenchi chiusi, numeri, orari e forme: mai nomi,
indirizzi completi, codici, cursori, cookie o pezzi di risposte. Le forme degli
indirizzi e la regola delle chiavi-dato passano dalla sonda a Core, e la sonda le
richiama. Il registratore non lancia mai eccezioni verso la raccolta e tiene i file in
Dati/diagnostica per 14 giorni.

Prova che il rapporto non contiene valori, con controlli di mutazione; tre giri di
revisione avversaria (25 difetti confermati, tutti corretti).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-13 14:41:34 +01:00

137 lines
5.6 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/diagnostica: the traces of the diagnostic report, two weeks (issue #39).</summary>
public string Diagnostics => Path.Combine(Root, "diagnostica");
/// <summary>Dati/segnali.json.</summary>
public string SignalsFile => Path.Combine(Root, "segnali.json");
}