Chronos Server for Windows is replaced by Settings > Server > Share this library: Chronos Desktop hosts the server engine in its own process over its own index, archive and processing services, job engine and scheduler, so a second PC, the Uploader and render nodes use this PC's library without a Windows service or a second index. - Chronos.Server is a library; Chronos.Server.Host is the console exe that Docker and development runs start. The App references the library and the ASP.NET Core shared framework (no package). - ServerHost.BuildHosted/HostedServer: reuses the desktop's engines and never creates, starts or disposes them; no static log sink, no console lifetime; server.json plus desktop-owned values locked "Set in Chronos Desktop"; only upload-cleanup and audit-cleanup schedules are added. - Both modes: by-id endpoints scoped to the served archive, a client's render export folder dropped; hosted mode exposes server job kinds only. - Desktop: LibraryShare (start after the schedules, stop first, port in use named), Settings > Server sharing rows, Server > This library panel (state, addresses with Copy, ports, fingerprint, temporary admin password, clients, admin panel), own library refused by ConnectFlow and dropped from LAN discovery, the uploads sweep step, thumbnails after scans while sharing, capture scenes server-share and server-share-failed. - Removed: the Windows service hosting of Chronos.Server, its switches and its tests. Refs #67 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
150 lines
7.6 KiB
C#
150 lines
7.6 KiB
C#
using System.IO;
|
|
using Chronos.Core.Windows.Shell;
|
|
|
|
namespace Chronos.App.Infrastructure;
|
|
|
|
/// <summary>
|
|
/// Where the client keeps its data (BRIEF §6.2, <see cref="AppPaths"/>): <c>%LOCALAPPDATA%\Chronos</c>, or <c>.\data</c>
|
|
/// next to the executable when <c>portable.txt</c> exists. Never inside a folder OneDrive synchronises (SQLite corrupts
|
|
/// there): a portable copy in one keeps its data in <c>%LOCALAPPDATA%\Chronos</c>, and Settings tells the user so
|
|
/// (<see cref="PortableSyncRoot"/>, <see cref="EarlierPortableData"/>). When that folder itself lies in one, nowhere
|
|
/// is safe: Chronos starts all the same and Settings warns (<see cref="SyncedFolder"/>).
|
|
/// </summary>
|
|
public sealed class AppEnvironment
|
|
{
|
|
private AppEnvironment(string dataDirectory, bool portable, bool isCapture, string? syncedFolder = null, string? portableSyncRoot = null,
|
|
string? earlierPortableData = null)
|
|
{
|
|
DataDirectory = dataDirectory;
|
|
IsPortable = portable;
|
|
IsCapture = isCapture;
|
|
SyncedFolder = syncedFolder;
|
|
PortableSyncRoot = portableSyncRoot;
|
|
EarlierPortableData = earlierPortableData;
|
|
Directory.CreateDirectory(DataDirectory);
|
|
Directory.CreateDirectory(LogDirectory);
|
|
Directory.CreateDirectory(CacheDirectory);
|
|
}
|
|
|
|
public string DataDirectory { get; }
|
|
public bool IsPortable { get; }
|
|
|
|
/// <summary>A throw-away environment for <c>--capture</c>: nothing of the user's data is read or written.</summary>
|
|
public bool IsCapture { get; }
|
|
|
|
/// <summary>The OneDrive folder <see cref="DataDirectory"/> itself lies in, where the index may be damaged; null when it is safe.</summary>
|
|
public string? SyncedFolder { get; }
|
|
|
|
/// <summary>The OneDrive folder this portable copy runs from; its data is in <c>%LOCALAPPDATA%\Chronos</c> instead of <c>.\data</c>. Null otherwise.</summary>
|
|
public string? PortableSyncRoot { get; }
|
|
|
|
/// <summary>The <c>.\data</c> folder this portable copy used before it ran from OneDrive, left as it was; null when there is none.</summary>
|
|
public string? EarlierPortableData { get; }
|
|
|
|
public string LogDirectory => Path.Combine(DataDirectory, "logs");
|
|
public string CacheDirectory => Path.Combine(DataDirectory, "cache");
|
|
public string BackupDirectory => Path.Combine(DataDirectory, "backups");
|
|
|
|
/// <summary>The private folder of the server this PC runs while it shares its library: certificate, settings, uploads, logs.</summary>
|
|
public string ShareDirectory => Path.Combine(DataDirectory, "server");
|
|
|
|
/// <summary>The PC's name as the screens show it: a capture shows <see cref="SamplePcName"/>.</summary>
|
|
public string PcName => IsCapture ? SamplePcName : Environment.MachineName;
|
|
|
|
/// <summary>The PC name captures show.</summary>
|
|
public const string SamplePcName = "STUDIO-PC";
|
|
|
|
public string DatabasePath => Path.Combine(DataDirectory, AppPaths.DatabaseFileName);
|
|
public string SettingsPath => Path.Combine(DataDirectory, AppPaths.SettingsFileName);
|
|
|
|
/// <summary>The environment of this process: its executable's folder and the user's OneDrive folders decide.</summary>
|
|
public static AppEnvironment Detect() => From(AppPaths.Resolve());
|
|
|
|
/// <summary>The environment at <paramref name="paths"/> (its folders are created).</summary>
|
|
public static AppEnvironment From(AppPaths paths)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(paths);
|
|
return new(paths.DataRoot, paths.IsPortable, isCapture: false, paths.SyncedFolder, paths.PortableSyncRoot, paths.EarlierPortableData);
|
|
}
|
|
|
|
/// <summary>
|
|
/// The data folder a capture shows in place of its temporary one (<see cref="Shown"/>): the default data folder of a
|
|
/// sample user, so the screenshots show no path of the machine (its temporary folder, its user name).
|
|
/// </summary>
|
|
public const string SampleDataDirectory = @"C:\Users\Studio\AppData\Local\Chronos";
|
|
|
|
/// <summary>The folder a capture shows for its sample archive (<c>archive</c> in its data folder), as the sample settings name it.</summary>
|
|
public const string SampleArchiveDirectory = @"D:\ChronosArchive";
|
|
|
|
/// <summary>The temporary data folder of the capture this process runs (the last one made); null outside a capture.</summary>
|
|
private static string? _captureData;
|
|
|
|
/// <summary>A fresh temporary data folder for screenshots and smoke tests; delete it with <see cref="DeleteCaptureData"/>.</summary>
|
|
public static AppEnvironment ForCapture()
|
|
{
|
|
var environment = new AppEnvironment(Path.Combine(Path.GetTempPath(), "chronos-capture-" + Guid.CreateVersion7().ToString("N")), portable: false, isCapture: true);
|
|
_captureData = environment.DataDirectory;
|
|
return environment;
|
|
}
|
|
|
|
/// <summary>
|
|
/// A path as the screens show it: one under a capture's temporary data folder as under <see cref="SampleDataDirectory"/>
|
|
/// (its sample archive as <see cref="SampleArchiveDirectory"/>), so the screenshots show nothing of the machine; any
|
|
/// other as it is. Only for text on screen: a path typed there goes through <see cref="Actual"/>.
|
|
/// </summary>
|
|
public static string Shown(string path) => Shown(path, _captureData);
|
|
|
|
/// <summary>A path typed or shown on screen as the one it stands for: <see cref="Shown(string)"/> undone, so a capture never writes outside its temporary folder.</summary>
|
|
public static string Actual(string shown) => Actual(shown, _captureData);
|
|
|
|
/// <summary><see cref="Shown(string)"/> for the capture whose data folder is <paramref name="captureData"/> (none: the path as it is).</summary>
|
|
internal static string Shown(string path, string? captureData)
|
|
{
|
|
foreach (var (actual, shown) in CaptureFolders(captureData))
|
|
{
|
|
if (Swap(path, actual, shown) is { } swapped) return swapped;
|
|
}
|
|
return path;
|
|
}
|
|
|
|
/// <summary><see cref="Actual(string)"/> for the capture whose data folder is <paramref name="captureData"/>.</summary>
|
|
internal static string Actual(string shown, string? captureData)
|
|
{
|
|
foreach (var (actual, sample) in CaptureFolders(captureData))
|
|
{
|
|
if (Swap(shown, sample, actual) is { } swapped) return swapped;
|
|
}
|
|
return shown;
|
|
}
|
|
|
|
/// <summary>A capture's folders and what the screens show for each, the more specific first.</summary>
|
|
private static (string Actual, string Shown)[] CaptureFolders(string? captureData) =>
|
|
captureData is null ? [] : [(Path.Combine(captureData, "archive"), SampleArchiveDirectory), (captureData, SampleDataDirectory)];
|
|
|
|
/// <summary><paramref name="path"/> with its root <paramref name="from"/> replaced by <paramref name="to"/>; null when it is not under it.</summary>
|
|
private static string? Swap(string path, string from, string to)
|
|
{
|
|
if (string.IsNullOrEmpty(path)) return null;
|
|
var root = from.TrimEnd(Path.DirectorySeparatorChar);
|
|
if (path.TrimEnd(Path.DirectorySeparatorChar).Equals(root, StringComparison.OrdinalIgnoreCase)) return to;
|
|
return path.StartsWith(root + Path.DirectorySeparatorChar, StringComparison.OrdinalIgnoreCase) ? to + path[root.Length..] : null;
|
|
}
|
|
|
|
/// <summary>Removes the temporary data folder of a capture environment (never a real one).</summary>
|
|
public void DeleteCaptureData()
|
|
{
|
|
if (!IsCapture) return;
|
|
try
|
|
{
|
|
Directory.Delete(DataDirectory, recursive: true);
|
|
}
|
|
catch (IOException)
|
|
{
|
|
// Files still open (log, database) are left to the OS temp cleanup.
|
|
}
|
|
catch (UnauthorizedAccessException)
|
|
{
|
|
}
|
|
}
|
|
}
|