using Febe.Core.Domain;
namespace Febe.Core.Library;
///
/// 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.
///
public sealed class LibraryLayout
{
public LibraryLayout(string libraryRoot, int maxPathLength = WindowsFileNames.MaxPathLength)
{
ArgumentException.ThrowIfNullOrWhiteSpace(libraryRoot);
Root = Path.GetFullPath(libraryRoot);
MaxPathLength = maxPathLength;
}
/// Full path of the library folder.
public string Root { get; }
public int MaxPathLength { get; }
///
/// Folder of an account: when absolute; relative to the library when relative;
/// otherwise <Libreria>/<username> (spec §4.4).
///
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));
}
///
/// The value to store in 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.
///
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, '/');
}
/// Full path of a file of an account from its stored relative path.
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;
}
///
/// The relative path (with '/') of a new file, rendered with the naming scheme and fitted to the path length limit
/// inside the account folder.
///
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));
}
/// '/'-separated relative path → platform separators.
public static string ToPlatform(string relativePath) =>
relativePath.Replace('\\', '/').Replace('/', Path.DirectorySeparatorChar);
/// Platform relative path → '/'-separated (the stored form).
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);
}
/// The Data folder (spec §8.2): database, browser profile, thumbnails, backups, logs.
public sealed record DataFolderLayout
{
public DataFolderLayout(string root)
{
ArgumentException.ThrowIfNullOrWhiteSpace(root);
Root = Path.GetFullPath(root);
}
public string Root { get; }
/// Dati/febe.sqlite.
public string Database => Path.Combine(Root, "febe.sqlite");
/// Dati/browser: holds the Instagram session.
public string BrowserProfile => Path.Combine(Root, "browser");
/// Dati/miniature.
public string Thumbnails => Path.Combine(Root, "miniature");
/// Dati/copie: daily database backups.
public string Backups => Path.Combine(Root, "copie");
/// Dati/registro: rotating logs.
public string Logs => Path.Combine(Root, "registro");
/// Dati/segnali.json.
public string SignalsFile => Path.Combine(Root, "segnali.json");
}