diff --git a/src/Chronos.App/Sections/SequenceView.xaml b/src/Chronos.App/Sections/SequenceView.xaml
index 10212366..5a6eb10f 100644
--- a/src/Chronos.App/Sections/SequenceView.xaml
+++ b/src/Chronos.App/Sections/SequenceView.xaml
@@ -127,6 +127,10 @@
+
+
diff --git a/src/Chronos.App/Sections/SequenceViewModel.cs b/src/Chronos.App/Sections/SequenceViewModel.cs
index dbc17373..568222dd 100644
--- a/src/Chronos.App/Sections/SequenceViewModel.cs
+++ b/src/Chronos.App/Sections/SequenceViewModel.cs
@@ -608,12 +608,20 @@ public sealed class SequenceViewModel : SectionViewModel
ImageDecoder decoder, Sequence row)
{
var request = TakeRequest(row.Id);
- var entry = request switch
+ WorkspaceEntry? entry;
+ if (request is { ProjectId: { } id }) entry = await workspace.FindAsync(id);
+ else if (request is { Main: true }) entry = (await workspace.ProjectsOfAsync(row.Id)).FirstOrDefault(p => p.IsMain);
+ else
{
- { ProjectId: { } id } => await workspace.FindAsync(id),
- { Main: true } => (await workspace.ProjectsOfAsync(row.Id)).FirstOrDefault(p => p.IsMain),
- _ => await workspace.LastUsedAsync(row.Id),
- };
+ var (cancelled, chosen) = await PolicyEntryAsync(services, workspace, row.Id, row.DisplayTitle, _ => true);
+ if (cancelled)
+ {
+ _afterOpen = null;
+ Shell.KeepSequence(OpenSequenceId);
+ return null;
+ }
+ entry = chosen;
+ }
// This Chronos cannot read its working state (damaged, or a newer Chronos's): nothing is checked out over it.
if (entry is { IsReadable: false } unreadable) throw WorkspaceHost.Unreadable(unreadable);
// No project of the sequence here opens, and its Main here is such a copy (never among its projects): said at once,
@@ -634,10 +642,12 @@ public sealed class SequenceViewModel : SectionViewModel
if (entry is { HasMedia: true })
{
workspace.Remember(row.Id, entry.ProjectId);
+ workspace.RecordOpened(entry.ProjectId);
return (new WorkspaceSequenceSource(workspace.Sync, entry.ProjectId, db, decoder, services.Engine), row.DisplayTitle);
}
var projectId = entry?.ProjectId ?? request?.ProjectId;
string? newName = null;
+ var media = DefaultMedia(services);
if (entry is null && projectId is { } wanted && (await workspace.HomeProjectsAsync(row.Id)).All(p => p.Id != wanted))
{
// A project file of a project this home does not have (another PC's workspace): its sequence's Main takes it.
@@ -653,12 +663,12 @@ public sealed class SequenceViewModel : SectionViewModel
Shell.KeepSequence(OpenSequenceId);
return null;
}
- (projectId, newName) = (choice.ProjectId, choice.NewName);
+ (projectId, newName, media) = (choice.ProjectId, choice.NewName, choice.Media);
}
WorkspaceCheckout checkout;
try
{
- checkout = await workspace.CheckoutAsync(row.Id, projectId, newName);
+ checkout = await workspace.CheckoutAsync(row.Id, projectId, newName, media);
}
catch (WorkspaceException ex) when (ex.FolderRefused)
{
@@ -710,12 +720,20 @@ public sealed class SequenceViewModel : SectionViewModel
private async Task OpenServerWorkspaceAsync(AppServices services, WorkspaceHost workspace, ChronosDatabase db, ImageDecoder decoder, Guid remoteId)
{
var request = _requested is { } asked && asked.SequenceId == remoteId ? asked : null;
- var entry = request switch
+ WorkspaceEntry? entry;
+ if (request is { ProjectId: { } id }) entry = await workspace.FindAsync(id) is { IsServerHome: true } found && found.SequenceId == remoteId ? found : null;
+ else if (request is { Main: true }) entry = (await workspace.ProjectsOfAsync(remoteId)).FirstOrDefault(p => p is { IsMain: true, IsServerHome: true });
+ else
{
- { ProjectId: { } id } => await workspace.FindAsync(id) is { IsServerHome: true } found && found.SequenceId == remoteId ? found : null,
- { Main: true } => (await workspace.ProjectsOfAsync(remoteId)).FirstOrDefault(p => p is { IsMain: true, IsServerHome: true }),
- _ => await workspace.LastUsedAsync(remoteId) is { IsServerHome: true } last ? last : null,
- };
+ var (cancelled, chosen) = await PolicyEntryAsync(services, workspace, remoteId, null, p => p.IsServerHome);
+ if (cancelled)
+ {
+ _afterOpen = null;
+ Shell.KeepSequence(OpenSequenceId);
+ return new ServerOpen(null);
+ }
+ entry = chosen;
+ }
var sync = services.Sync;
if (entry is { IsReadable: false } unreadable)
{
@@ -726,6 +744,7 @@ public sealed class SequenceViewModel : SectionViewModel
{
TakeRequest(remoteId);
workspace.Remember(remoteId, entry.ProjectId);
+ workspace.RecordOpened(entry.ProjectId);
// Its analyses and renders travel on a connection of the signed-in session, when that is the project's server:
// made again whenever the sign-in changes, so a project opened signed out renders once signed in.
var server = entry.Home.Server;
@@ -755,6 +774,7 @@ public sealed class SequenceViewModel : SectionViewModel
}
var projectId = entry?.ProjectId ?? request?.ProjectId;
string? newName = null;
+ var media = DefaultMedia(services);
if (entry is null && projectId is { } wanted && (await workspace.HomeProjectsAsync(home)).All(p => p.Id != wanted))
{
// A project the server does not have (deleted there, or another server's): the sequence's Main opens instead.
@@ -770,12 +790,12 @@ public sealed class SequenceViewModel : SectionViewModel
Shell.KeepSequence(OpenSequenceId);
return new ServerOpen(null);
}
- (projectId, newName) = (choice.ProjectId, choice.NewName);
+ (projectId, newName, media) = (choice.ProjectId, choice.NewName, choice.Media);
}
WorkspaceCheckout checkout;
try
{
- checkout = await workspace.CheckoutAsync(home, projectId, newName);
+ checkout = await workspace.CheckoutAsync(home, projectId, newName, media);
}
catch (WorkspaceException ex) when (ex.FolderRefused)
{
@@ -811,25 +831,23 @@ public sealed class SequenceViewModel : SectionViewModel
///
/// "Open in workspace" for a sequence of (this PC's archive, or a server): its projects, the
- /// media, and for this PC's archive what their copy writes and the room the workspace's drive has; null when closed
- /// without a choice. , the copy of Main here this Chronos cannot read, is listed
+ /// media (Settings › Workspace › Media chosen first), what their copy writes and the room the workspace's drive has; null
+ /// when closed without a choice. , the copy of Main here this Chronos cannot read, is listed
/// disabled, why in its tooltip.
///
private async Task AskProjectAsync(WorkspaceHost workspace, BundleHome home, string title, WorkspaceEntry? unreadableMain)
{
var projects = await workspace.HomeProjectsAsync(home);
WorkspaceCopyFacts? copy = null;
- if (home.IsArchive)
+ try
{
- try
- {
- copy = await workspace.CopyFactsAsync(home.SequenceId);
- }
- catch (Exception ex) when (ex is System.IO.IOException or System.IO.InvalidDataException or InvalidOperationException or DatabaseException or UnauthorizedAccessException)
- {
- // Only shown: the copy measures again before it writes.
- Log.Info("workspace", $"The copy of sequence {home.SequenceId} could not be measured: {ex.Message}");
- }
+ copy = await workspace.CopyFactsAsync(home);
+ }
+ catch (Exception ex) when (ex is System.IO.IOException or System.IO.InvalidDataException or InvalidOperationException or DatabaseException
+ or UnauthorizedAccessException or ChronosApiException or System.Net.Http.HttpRequestException)
+ {
+ // Only shown: the copy measures again before it writes.
+ Log.Info("workspace", $"The copy of sequence {home.SequenceId} could not be measured: {ex.Message}");
}
var unreadable = unreadableMain is { } main
? new Dictionary
@@ -837,7 +855,8 @@ public sealed class SequenceViewModel : SectionViewModel
[main.ProjectId] = Loc.L(main.Newer ? "Tooltips.Editor.Workspace.State.NewerCopy" : "Tooltips.Editor.Workspace.State.DamagedCopy"),
}
: null;
- var dialog = new OpenInWorkspaceDialogViewModel(title, projects, d => CloseDialog(d), copy: copy, unreadable: unreadable, server: !home.IsArchive);
+ var dialog = new OpenInWorkspaceDialogViewModel(title, projects, d => CloseDialog(d), copy: copy, unreadable: unreadable, server: !home.IsArchive,
+ defaultMedia: DefaultMedia(Shell.Services));
// The picker or the open sequence shows behind the dialog meanwhile, not the progress bar.
IsLoading = false;
OpenDialog(dialog);
@@ -851,6 +870,37 @@ public sealed class SequenceViewModel : SectionViewModel
}
}
+ /// The media a checkout copies unless "Open in workspace" chose: Settings › Workspace › Media.
+ private static string DefaultMedia(AppServices services) =>
+ services.Settings.WorkspaceDefaultMedia == BundleMedia.Originals ? BundleMedia.Originals : BundleMedia.Proxies;
+
+ ///
+ /// The project of the sequence Edit opens when none was asked for (phase 5c, Settings › Workspace › Open project): the
+ /// one opened last, Main, or the one chosen in "Open which project?" when the workspace holds several (Cancelled: closed
+ /// without a choice). Only projects takes count; none here: null.
+ ///
+ private async Task<(bool Cancelled, WorkspaceEntry? Entry)> PolicyEntryAsync(AppServices services, WorkspaceHost workspace, Guid sequenceId, string? title,
+ Func fits)
+ {
+ var last = await workspace.LastUsedAsync(sequenceId) is { } used && fits(used) ? used : null;
+ var policy = services.Settings.WorkspaceOpenProject;
+ if (policy == WorkspaceOpenPolicy.LastUsed) return (false, last);
+ var here = (await workspace.ProjectsOfAsync(sequenceId)).Where(p => fits(p) && p.IsReadable).ToList();
+ if (policy == WorkspaceOpenPolicy.Main) return (false, here.FirstOrDefault(p => p.IsMain) ?? last);
+ if (here.Count < 2) return (false, last ?? here.FirstOrDefault());
+ var dialog = new OpenProjectDialogViewModel(title ?? here[0].SequenceTitle, here, last?.ProjectId, d => CloseDialog(d));
+ IsLoading = false;
+ OpenDialog(dialog);
+ try
+ {
+ return await dialog.Choice is { } chosen ? (false, chosen) : (true, null);
+ }
+ finally
+ {
+ IsLoading = true;
+ }
+ }
+
/// The project the next open of takes, taken once; null when none was asked for.
private ProjectRequest? TakeRequest(Guid sequenceId)
{
diff --git a/src/Chronos.App/Workspace/ProjectMenuViewModel.cs b/src/Chronos.App/Workspace/ProjectMenuViewModel.cs
index 3a38bec6..318a7498 100644
--- a/src/Chronos.App/Workspace/ProjectMenuViewModel.cs
+++ b/src/Chronos.App/Workspace/ProjectMenuViewModel.cs
@@ -81,6 +81,8 @@ public sealed class ProjectMenuViewModel : LocalizedViewModel
private bool _isSyncing;
private bool _homeFramesChanged;
private Guid _copyJob;
+ private bool _copyingOriginals;
+ private bool _changingMedia;
private bool _disposed;
/// Counts the editor's changes, so only the last one's state read applies.
@@ -110,6 +112,8 @@ public sealed class ProjectMenuViewModel : LocalizedViewModel
RenameCommand = new AsyncRelayCommand(OpenRenameAsync);
DeleteCommand = new AsyncRelayCommand(DeleteAsync, () => !IsMain);
RemoveCopyCommand = new AsyncRelayCommand(OpenRemoveCopyAsync);
+ CopyOriginalsCommand = new AsyncRelayCommand(CopyOriginalsAsync, () => !HoldsOriginals && !IsMediaBusy);
+ ProxiesOnlyCommand = new AsyncRelayCommand(ProxiesOnlyAsync, () => HoldsOriginals && !IsMediaBusy);
source.JobChanged += OnJobChanged;
session.ProjectChanged += OnProjectChanged;
host.HomeChanged += OnHomeChanged;
@@ -193,6 +197,27 @@ public sealed class ProjectMenuViewModel : LocalizedViewModel
public ICommand UpdateMediaCommand => _updateMedia;
+ /// The workspace holds the project's originals besides its proxies (phase 5c): renders run on this PC.
+ public bool HoldsOriginals => _source.Workspace?.Media.Kind == BundleMedia.Originals;
+
+ ///
+ /// The media cannot change now: changes not saved as a version, a sync or a copy of the media running (the jobs of the
+ /// sequence are asked when the command runs).
+ ///
+ public bool IsMediaBusy => _changingMedia || _isSyncing || _copyJob != Guid.Empty || _status is { Modified: true, ReadOnly: false };
+
+ /// Copy originals… (phase 5c): the project's originals into the workspace, to render on this PC.
+ public ICommand CopyOriginalsCommand { get; }
+
+ /// Proxies only… (phase 5c): the originals copied here deleted once the home is checked to hold each one.
+ public ICommand ProxiesOnlyCommand { get; }
+
+ public string CopyOriginalsToolTip => IsMediaBusy ? Loc.L("Tooltips.Editor.Project.MediaBusy")
+ : WorkspaceText.ForHome("Tooltips.Editor.Project.CopyOriginals", IsServerHome);
+
+ public string ProxiesOnlyToolTip => IsMediaBusy ? Loc.L("Tooltips.Editor.Project.MediaBusy")
+ : WorkspaceText.ForHome("Tooltips.Editor.Project.ProxiesOnly", IsServerHome);
+
/// Reads where the project stands (against its home) and whether the home's frames moved.
public async Task RefreshAsync()
{
@@ -644,6 +669,130 @@ public sealed class ProjectMenuViewModel : LocalizedViewModel
}
}
+ ///
+ /// Copy originals…: refused while the media cannot change (, or a job of the sequence waits or
+ /// runs); otherwise the copy is queued, the banner says it runs, and the editor reads the media again once it is done.
+ ///
+ private async Task CopyOriginalsAsync()
+ {
+ try
+ {
+ await StoreEditAsync();
+ if (await MediaRefusalAsync() is { } refusal)
+ {
+ _ui.Notify(Loc.L("Editor.Workspace.CopyFailed"), refusal, isError: true);
+ return;
+ }
+ var job = await _host.QueueOriginalsCopyAsync(ProjectId);
+ _copyJob = job.Id;
+ _copyingOriginals = true;
+ RaiseState();
+ _ui.Notify(Loc.L("Editor.Workspace.CopyingOriginals"), _session.Content.Sequence.DisplayTitle, Loc.L("Shell.Nav.Jobs"), Loc.L("Tooltips.Nav.Jobs"));
+ }
+ catch (Exception ex) when (ex is not OutOfMemoryException)
+ {
+ Log.Warning("workspace", $"The originals of project {ProjectId} could not be queued for copy", ex);
+ _ui.Notify(Loc.L("Editor.Workspace.CopyFailed"), WorkspaceText.Failure(ex, IsServerHome), isError: true);
+ }
+ }
+
+ ///
+ /// Proxies only…: the plan (each original hashed and weighed against the home, read now), confirmed by the user, then
+ /// applied; the home out of reach, or anything changed since the plan, removes nothing.
+ ///
+ private async Task ProxiesOnlyAsync()
+ {
+ try
+ {
+ await StoreEditAsync();
+ if (await MediaRefusalAsync() is { } refusal)
+ {
+ _ui.Notify(Loc.L("Editor.Workspace.OriginalsNotRemoved"), refusal, isError: true);
+ return;
+ }
+ SetChangingMedia(true);
+ var plan = await _host.PlanProxiesOnlyAsync(ProjectId);
+ if (_disposed) return;
+ var detail = new List
+ {
+ Loc.L("Editor.ProxiesOnly.Originals", Formatting.Count(plan.Originals)),
+ Loc.L("Editor.ProxiesOnly.Size", Formatting.Bytes(plan.Bytes)),
+ };
+ if (plan.ToTrash > 0) detail.Add(Loc.L("Editor.ProxiesOnly.ToTrash", Formatting.Count(plan.ToTrash)));
+ if (!_ui.Confirm(new ConfirmRequest
+ {
+ Heading = Loc.L("Editor.ProxiesOnly.Title", Name),
+ Message = WorkspaceText.ForHome("Editor.ProxiesOnly.Message", IsServerHome),
+ Detail = string.Join(Environment.NewLine, detail),
+ IsDetailMonospaced = false,
+ DetailToolTip = plan.ToTrash > 0 ? WorkspaceText.ForHome("Tooltips.Editor.ProxiesOnly.ToTrash", IsServerHome) : null,
+ SafeText = Loc.L("Editor.Project.Keep"),
+ SafeToolTip = Loc.L("Tooltips.Editor.ProxiesOnly.Keep"),
+ ActionText = Loc.L("Editor.ProxiesOnly.Action"),
+ ActionToolTip = Loc.L("Tooltips.Editor.ProxiesOnly.Action"),
+ IsDestructive = true,
+ }))
+ return;
+ var trashed = await _host.ProxiesOnlyAsync(ProjectId, plan);
+ if (_disposed) return;
+ await _session.ReloadAsync();
+ _ui.Notify(Loc.L("Editor.Workspace.OriginalsRemoved"),
+ trashed > 0 ? Loc.L("Editor.ProxiesOnly.ToTrash", Formatting.Count(trashed)) : _session.Content.Sequence.DisplayTitle);
+ }
+ catch (Exception ex) when (ex is not OutOfMemoryException)
+ {
+ Log.Warning("workspace", $"The originals of project {ProjectId} were not removed", ex);
+ _ui.Notify(Loc.L("Editor.Workspace.OriginalsNotRemoved"), WorkspaceText.Failure(ex, IsServerHome), isError: true);
+ }
+ finally
+ {
+ SetChangingMedia(false);
+ await RefreshAsync();
+ }
+ }
+
+ ///
+ /// Why the media cannot change now (Errors.Workspace.MediaBusy): changes not saved as a version, a copy or sync
+ /// running, or an analysis, render or workspace job of the sequence waiting or running; null when they can.
+ ///
+ private async Task MediaRefusalAsync()
+ {
+ var local = await _host.StatusAsync(ProjectId, askHome: false);
+ if (_disposed) return null;
+ Apply(WithLocal(_status, local));
+ if (IsMediaBusy) return Loc.L("Errors.Workspace.MediaBusy");
+ var jobs = await _source.ListJobsAsync(CancellationToken.None);
+ var sequenceId = SequenceId;
+ return jobs.Any(j => j.State is JobState.Queued or JobState.Running or JobState.Paused && j.TargetIds.Contains(sequenceId)
+ && j.Kind is JobKind.DeepAnalyse or JobKind.LightAnalyse or JobKind.Render or JobKind.WorkspaceCopy or JobKind.WorkspaceRender
+ or JobKind.WorkspaceSend)
+ ? Loc.L("Errors.Workspace.MediaBusy")
+ : null;
+ }
+
+ /// The copy of the originals ended: said, with the frames that have none (they stay proxies, and a local render waits for them).
+ private void NotifyOriginalsCopied()
+ {
+ int missing;
+ try
+ {
+ missing = _source.Project.ReadMedia()?.Frames.Count(f => f.Original is null) ?? 0;
+ }
+ catch (Exception ex) when (ex is System.IO.IOException or System.IO.InvalidDataException or UnauthorizedAccessException)
+ {
+ Log.Warning("workspace", $"The media of project {ProjectId} could not be read after the copy of its originals", ex);
+ missing = 0;
+ }
+ if (missing == 0) _ui.Notify(Loc.L("Editor.Workspace.OriginalsCopied"), _session.Content.Sequence.DisplayTitle);
+ else _ui.Notify(Loc.L("Editor.Workspace.OriginalsCopied"), Loc.L("Editor.Workspace.OriginalsMissing", Formatting.Count(missing)));
+ }
+
+ private void SetChangingMedia(bool changing)
+ {
+ _changingMedia = changing;
+ RaiseState();
+ }
+
/// The editor's last edit stored in the working state before an operation reads it.
/// It could not be stored (it is retried in the background; nothing was lost).
private async Task StoreEditAsync()
@@ -787,10 +936,13 @@ public sealed class ProjectMenuViewModel : LocalizedViewModel
{
if (_disposed || job.Id != _copyJob) return;
_copyJob = Guid.Empty;
+ var originals = _copyingOriginals;
+ _copyingOriginals = false;
if (job.State == JobState.Failed) _ui.Notify(Loc.L("Editor.Workspace.CopyFailed"), job.Error ?? _session.Content.Sequence.DisplayTitle, isError: true);
try
{
if (job.State == JobState.Done) await _session.ReloadAsync();
+ if (job.State == JobState.Done && originals) NotifyOriginalsCopied();
}
catch (Exception ex) when (ex is not OutOfMemoryException)
{
@@ -836,6 +988,10 @@ public sealed class ProjectMenuViewModel : LocalizedViewModel
OnPropertyChanged(nameof(RemoveCopyToolTip));
OnPropertyChanged(nameof(MediaChangedToolTip));
OnPropertyChanged(nameof(UpdateMediaToolTip));
+ OnPropertyChanged(nameof(HoldsOriginals));
+ OnPropertyChanged(nameof(IsMediaBusy));
+ OnPropertyChanged(nameof(CopyOriginalsToolTip));
+ OnPropertyChanged(nameof(ProxiesOnlyToolTip));
_updateMedia.RaiseCanExecuteChanged();
CommandManager.InvalidateRequerySuggested();
}
diff --git a/src/Chronos.App/Workspace/WorkspaceDialogs.cs b/src/Chronos.App/Workspace/WorkspaceDialogs.cs
index 3fb826a8..60467b4f 100644
--- a/src/Chronos.App/Workspace/WorkspaceDialogs.cs
+++ b/src/Chronos.App/Workspace/WorkspaceDialogs.cs
@@ -47,8 +47,9 @@ public static class ProjectNames
///
/// "Open in workspace" (owner review 2, R8), asked when a sequence has no project in the workspace yet, and by the project
/// menu's Open project: which project of its home (Main, made from the sequence's settings as they are, or another) or a
-/// new one with its name, and the media to copy (proxies; the originals come in phase 5c). Open queues the copy; the
-/// sequence opens once it is done.
+/// new one with its name, and the media to copy: the proxies, or (phase 5c) the originals besides, to render on this PC,
+/// offered while the workspace's drive has room for them (Settings › Workspace › Media chooses the default). Open queues
+/// the copy; the sequence opens once it is done.
///
public sealed class OpenInWorkspaceDialogViewModel : ArchiveDialogViewModel
{
@@ -56,6 +57,7 @@ public sealed class OpenInWorkspaceDialogViewModel : ArchiveDialogViewModel
private readonly bool _server;
private ProjectOption? _selected;
private bool _isNew;
+ private bool _isOriginals;
/// The names a new project cannot take (default: those of ).
/// What the copy of the media writes and the free space of the workspace's drive; null when not measured (shown as nothing).
@@ -63,8 +65,10 @@ public sealed class OpenInWorkspaceDialogViewModel : ArchiveDialogViewModel
/// The projects whose copy here this Chronos cannot read (damaged, or a newer Chronos's), with why: listed, never chosen.
///
/// The sequence's home is a server (phase 5b): the texts name the server.
+ /// The media chosen first (); the proxies when the originals have no room.
public OpenInWorkspaceDialogViewModel(string sequenceTitle, IReadOnlyList projects, Action close,
- IReadOnlyList? taken = null, WorkspaceCopyFacts? copy = null, IReadOnlyDictionary? unreadable = null, bool server = false)
+ IReadOnlyList? taken = null, WorkspaceCopyFacts? copy = null, IReadOnlyDictionary? unreadable = null, bool server = false,
+ string defaultMedia = BundleMedia.Proxies)
: base(close)
{
_server = server;
@@ -76,7 +80,11 @@ public sealed class OpenInWorkspaceDialogViewModel : ArchiveDialogViewModel
new DialogFact(Loc.L("Editor.OpenWorkspace.Size"), Formatting.Bytes(copy.Bytes), Reason: Loc.L("Tooltips.Editor.OpenWorkspace.Size")),
new DialogFact(Loc.L("Editor.OpenWorkspace.Free"), copy.Free is { } free ? Formatting.Bytes(free) : Loc.L("Server.Sync.None"),
Reason: Loc.L(copy.TooLittleSpace ? "Errors.Workspace.NoRoom" : "Tooltips.Editor.OpenWorkspace.Free")),
+ .. copy.OriginalsBytes is { } originals
+ ? [new DialogFact(Loc.L("Editor.OpenWorkspace.Originals"), Formatting.Bytes(originals), Reason: Loc.L("Tooltips.Editor.OpenWorkspace.OriginalsSize"))]
+ : Array.Empty(),
];
+ _isOriginals = defaultMedia == BundleMedia.Originals && CanCopyOriginals;
Projects = [.. projects.OrderByDescending(p => p.IsMain).Select(p => new ProjectOption(p, unreadable?.GetValueOrDefault(p.Id)))];
_selected = Projects.FirstOrDefault(p => p.IsAvailable);
_isNew = !HasProjects;
@@ -97,8 +105,8 @@ public sealed class OpenInWorkspaceDialogViewModel : ArchiveDialogViewModel
/// "Copy size" and "Free space", under the media.
public IReadOnlyList CopyFacts { get; }
- /// The drive lacks room for the copy: the free space says so (the copy would stop saying how much is missing).
- public bool IsShortOfSpace => Copy?.TooLittleSpace == true;
+ /// The drive lacks room for the copy of the media chosen: the free space says so (the copy would stop saying how much is missing).
+ public bool IsShortOfSpace => Copy?.TooLittleSpaceFor(Media) == true;
public string Heading => Loc.L("Editor.OpenWorkspace.Title");
@@ -147,11 +155,37 @@ public sealed class OpenInWorkspaceDialogViewModel : ArchiveDialogViewModel
public string ProxiesToolTip => WorkspaceText.ForHome("Tooltips.Editor.OpenWorkspace.Proxies", _server);
- /// The proxies, the only media this phase copies.
- public bool IsProxies => true;
+ /// The proxies alone: light to edit, renders run where the originals are.
+ public bool IsProxies
+ {
+ get => !_isOriginals;
+ set
+ {
+ if (value) IsOriginals = false;
+ }
+ }
- /// The originals: offered from phase 5c, to render on this PC.
- public bool CanCopyOriginals => false;
+ /// The proxies and the originals (phase 5c): renders run on this PC.
+ public bool IsOriginals
+ {
+ get => _isOriginals;
+ set
+ {
+ if (!Set(ref _isOriginals, value && CanCopyOriginals)) return;
+ OnPropertyChanged(nameof(IsProxies));
+ OnPropertyChanged(nameof(IsShortOfSpace));
+ }
+ }
+
+ /// The media copies.
+ public string Media => _isOriginals ? BundleMedia.Originals : BundleMedia.Proxies;
+
+ /// The originals can be chosen: the drive has room for them besides the proxies (or was not measured: the copy measures again).
+ public bool CanCopyOriginals => Copy?.TooLittleSpaceFor(BundleMedia.Originals) != true;
+
+ public string OriginalsToolTip => CanCopyOriginals
+ ? WorkspaceText.ForHome("Tooltips.Editor.OpenWorkspace.OriginalsReady", _server)
+ : Loc.L("Tooltips.Editor.OpenWorkspace.NoRoomOriginals");
public ICommand OpenCommand { get; }
@@ -169,7 +203,7 @@ public sealed class OpenInWorkspaceDialogViewModel : ArchiveDialogViewModel
private void Open()
{
if (!CanOpen) return;
- _choice.TrySetResult(_isNew ? new OpenChoice(null, NewName.Text.Trim(), BundleMedia.Proxies) : new OpenChoice(_selected!.Project.Id, null, BundleMedia.Proxies));
+ _choice.TrySetResult(_isNew ? new OpenChoice(null, NewName.Text.Trim(), Media) : new OpenChoice(_selected!.Project.Id, null, Media));
Close();
}
@@ -181,6 +215,69 @@ public sealed class OpenInWorkspaceDialogViewModel : ArchiveDialogViewModel
}
}
+/// A project of the workspace offered by "Open which project?" (Main in the UI language).
+public sealed record WorkspaceProjectChoice(WorkspaceEntry Entry)
+{
+ public string Label => WorkspaceText.ProjectName(Entry.Name, Entry.IsMain);
+
+ public override string ToString() => Label;
+}
+
+///
+/// "Open which project?" (phase 5c, Settings › Workspace › Open project: Ask): Edit asks which of the sequence's projects in
+/// the workspace opens, the one opened last chosen first.
+///
+public sealed class OpenProjectDialogViewModel : ArchiveDialogViewModel
+{
+ private readonly TaskCompletionSource _choice = new(TaskCreationOptions.RunContinuationsAsynchronously);
+ private WorkspaceProjectChoice? _selected;
+
+ /// The sequence's readable projects in the workspace.
+ /// The project opened last, chosen first; null: Main.
+ public OpenProjectDialogViewModel(string sequenceTitle, IReadOnlyList projects, Guid? last, Action close)
+ : base(close)
+ {
+ ArgumentNullException.ThrowIfNull(projects);
+ Message = Loc.L("Editor.OpenProject.Message", sequenceTitle);
+ Projects = [.. projects.OrderByDescending(p => p.IsMain).ThenBy(p => p.Name, StringComparer.CurrentCultureIgnoreCase).Select(p => new WorkspaceProjectChoice(p))];
+ _selected = Projects.FirstOrDefault(p => p.Entry.ProjectId == last) ?? Projects.FirstOrDefault();
+ OpenCommand = new RelayCommand(Open, () => _selected is not null);
+ }
+
+ /// Completes with the project chosen, or null when the dialog was closed without one.
+ public Task Choice => _choice.Task;
+
+ public string Heading => Loc.L("Editor.OpenProject.Title");
+
+ public string Message { get; }
+
+ public IReadOnlyList Projects { get; }
+
+ public WorkspaceProjectChoice? SelectedProject
+ {
+ get => _selected;
+ set
+ {
+ if (Set(ref _selected, value)) CommandManager.InvalidateRequerySuggested();
+ }
+ }
+
+ public ICommand OpenCommand { get; }
+
+ public override void Dispose()
+ {
+ _choice.TrySetResult(null);
+ base.Dispose();
+ }
+
+ private void Open()
+ {
+ if (_selected is null) return;
+ _choice.TrySetResult(_selected.Entry);
+ Close();
+ }
+}
+
/// Names a project: "New project" (made from the open project's last version at the home) or "Rename".
public sealed class ProjectNameDialogViewModel : ArchiveDialogViewModel
{
diff --git a/src/Chronos.App/Workspace/WorkspaceDialogs.xaml b/src/Chronos.App/Workspace/WorkspaceDialogs.xaml
index cb9391be..d7af2a3b 100644
--- a/src/Chronos.App/Workspace/WorkspaceDialogs.xaml
+++ b/src/Chronos.App/Workspace/WorkspaceDialogs.xaml
@@ -54,10 +54,10 @@
IsEnabled="{Binding IsNew}" ToolTip="{Binding NewName.ToolTip}" MaxLength="60" AutomationProperties.Name="{l:Loc Editor.OpenWorkspace.New}" />
-
-
+
@@ -88,6 +88,30 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/src/Chronos.App/Workspace/WorkspaceHost.Media.cs b/src/Chronos.App/Workspace/WorkspaceHost.Media.cs
index d3ba872d..8b316327 100644
--- a/src/Chronos.App/Workspace/WorkspaceHost.Media.cs
+++ b/src/Chronos.App/Workspace/WorkspaceHost.Media.cs
@@ -1,4 +1,9 @@
+using System.IO;
+using Chronos.Core.Diagnostics;
using Chronos.Core.Localization;
+using Chronos.Core.Model;
+using Chronos.Core.Project;
+using Chronos.Data.Projects;
using Chronos.Data.Workspaces;
namespace Chronos.App.Workspace;
@@ -9,7 +14,13 @@ namespace Chronos.App.Workspace;
///
public sealed partial class WorkspaceHost
{
- /// The bytes the first copy of a sequence's media writes into the workspace, and the free bytes of the workspace's drive.
+ /// A server's proxy when it says nothing of its size: some 1.5 MB, as the copy reckons one at 1920 px.
+ private const long ServerProxyEstimate = 1_500_000;
+
+ ///
+ /// The bytes the first copy of a sequence's media writes into the workspace (the proxies; the originals besides in
+ /// ), and the free bytes of the workspace's drive.
+ ///
/// The index or the archive engine is not open.
public Task CopyFactsAsync(Guid sequenceId, CancellationToken cancellationToken = default)
{
@@ -19,7 +30,100 @@ public sealed partial class WorkspaceHost
return Task.Run(async () =>
{
var (bytes, free) = await WorkspaceJobs.CopyFactsAsync(db, archives, sequenceId, root, cancellationToken).ConfigureAwait(false);
- return new WorkspaceCopyFacts(bytes, free);
+ var originals = await WorkspaceOriginals.ArchiveOriginalsBytesAsync(db, archives, sequenceId, cancellationToken: cancellationToken).ConfigureAwait(false);
+ return new WorkspaceCopyFacts(bytes, free, originals);
}, cancellationToken);
}
+
+ ///
+ /// What the copy of a sequence of writes: for this PC's archive as
+ /// ; for a server, reckoned from its frames (a proxy some 1.5 MB, the
+ /// originals as large as the server says they are).
+ ///
+ /// The server cannot be reached now.
+ public Task CopyFactsAsync(BundleHome home, CancellationToken cancellationToken = default)
+ {
+ ArgumentNullException.ThrowIfNull(home);
+ if (home.IsArchive) return CopyFactsAsync(home.SequenceId, cancellationToken);
+ var reached = Reachable(home);
+ var root = Root;
+ return Task.Run(async () =>
+ {
+ var frames = (await reached.FramesAsync(cancellationToken).ConfigureAwait(false)).Frames;
+ var present = frames.Where(f => (f.Flags & FrameFlags.Missing) == 0).ToList();
+ return new WorkspaceCopyFacts(present.Count * ServerProxyEstimate, FreeBytes(root), present.Sum(f => Math.Max(0, f.Size)));
+ }, cancellationToken);
+ }
+
+ ///
+ /// "Copy originals…": queues the copy of a workspace project's media with its originals (phase 5c), unless a copy is
+ /// waiting or running already (that one is returned). The project file says originals once the copy commits.
+ ///
+ /// The project is not here, or the workspace folder is being moved.
+ public Task QueueOriginalsCopyAsync(Guid projectId, CancellationToken cancellationToken = default) => Task.Run(async () =>
+ {
+ EnsureNotMoving();
+ var bundle = Existing(projectId).ReadBundle();
+ Log.Info("workspace", $"Originals of project {projectId} queued for copy");
+ return await QueueCopyCoreAsync(bundle.Workspace!.Home, projectId, BundleMedia.Originals, bundle.Sequence.Title ?? string.Empty, cancellationToken)
+ .ConfigureAwait(false);
+ }, cancellationToken);
+
+ ///
+ /// The plan of "Proxies only…" (phase 5c): each original of the project hashed and weighed against the home's frames,
+ /// read now. Nothing is changed.
+ ///
+ /// The home cannot be reached to check the originals (Errors.Workspace.OriginalsCheck).
+ public Task PlanProxiesOnlyAsync(Guid projectId, CancellationToken cancellationToken = default) => Task.Run(async () =>
+ {
+ var project = Existing(projectId);
+ var home = await CheckedHomeFramesAsync(project, cancellationToken).ConfigureAwait(false);
+ return WorkspaceOriginals.PlanProxiesOnly(project, home);
+ }, cancellationToken);
+
+ ///
+ /// Applies "Proxies only…" as planned (the user confirmed it): the home's frames read again, the plan refused when the
+ /// project or the home changed since; then the originals the home holds are deleted, the others moved to the trash.
+ ///
+ /// The originals moved to the workspace's trash.
+ /// The home cannot be reached, or the workspace folder is being moved.
+ /// The project or the home changed since the plan (Errors.Workspace.MediaChanged).
+ public Task ProxiesOnlyAsync(Guid projectId, WorkspaceProxiesOnlyPlan plan, CancellationToken cancellationToken = default) => Task.Run(async () =>
+ {
+ ArgumentNullException.ThrowIfNull(plan);
+ EnsureNotMoving();
+ var project = Existing(projectId);
+ var home = await CheckedHomeFramesAsync(project, cancellationToken).ConfigureAwait(false);
+ return await WorkspaceOriginals.ApplyProxiesOnlyAsync(project, plan, home, LockWait, cancellationToken).ConfigureAwait(false);
+ }, cancellationToken);
+
+ /// The frames of the project's home, as it answers now; one out of reach refuses ("nothing was removed").
+ private async Task CheckedHomeFramesAsync(WorkspaceProject project, CancellationToken cancellationToken)
+ {
+ var named = project.ReadBundle().Workspace!.Home;
+ var refused = WorkspaceText.ForHome("Errors.Workspace.OriginalsCheck", !named.IsArchive);
+ var home = HomeOf(named) ?? throw new WorkspaceException(refused);
+ try
+ {
+ return await home.FramesAsync(cancellationToken).ConfigureAwait(false);
+ }
+ catch (HomeUnavailableException ex)
+ {
+ throw new WorkspaceException(refused, ex);
+ }
+ }
+
+ /// Free bytes on the drive of ; null when it cannot be measured (a network share).
+ private static long? FreeBytes(string folder)
+ {
+ try
+ {
+ var root = Path.GetPathRoot(Path.GetFullPath(folder));
+ return string.IsNullOrEmpty(root) ? null : new DriveInfo(root).AvailableFreeSpace;
+ }
+ catch (Exception ex) when (ex is ArgumentException or IOException or UnauthorizedAccessException)
+ {
+ return null;
+ }
+ }
}
diff --git a/src/Chronos.Data/Workspaces/WorkspaceCopyJobHandler.cs b/src/Chronos.Data/Workspaces/WorkspaceCopyJobHandler.cs
index d788b35a..efa1b139 100644
--- a/src/Chronos.Data/Workspaces/WorkspaceCopyJobHandler.cs
+++ b/src/Chronos.Data/Workspaces/WorkspaceCopyJobHandler.cs
@@ -37,7 +37,14 @@ namespace Chronos.Data.Workspaces;
/// removes the media it wrote itself, once it has stopped writing and under the project's lease, unless another copy of the
/// project goes on with them (); one cancelled while paused or failed, which no handler
/// runs, has them removed the same way ().
-/// Originals come in phase 5c: asked for, the job fails saying so.
+/// With (phase 5c) the proxies are copied first as above, then each frame's original into
+/// media/originals/<frame id>-<sha8><ext> (): streamed into a
+/// .part file and hashed as it is written, the SHA-256 checked against the home's (recorded as copied when the home has
+/// none), the file read back after its rename; one already there with the same SHA-256 is kept. A frame whose original the
+/// home lacks (gone, flagged missing or damaged, refused by a server, not matching its SHA-256 twice) keeps its proxy: the
+/// copy finishes saying how many. The space reckoned adds the originals not held yet; the checkpoint of that stage is
+/// originals:<frame>:<frames version>:<folder>. Originals the committed media no longer name are deleted
+/// only when the home holds a frame of the same SHA-256, else kept in the workspace's trash ().
/// A project whose home is a server (phase 5b, ) has its media downloaded from
/// the server through the desktop's session (), never through this PC's archive: the frames as
/// of one frames version with the aliases of the server's conversions (GET …/projects/frames), each frame's proxy as
@@ -81,8 +88,7 @@ internal sealed class WorkspaceCopyJobHandler(ChronosDatabase db, ArchiveService
{
var ct = context.Cancellation;
var parameters = Parameters(context.Job);
- if (parameters.Media == BundleMedia.Originals) throw new NotSupportedException(Loc.L("Errors.Workspace.OriginalsLater"));
- if (parameters.Media != BundleMedia.Proxies) throw new InvalidDataException(Loc.L("Errors.Workspace.Parameters"));
+ if (parameters.Media is not (BundleMedia.Proxies or BundleMedia.Originals)) throw new InvalidDataException(Loc.L("Errors.Workspace.Parameters"));
if (parameters.Server is { Length: > 0 } server)
{
await RunFromServerAsync(context, parameters, server).ConfigureAwait(false);
@@ -259,6 +265,7 @@ internal sealed class WorkspaceCopyJobHandler(ChronosDatabase db, ArchiveService
// Read only: a read-only archive is copied from like any other.
if (ArchiveVolume.Probe(paths) == ArchiveAvailability.Offline) throw new ArchiveOfflineException(archive.RootPath, ArchiveAvailability.Offline);
var (copied, checkpointVersion, checkpointFolder) = Parse(context.Checkpoint);
+ var withOriginals = parameters.Media == BundleMedia.Originals;
for (var pass = 1; ; pass++)
{
var (row, frames) = await FramesAsync(sequenceId, ct).ConfigureAwait(false);
@@ -274,15 +281,18 @@ internal sealed class WorkspaceCopyJobHandler(ChronosDatabase db, ArchiveService
// Files kept from an earlier run are this frames version's and this folder's only (a conversion keeps a frame's id,
// not its photo; a sequence moved meanwhile may have left its photos' names to another sequence's).
if ((checkpointVersion is { } version && version != row.FramesVersion) || (checkpointFolder is { } read && read != row.Folder)) copied = 0;
- var total = (long)frames.Count;
+ var total = (long)frames.Count * (withOriginals ? 2 : 1);
var tally = new Tally();
var entries = new WorkspaceFrame[frames.Count];
// The media the editor reads until this copy commits: their files stay until then (RemoveStale).
- var current = TryReadMedia(project)?.Frames.Where(f => f.File is not null).ToDictionary(f => f.Id) ?? [];
+ var held = TryReadMedia(project)?.Frames ?? [];
+ var current = held.Where(f => f.File is not null).ToDictionary(f => f.Id);
+ var heldOriginals = held.Where(f => f.Original is not null).ToDictionary(f => f.Id);
Directory.CreateDirectory(project.ProxiesDirectory);
// The workspace is on this PC's own drive, with the index, the settings and the logs: the copy never fills it.
var remaining = Remaining(project, folder, frames, policy);
- EnsureSpace(parameters.WorkspaceRoot, remaining[0]);
+ var originalsLeft = withOriginals ? RemainingOriginals(project, frames, heldOriginals, (f, _) => f.Size) : new long[frames.Count + 1];
+ EnsureSpace(parameters.WorkspaceRoot, remaining[0] + originalsLeft[0]);
var clock = System.Diagnostics.Stopwatch.StartNew();
for (var i = 0; i < frames.Count; i++)
{
@@ -292,7 +302,7 @@ internal sealed class WorkspaceCopyJobHandler(ChronosDatabase db, ArchiveService
{
await context.CheckpointAsync(Checkpoint(Stage.Copy, i + 1, row.FramesVersion, row.Folder), i + 1, total).ConfigureAwait(false);
// Another program may take the space meanwhile.
- EnsureSpace(parameters.WorkspaceRoot, remaining[i + 1]);
+ EnsureSpace(parameters.WorkspaceRoot, remaining[i + 1] + originalsLeft[0]);
clock.Restart();
}
else
@@ -300,6 +310,27 @@ internal sealed class WorkspaceCopyJobHandler(ChronosDatabase db, ArchiveService
await context.ReportAsync(i + 1, total).ConfigureAwait(false);
}
}
+ var originals = new OriginalsTally();
+ if (withOriginals)
+ {
+ for (var i = 0; i < frames.Count; i++)
+ {
+ ct.ThrowIfCancellationRequested();
+ entries[i] = await CopyOriginalAsync(project, paths, frames[i], entries[i], heldOriginals.GetValueOrDefault(frames[i].Id),
+ originals, ct).ConfigureAwait(false);
+ var done = frames.Count + i + 1;
+ if (clock.Elapsed >= CheckpointEvery)
+ {
+ await context.CheckpointAsync(Checkpoint(Stage.Originals, i + 1, row.FramesVersion, row.Folder), done, total).ConfigureAwait(false);
+ EnsureSpace(parameters.WorkspaceRoot, originalsLeft[i + 1]);
+ clock.Restart();
+ }
+ else
+ {
+ await context.ReportAsync(done, total).ConfigureAwait(false);
+ }
+ }
+ }
var analysis = CopyAnalysis(folder, project, frames.Count);
var (after, now) = await FramesAsync(sequenceId, ct).ConfigureAwait(false);
var moved = after.Folder != row.Folder || !SequenceFolders.Holds(paths.Sequence(after.Folder), sequenceId) || Renamed(frames, now, entries);
@@ -322,15 +353,16 @@ internal sealed class WorkspaceCopyJobHandler(ChronosDatabase db, ArchiveService
}
}
var aliases = await HomePhotos.AliasesAsync(db, sequenceId, ct).ConfigureAwait(false);
- var media = new WorkspaceMedia(WorkspaceMedia.FormatName, WorkspaceMedia.CurrentFormatVersion, BundleMedia.Proxies, sequenceId, row.FramesVersion, entries,
+ var media = new WorkspaceMedia(WorkspaceMedia.FormatName, WorkspaceMedia.CurrentFormatVersion, parameters.Media, sequenceId, row.FramesVersion, entries,
DateTimeOffset.UtcNow, analysis, aliases.Count > 0 ? aliases : null);
// The working state is the editor's meanwhile: it is held only while the media index and the positions move.
using (await project.LockAsync(LockWait, ct).ConfigureAwait(false))
{
Commit(project, project.ReadBundle(), media, row.Folder, row.DisplayTitle);
- RemoveStale(project, media);
+ RemoveStale(project, media, frames);
}
context.Log(Loc.L("Archive.Log.WorkspaceCopied", row.DisplayTitle, tally.Copied, tally.Made, tally.Kept, tally.Missing));
+ if (withOriginals) originals.LogTo(context, "Archive.Log.WorkspaceOriginals", row.DisplayTitle);
await context.CheckpointAsync(Checkpoint(Stage.Done, frames.Count, row.FramesVersion, row.Folder), total, total).ConfigureAwait(false);
return;
}
@@ -372,6 +404,7 @@ internal sealed class WorkspaceCopyJobHandler(ChronosDatabase db, ArchiveService
CancellationToken ct)
{
var (copied, checkpointVersion, checkpointFolder) = Parse(context.Checkpoint);
+ var withOriginals = parameters.Media == BundleMedia.Originals;
// What this run downloaded, pass after pass: kept where a frame still shows the same photo.
var fetched = new Dictionary();
for (var pass = 1; ; pass++)
@@ -379,15 +412,20 @@ internal sealed class WorkspaceCopyJobHandler(ChronosDatabase db, ArchiveService
var (sequence, frames, assets, aliases, framesVersion, spec) = await ServerFramesAsync(remote, sequenceId, ct).ConfigureAwait(false);
var folder = sequence.Folder ?? string.Empty;
if ((checkpointVersion is { } version && version != framesVersion) || (checkpointFolder is { } read && read != folder)) copied = 0;
- var total = (long)frames.Count;
+ var total = (long)frames.Count * (withOriginals ? 2 : 1);
var tally = new Tally();
var entries = new WorkspaceFrame[frames.Count];
// The media the editor reads until this copy commits: their files stay until then (RemoveStale).
- var current = TryReadMedia(project)?.Frames.Where(f => f.File is not null).ToDictionary(f => f.Id) ?? [];
+ var held = TryReadMedia(project)?.Frames ?? [];
+ var current = held.Where(f => f.File is not null).ToDictionary(f => f.Id);
+ var heldOriginals = held.Where(f => f.Original is not null).ToDictionary(f => f.Id);
Directory.CreateDirectory(project.ProxiesDirectory);
// The workspace is on this PC's own drive: the copy never fills it (the server's files estimated at its proxy size).
var remaining = RemainingDownloads(project, frames, assets, spec);
- EnsureSpace(parameters.WorkspaceRoot, remaining[0]);
+ var originalsLeft = withOriginals
+ ? RemainingOriginals(project, frames, heldOriginals, (f, _) => assets.GetValueOrDefault(f.Id) is { } asset ? asset.Size : 0)
+ : new long[frames.Count + 1];
+ EnsureSpace(parameters.WorkspaceRoot, remaining[0] + originalsLeft[0]);
var clock = System.Diagnostics.Stopwatch.StartNew();
var options = new ParallelOptions { MaxDegreeOfParallelism = Downloads, CancellationToken = ct };
for (var start = 0; start < frames.Count; start += DownloadBatch)
@@ -401,7 +439,7 @@ internal sealed class WorkspaceCopyJobHandler(ChronosDatabase db, ArchiveService
{
await context.CheckpointAsync(Checkpoint(Stage.Copy, end, framesVersion, folder), end, total).ConfigureAwait(false);
// Another program may take the space meanwhile.
- EnsureSpace(parameters.WorkspaceRoot, remaining[end]);
+ EnsureSpace(parameters.WorkspaceRoot, remaining[end] + originalsLeft[0]);
clock.Restart();
}
else
@@ -410,6 +448,27 @@ internal sealed class WorkspaceCopyJobHandler(ChronosDatabase db, ArchiveService
}
}
foreach (var entry in entries.Where(e => e.File is not null)) fetched[entry.Id] = entry;
+ var originals = new OriginalsTally();
+ if (withOriginals)
+ {
+ for (var start = 0; start < frames.Count; start += DownloadBatch)
+ {
+ var end = Math.Min(frames.Count, start + DownloadBatch);
+ await Parallel.ForEachAsync(Enumerable.Range(start, end - start), options, async (i, token) =>
+ entries[i] = await DownloadOriginalAsync(remote, project, frames[i], assets.GetValueOrDefault(frames[i].Id), entries[i],
+ heldOriginals.GetValueOrDefault(frames[i].Id), originals, token).ConfigureAwait(false)).ConfigureAwait(false);
+ if (clock.Elapsed >= CheckpointEvery || end == frames.Count)
+ {
+ await context.CheckpointAsync(Checkpoint(Stage.Originals, end, framesVersion, folder), frames.Count + end, total).ConfigureAwait(false);
+ EnsureSpace(parameters.WorkspaceRoot, originalsLeft[end]);
+ clock.Restart();
+ }
+ else
+ {
+ await context.ReportAsync(frames.Count + end, total).ConfigureAwait(false);
+ }
+ }
+ }
var analysis = await CopyServerAnalysisAsync(remote, sequenceId, project, frames.Count, ct).ConfigureAwait(false);
var after = await remote.GetSequenceAsync(sequenceId, ct).ConfigureAwait(false);
if (after.FramesVersion != framesVersion && pass < Passes)
@@ -422,15 +481,16 @@ internal sealed class WorkspaceCopyJobHandler(ChronosDatabase db, ArchiveService
continue;
}
// Frames still arriving are copied as they were read (Update media takes the rest).
- var media = new WorkspaceMedia(WorkspaceMedia.FormatName, WorkspaceMedia.CurrentFormatVersion, BundleMedia.Proxies, sequenceId, framesVersion, entries,
+ var media = new WorkspaceMedia(WorkspaceMedia.FormatName, WorkspaceMedia.CurrentFormatVersion, parameters.Media, sequenceId, framesVersion, entries,
DateTimeOffset.UtcNow, analysis, aliases.Count > 0 ? aliases : null);
// The working state is the editor's meanwhile: it is held only while the media index and the positions move.
using (await project.LockAsync(LockWait, ct).ConfigureAwait(false))
{
Commit(project, project.ReadBundle(), media, folder, sequence.DisplayTitle);
- RemoveStale(project, media);
+ RemoveStale(project, media, frames);
}
context.Log(Loc.L("Archive.Log.WorkspaceDownloaded", sequence.DisplayTitle, tally.Copied, tally.Made, tally.Kept, tally.Missing));
+ if (withOriginals) originals.LogTo(context, "Archive.Log.WorkspaceOriginalsDownloaded", sequence.DisplayTitle);
await context.CheckpointAsync(Checkpoint(Stage.Done, frames.Count, framesVersion, folder), total, total).ConfigureAwait(false);
return;
}
@@ -572,6 +632,166 @@ internal sealed class WorkspaceCopyJobHandler(ChronosDatabase db, ArchiveService
entry is { FileSha256: { } held } && string.Equals(held, sha, StringComparison.OrdinalIgnoreCase) ? entry.FileKind ?? ProxyCache.Proxy : null;
}
+ ///
+ /// A frame's original from this PC's archive into media/originals/ (phase 5c), beside its proxy in
+ /// : kept when the workspace holds it already with the same SHA-256, else streamed into a
+ /// .part file hashed as it is written (never held in memory), checked against the home's SHA-256, renamed and read
+ /// back. A frame flagged missing or damaged at the home, or whose file is gone, keeps its proxy alone; so does one whose
+ /// original twice does not match the SHA-256 the home has of it (the part is removed, the copy goes on).
+ ///
+ private static async Task CopyOriginalAsync(WorkspaceProject project, ArchivePaths paths, Frame frame, WorkspaceFrame entry,
+ WorkspaceFrame? held, OriginalsTally tally, CancellationToken cancellationToken)
+ {
+ var extension = OriginalExtension(frame);
+ var expected = string.IsNullOrEmpty(frame.Sha256) ? null : frame.Sha256.ToLowerInvariant();
+ if ((frame.Flags & (FrameFlags.Missing | FrameFlags.Corrupt)) != 0) return tally.Missed(entry, frame.Index);
+ if (await HeldOriginalAsync(project, frame, expected, extension, held, cancellationToken).ConfigureAwait(false) is { } kept)
+ {
+ tally.Keep();
+ return entry with { Original = kept.Name, OriginalSha256 = kept.Sha256, OriginalBytes = kept.Bytes };
+ }
+ var source = paths.FromRelative(frame.RelativePath);
+ if (!File.Exists(source)) return tally.Missed(entry, frame.Index);
+ Directory.CreateDirectory(project.OriginalsDirectory);
+ var part = Path.Combine(project.OriginalsDirectory, frame.Id.ToString("N") + ".part");
+ for (var attempt = 1; ; attempt++)
+ {
+ string sha;
+ long bytes;
+ try
+ {
+ await using var input = new FileStream(source, FileMode.Open, FileAccess.Read, FileShare.Read | FileShare.Delete, StreamBuffer,
+ FileOptions.Asynchronous | FileOptions.SequentialScan);
+ await using var output = new FileStream(part, FileMode.Create, FileAccess.Write, FileShare.None, StreamBuffer, FileOptions.Asynchronous);
+ using var hashing = new HashingStream(output);
+ await input.CopyToAsync(hashing, StreamBuffer, cancellationToken).ConfigureAwait(false);
+ output.Flush(flushToDisk: true);
+ (sha, bytes) = (hashing.Sha256(), hashing.Written);
+ }
+ catch (FileNotFoundException)
+ {
+ // Gone while it was read (a Reorganize): the frame keeps its proxy, the pass's check of the folder decides the rest.
+ TryDelete(part);
+ return tally.Missed(entry, frame.Index);
+ }
+ catch (OperationCanceledException)
+ {
+ // A part is never resumed from (a source is read whole): it goes, so a cancel leaves nothing half written.
+ TryDelete(part);
+ throw;
+ }
+ if (expected is not null && sha != expected)
+ {
+ TryDelete(part);
+ if (attempt == 1) continue;
+ Log.Warning("workspace", Loc.L("Errors.Workspace.OriginalMismatch", source));
+ return tally.Missed(entry, frame.Index);
+ }
+ var name = WorkspaceProject.OriginalName(frame.Id, sha, extension);
+ var target = project.MediaFile(name);
+ File.Move(part, target, overwrite: true);
+ if (string.Equals(await ContentHash.Sha256Async(target, cancellationToken: cancellationToken).ConfigureAwait(false), sha, StringComparison.OrdinalIgnoreCase))
+ {
+ tally.Copy();
+ return entry with { Original = name, OriginalSha256 = sha, OriginalBytes = bytes };
+ }
+ TryDelete(target);
+ if (attempt == 2) throw new IOException(Loc.L("Errors.Workspace.CopyMismatch", target));
+ }
+ }
+
+ ///
+ /// A frame's original from a server into media/originals/ (phase 5c): kept when held already, else downloaded with
+ /// (resumable, checked against the server's SHA-256, two attempts) into a
+ /// staging folder beside the originals, then renamed into place. A frame the server has no original of (flagged missing
+ /// or damaged, 404), one this account may not read (403), or one that does not match twice keeps its proxy alone.
+ ///
+ private static async Task DownloadOriginalAsync(ISyncRemote remote, WorkspaceProject project, Frame frame, ManifestFrame? asset, WorkspaceFrame entry,
+ WorkspaceFrame? held, OriginalsTally tally, CancellationToken cancellationToken)
+ {
+ if (asset is null || (frame.Flags & (FrameFlags.Missing | FrameFlags.Corrupt)) != 0 || (((FrameFlags)asset.Flags) & (FrameFlags.Missing | FrameFlags.Corrupt)) != 0)
+ return tally.Missed(entry, frame.Index);
+ var extension = Path.GetExtension(asset.FileName);
+ var expected = string.IsNullOrEmpty(asset.Sha256) ? null : asset.Sha256.ToLowerInvariant();
+ if (await HeldOriginalAsync(project, frame, expected, extension, held, cancellationToken).ConfigureAwait(false) is { } kept)
+ {
+ tally.Keep();
+ return entry with { Original = kept.Name, OriginalSha256 = kept.Sha256, OriginalBytes = kept.Bytes };
+ }
+ var staging = Path.Combine(project.OriginalsDirectory, OriginalsStaging);
+ Directory.CreateDirectory(staging);
+ string path, sha;
+ try
+ {
+ (path, sha) = await SyncTransfers.FetchOriginalAsync(remote, asset, staging, cancellationToken).ConfigureAwait(false);
+ }
+ catch (ChronosApiException ex) when (ex.Status is HttpStatusCode.NotFound or HttpStatusCode.Forbidden)
+ {
+ return tally.Missed(entry, frame.Index);
+ }
+ catch (InvalidDataException ex)
+ {
+ Log.Warning("workspace", $"The original of frame {frame.Id} did not match the server's SHA-256", ex);
+ return tally.Missed(entry, frame.Index);
+ }
+ var name = WorkspaceProject.OriginalName(frame.Id, sha, extension);
+ var target = project.MediaFile(name);
+ var bytes = new FileInfo(path).Length;
+ File.Move(path, target, overwrite: true);
+ tally.Copy();
+ return entry with { Original = name, OriginalSha256 = sha, OriginalBytes = bytes };
+ }
+
+ ///
+ /// The original the workspace holds of , hashed again: the one the media name for it (the same
+ /// photo, of the SHA-256 the home has when it has one), else the file a copy of takes; null when
+ /// none holds it (a file that no longer matches is copied anew).
+ ///
+ private static async Task<(string Name, string Sha256, long Bytes)?> HeldOriginalAsync(WorkspaceProject project, Frame frame, string? expected, string extension,
+ WorkspaceFrame? held, CancellationToken cancellationToken)
+ {
+ var candidates = new List<(string Name, string? Sha)>();
+ if (held is { Original: { } heldName, OriginalSha256: { } heldSha } && SamePhoto(held, frame) && (expected is null || string.Equals(heldSha, expected, StringComparison.OrdinalIgnoreCase)))
+ candidates.Add((heldName, heldSha.ToLowerInvariant()));
+ if (expected is not null) candidates.Add((WorkspaceProject.OriginalName(frame.Id, expected, extension), expected));
+ foreach (var (name, sha) in candidates)
+ {
+ var path = project.MediaFile(name);
+ if (!File.Exists(path)) continue;
+ var found = await ContentHash.Sha256Async(path, cancellationToken: cancellationToken).ConfigureAwait(false);
+ if (string.Equals(found, sha, StringComparison.OrdinalIgnoreCase)) return (name, found.ToLowerInvariant(), new FileInfo(path).Length);
+ }
+ return null;
+ }
+
+ ///
+ /// The bytes a copy of the originals still writes from each frame on (index i: frames i to the last; one
+ /// more entry, 0): of each frame whose original the workspace does not hold yet under the name a
+ /// copy gives it, nothing for one the media name already, or flagged missing.
+ ///
+ private static long[] RemainingOriginals(WorkspaceProject project, List frames, Dictionary held, Func size)
+ {
+ var remaining = new long[frames.Count + 1];
+ for (var i = frames.Count - 1; i >= 0; i--)
+ {
+ var frame = frames[i];
+ var there = (held.GetValueOrDefault(frame.Id) is { Original: { } name } && File.Exists(project.MediaFile(name)))
+ || (!string.IsNullOrEmpty(frame.Sha256) && File.Exists(project.MediaFile(WorkspaceProject.OriginalName(frame.Id, frame.Sha256, OriginalExtension(frame)))));
+ var bytes = there || (frame.Flags & FrameFlags.Missing) != 0 ? 0 : Math.Max(0, size(frame, i));
+ remaining[i] = remaining[i + 1] + bytes;
+ }
+ return remaining;
+ }
+
+ /// The extension of a frame's original as its path at the home has it (else its file name), which decoders go by.
+ private static string OriginalExtension(Frame frame) => Path.GetExtension(frame.RelativePath) is { Length: > 0 } extension ? extension : Path.GetExtension(frame.FileName);
+
+ /// The folder beside the originals a download from a server stages them in (resumable parts), emptied by the commit.
+ private const string OriginalsStaging = ".staging";
+
+ /// The buffer an original is streamed through.
+ private const int StreamBuffer = 1 << 20;
+
///
/// The bytes a copy from a server still writes from each frame on (index i: frames i to the last; one more
/// entry, 0): nothing for a frame the workspace holds a file of already or the server has neither proxy nor thumbnail of,
@@ -785,7 +1005,7 @@ internal sealed class WorkspaceCopyJobHandler(ChronosDatabase db, ArchiveService
private static void Commit(WorkspaceProject project, ProjectBundle bundle, WorkspaceMedia media, string folder, string title)
{
var old = TryReadMedia(project);
- var workspace = bundle.Workspace! with { Media = new BundleMedia(BundleMedia.Proxies, media.FramesVersion) };
+ var workspace = bundle.Workspace! with { Media = new BundleMedia(media.Kind, media.FramesVersion) };
var sequence = bundle.Sequence with { Folder = folder, Title = title, Frames = media.Frames.Count };
var working = bundle.Project;
// Without the media index the positions were made against, they are taken as naming these frames.
@@ -814,8 +1034,36 @@ internal sealed class WorkspaceCopyJobHandler(ChronosDatabase db, ArchiveService
///
/// Proxies of frames the sequence no longer has, and downloads a stopped copy from a server left half done: regenerable
- /// copies, removed so the media folder holds the frames listed.
+ /// copies, removed so the media folder holds the frames listed. Media of kind originals (phase 5c) clear their folder
+ /// alike, but an original is never erased on a guess: one the media no longer name is deleted only when the home's
+ /// frames (, as this copy read them) hold a photo of its SHA-256, else it goes to the workspace's
+ /// trash (). A copy of the proxies alone leaves the originals as they are.
///
+ private static void RemoveStale(WorkspaceProject project, WorkspaceMedia media, IReadOnlyList home)
+ {
+ RemoveStale(project, media);
+ if (!media.HoldsOriginals || !Directory.Exists(project.OriginalsDirectory)) return;
+ var staging = Path.Combine(project.OriginalsDirectory, OriginalsStaging);
+ if (Directory.Exists(staging))
+ {
+ try
+ {
+ Directory.Delete(staging, recursive: true);
+ }
+ catch (Exception ex) when (ex is IOException or UnauthorizedAccessException)
+ {
+ Log.Warning("workspace", $"{staging} could not be removed", ex);
+ }
+ }
+ var originals = media.Frames.Where(f => f.Original is not null).Select(f => Path.GetFileName(f.Original!)).ToHashSet(StringComparer.OrdinalIgnoreCase);
+ var stale = Directory.EnumerateFiles(project.OriginalsDirectory).Where(f => !originals.Contains(Path.GetFileName(f))).ToList();
+ foreach (var part in stale.Where(IsPart)) TryDelete(part);
+ WorkspaceOriginals.DeleteOrTrash(project, [.. stale.Where(f => !IsPart(f))], home);
+ }
+
+ private static bool IsPart(string file) => file.EndsWith(".part", StringComparison.OrdinalIgnoreCase);
+
+ /// The proxies' folder cleared as says.
private static void RemoveStale(WorkspaceProject project, WorkspaceMedia media)
{
var listed = media.Frames.Where(f => f.File is not null).Select(f => Path.GetFileName(f.File!)).ToHashSet(StringComparer.OrdinalIgnoreCase);
@@ -889,6 +1137,18 @@ internal sealed class WorkspaceCopyJobHandler(ChronosDatabase db, ArchiveService
return (Remaining([], paths.Sequence(row.Folder), frames, policy)[0], FreeBytes(workspaceRoot));
}
+ ///
+ /// The bytes a copy of the sequence's originals writes (phase 5c): every frame's original the archive has, or, for a
+ /// workspace project that holds some already (), only those it lacks.
+ ///
+ internal async Task OriginalsBytesAsync(Guid sequenceId, WorkspaceProject? project, CancellationToken cancellationToken)
+ {
+ var (_, frames) = await FramesAsync(sequenceId, cancellationToken).ConfigureAwait(false);
+ if (project is null || !File.Exists(project.MediaIndexPath)) return frames.Where(f => (f.Flags & FrameFlags.Missing) == 0).Sum(f => Math.Max(0, f.Size));
+ var held = (TryReadMedia(project)?.Frames ?? []).Where(f => f.Original is not null).ToDictionary(f => f.Id);
+ return RemainingOriginals(project, frames, held, (f, _) => f.Size)[0];
+ }
+
/// A proxy's size when the archive has none to measure: some 1.5 MB at 1920 px, scaled by the area at the copy's long edge.
private static long Estimate(Frame frame, ProxyPolicy policy)
{
@@ -980,6 +1240,9 @@ internal sealed class WorkspaceCopyJobHandler(ChronosDatabase db, ArchiveService
{
Copy,
Done,
+
+ /// The proxies are copied; the originals are (phase 5c).
+ Originals,
}
private static string Checkpoint(Stage stage, int frame, int framesVersion, string folder) =>
@@ -994,11 +1257,46 @@ internal sealed class WorkspaceCopyJobHandler(ChronosDatabase db, ArchiveService
private static (int Copied, int? FramesVersion, string? Folder) Parse(string? checkpoint)
{
var parts = checkpoint?.Split(':', 4);
- if (parts is not ([_, _] or [_, _, _] or [_, _, _, _]) || !Enum.TryParse(parts[0], ignoreCase: true, out var stage) || stage != Stage.Copy
+ if (parts is not ([_, _] or [_, _, _] or [_, _, _, _]) || !Enum.TryParse(parts[0], ignoreCase: true, out var stage) || stage is not (Stage.Copy or Stage.Originals)
|| !int.TryParse(parts[1], NumberStyles.None, CultureInfo.InvariantCulture, out var next))
return (0, null, null);
int? version = parts.Length >= 3 && int.TryParse(parts[2], NumberStyles.AllowLeadingSign, CultureInfo.InvariantCulture, out var v) ? v : null;
- return (next, version, parts.Length == 4 ? parts[3] : null);
+ return (stage == Stage.Originals ? int.MaxValue : next, version, parts.Length == 4 ? parts[3] : null);
+ }
+
+ /// What a copy of the originals did with each frame (phase 5c), counted from the workers that download them at once.
+ private sealed class OriginalsTally
+ {
+ /// Frames named one by one in the job's log when they have no original; the count says the rest.
+ private const int Named = 20;
+
+ private readonly System.Collections.Concurrent.ConcurrentQueue<(int Index, string Name)> _missed = new();
+ private int _copied;
+ private int _kept;
+
+ public int Copied => Volatile.Read(ref _copied);
+
+ public int Kept => Volatile.Read(ref _kept);
+
+ public int Missing => _missed.Count;
+
+ public void Copy() => Interlocked.Increment(ref _copied);
+
+ public void Keep() => Interlocked.Increment(ref _kept);
+
+ /// The frame keeps its proxy alone: with no original.
+ public WorkspaceFrame Missed(WorkspaceFrame entry, int index)
+ {
+ _missed.Enqueue((index, entry.FileName));
+ return entry with { Original = null, OriginalSha256 = null, OriginalBytes = 0 };
+ }
+
+ /// Says in the job's log what was copied (), and names the first frames without an original.
+ public void LogTo(JobContext context, string key, string title)
+ {
+ foreach (var (_, name) in _missed.OrderBy(m => m.Index).Take(Named)) context.Log(Loc.L("Archive.Log.WorkspaceOriginalMissing", title, name));
+ context.Log(Loc.L(key, title, Copied, Kept, Missing));
+ }
}
/// What a copy did with each frame, counted from the workers that download them at once.
diff --git a/src/Chronos.Data/Workspaces/WorkspaceOriginals.cs b/src/Chronos.Data/Workspaces/WorkspaceOriginals.cs
new file mode 100644
index 00000000..61c557a1
--- /dev/null
+++ b/src/Chronos.Data/Workspaces/WorkspaceOriginals.cs
@@ -0,0 +1,186 @@
+using System.Globalization;
+using System.Security.Cryptography;
+using System.Text;
+using Chronos.Core.Diagnostics;
+using Chronos.Core.Localization;
+using Chronos.Core.Model;
+using Chronos.Core.Project;
+using Chronos.Data.Archiving;
+using Chronos.Data.Projects;
+
+namespace Chronos.Data.Workspaces;
+
+///
+/// What "Proxies only" (phase 5c) does to a workspace project that holds its originals: files
+/// of the home holds with the same SHA-256, deleted; files of
+/// it does not (gone there, damaged, or another photo now), moved to the workspace's trash.
+/// names the files and the home's answer the plan read: an apply refuses a plan the
+/// project or the home changed since.
+///
+public sealed record WorkspaceProxiesOnlyPlan(Guid ProjectId, int Originals, long Bytes, int ToTrash, long TrashBytes, string Fingerprint)
+{
+ /// Nothing to remove: the project holds no originals.
+ public bool IsEmpty => Originals == 0 && ToTrash == 0;
+}
+
+///
+/// The originals of a workspace project (phase 5c): what a copy of them writes, and their removal. An original is never
+/// erased on a guess: a file is deleted only when the home holds a photo of its SHA-256, read from the file at that moment,
+/// that the home has not flagged missing or damaged; any other goes to the workspace's trash
+/// (<root>/.trash/<project id>-<ticks>/originals/, which the clean-up empties).
+///
+public static class WorkspaceOriginals
+{
+ /// The workspace's trash folder, under its root (the App's WorkspaceHost.TrashFolder).
+ public const string TrashFolder = ".trash";
+
+ ///
+ /// The bytes a copy of the sequence's originals writes from this PC's archive: all the archive has, or only those
+ /// lacks when it holds some already.
+ ///
+ public static Task ArchiveOriginalsBytesAsync(ChronosDatabase db, ArchiveService archives, Guid sequenceId, WorkspaceProject? project = null,
+ CancellationToken cancellationToken = default) =>
+ new WorkspaceCopyJobHandler(db, archives).OriginalsBytesAsync(sequenceId, project, cancellationToken);
+
+ ///
+ /// What "Proxies only" removes from , each original hashed now and weighed against
+ /// (the home's frames as read just before): nothing when it holds none.
+ ///
+ /// The media index is damaged or newer.
+ public static WorkspaceProxiesOnlyPlan PlanProxiesOnly(WorkspaceProject project, HomeFrames home)
+ {
+ ArgumentNullException.ThrowIfNull(project);
+ ArgumentNullException.ThrowIfNull(home);
+ var files = Files(project);
+ var held = HeldAtHome(home.Frames);
+ var fingerprint = new StringBuilder(home.FramesVersion.ToString(CultureInfo.InvariantCulture));
+ int deleted = 0, trashed = 0;
+ long deletedBytes = 0, trashedBytes = 0;
+ foreach (var file in files)
+ {
+ var (sha, bytes) = Hash(file);
+ var atHome = sha is not null && held.Contains(sha);
+ if (atHome) (deleted, deletedBytes) = (deleted + 1, deletedBytes + bytes);
+ else (trashed, trashedBytes) = (trashed + 1, trashedBytes + bytes);
+ fingerprint.Append('|').Append(Path.GetFileName(file)).Append(':').Append(sha).Append(':').Append(atHome ? 'd' : 't');
+ }
+ var digest = Convert.ToHexStringLower(SHA256.HashData(Encoding.UTF8.GetBytes(fingerprint.ToString())));
+ return new WorkspaceProxiesOnlyPlan(project.ProjectId, deleted, deletedBytes, trashed, trashedBytes, digest);
+ }
+
+ ///
+ /// Applies a plan of under the project's lease: planned again, refused when anything changed
+ /// (Errors.Workspace.MediaChanged); then the media index and the project file say proxies first (so a stop
+ /// half way leaves media that name no original that is gone), and last each original is deleted or trashed as the plan
+ /// says, hashed again as it goes.
+ ///
+ /// The files moved to the trash.
+ /// The project or the home changed since the plan.
+ /// Another writer holds the project.
+ public static async Task ApplyProxiesOnlyAsync(WorkspaceProject project, WorkspaceProxiesOnlyPlan plan, HomeFrames home, TimeSpan lockWait,
+ CancellationToken cancellationToken = default)
+ {
+ ArgumentNullException.ThrowIfNull(project);
+ ArgumentNullException.ThrowIfNull(plan);
+ ArgumentNullException.ThrowIfNull(home);
+ using (await project.LockAsync(lockWait, cancellationToken).ConfigureAwait(false))
+ {
+ var now = PlanProxiesOnly(project, home);
+ if (now.ProjectId != plan.ProjectId || now.Fingerprint != plan.Fingerprint) throw new InvalidOperationException(Loc.L("Errors.Workspace.MediaChanged"));
+ var media = project.ReadMedia();
+ var files = Files(project);
+ if (media is not null)
+ {
+ project.WriteMedia(media with
+ {
+ Kind = BundleMedia.Proxies,
+ Frames = [.. media.Frames.Select(f => f with { Original = null, OriginalSha256 = null, OriginalBytes = 0 })],
+ });
+ }
+ var bundle = project.ReadBundle();
+ if (bundle.Workspace is { } workspace && workspace.Media.Kind != BundleMedia.Proxies)
+ project.WriteBundle(bundle with { Workspace = workspace with { Media = workspace.Media with { Kind = BundleMedia.Proxies } } });
+ var trashed = DeleteOrTrash(project, files, home.Frames);
+ var staging = Path.Combine(project.OriginalsDirectory, ".staging");
+ if (Directory.Exists(staging)) TryDeleteFolder(staging);
+ if (Directory.Exists(project.OriginalsDirectory) && !Directory.EnumerateFileSystemEntries(project.OriginalsDirectory).Any()) TryDeleteFolder(project.OriginalsDirectory);
+ Log.Info("workspace", $"Originals of project {project.ProjectId} removed: {plan.Originals} deleted, {trashed} kept in {TrashFolder}");
+ return trashed;
+ }
+ }
+
+ ///
+ /// Deletes each of (originals of ) whose SHA-256, read now, is that of a
+ /// photo holds, not flagged missing or damaged; moves the others to the workspace's trash.
+ ///
+ /// The files moved to the trash.
+ internal static int DeleteOrTrash(WorkspaceProject project, IReadOnlyList files, IReadOnlyList home)
+ {
+ if (files.Count == 0) return 0;
+ var held = HeldAtHome(home);
+ string? trash = null;
+ var trashed = 0;
+ foreach (var file in files)
+ {
+ try
+ {
+ var (sha, _) = Hash(file);
+ if (sha is not null && held.Contains(sha))
+ {
+ File.Delete(file);
+ continue;
+ }
+ trash ??= Path.Combine(project.Store.Root, TrashFolder,
+ project.ProjectId.ToString("N") + "-" + DateTime.UtcNow.Ticks.ToString(CultureInfo.InvariantCulture), WorkspaceProject.OriginalsFolder);
+ Directory.CreateDirectory(trash);
+ File.Move(file, Path.Combine(trash, Path.GetFileName(file)), overwrite: true);
+ trashed++;
+ }
+ catch (Exception ex) when (ex is IOException or UnauthorizedAccessException)
+ {
+ Log.Warning("workspace", $"{file} could not be removed", ex);
+ }
+ }
+ return trashed;
+ }
+
+ /// The original files in the project's folder of originals (never a part a copy writes, nor its staging folder).
+ private static List Files(WorkspaceProject project)
+ {
+ if (!Directory.Exists(project.OriginalsDirectory)) return [];
+ return [.. Directory.EnumerateFiles(project.OriginalsDirectory)
+ .Where(f => !f.EndsWith(".part", StringComparison.OrdinalIgnoreCase))
+ .Order(StringComparer.OrdinalIgnoreCase)];
+ }
+
+ /// The SHA-256 (lower case) of the photos the home holds, flagged neither missing nor damaged.
+ private static HashSet HeldAtHome(IReadOnlyList home) =>
+ home.Where(f => !string.IsNullOrEmpty(f.Sha256) && (f.Flags & (FrameFlags.Missing | FrameFlags.Corrupt)) == 0)
+ .Select(f => f.Sha256!.ToLowerInvariant()).ToHashSet(StringComparer.Ordinal);
+
+ private static (string? Sha256, long Bytes) Hash(string file)
+ {
+ try
+ {
+ using var stream = new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.Read, 1 << 20, FileOptions.SequentialScan);
+ return (Convert.ToHexStringLower(SHA256.HashData(stream)), stream.Length);
+ }
+ catch (Exception ex) when (ex is IOException or UnauthorizedAccessException)
+ {
+ Log.Warning("workspace", $"{file} could not be read", ex);
+ return (null, 0);
+ }
+ }
+
+ private static void TryDeleteFolder(string folder)
+ {
+ try
+ {
+ Directory.Delete(folder, recursive: true);
+ }
+ catch (Exception ex) when (ex is IOException or UnauthorizedAccessException)
+ {
+ Log.Warning("workspace", $"{folder} could not be removed", ex);
+ }
+ }
+}
diff --git a/tests/Chronos.App.Tests/Workspace/WorkspaceFallbackTests.cs b/tests/Chronos.App.Tests/Workspace/WorkspaceFallbackTests.cs
index d635101f..31953661 100644
--- a/tests/Chronos.App.Tests/Workspace/WorkspaceFallbackTests.cs
+++ b/tests/Chronos.App.Tests/Workspace/WorkspaceFallbackTests.cs
@@ -266,7 +266,8 @@ public sealed class WorkspaceFallbackTests(TestContext context)
Assert.False(facts.TooLittleSpace, "the test drive has room for four small proxies");
var dialog = new OpenInWorkspaceDialogViewModel("Synthetic", await host.HomeProjectsAsync(bed.SequenceId), _ => { }, copy: facts);
- Assert.SequenceEqual([Loc.L("Editor.OpenWorkspace.Size"), Loc.L("Editor.OpenWorkspace.Free")], dialog.CopyFacts.Select(f => f.Label));
+ Assert.SequenceEqual([Loc.L("Editor.OpenWorkspace.Size"), Loc.L("Editor.OpenWorkspace.Free"), Loc.L("Editor.OpenWorkspace.Originals")], dialog.CopyFacts.Select(f => f.Label));
+ Assert.Equal(Formatting.Bytes(facts.OriginalsBytes!.Value), dialog.CopyFacts[2].Value);
Assert.Equal(Formatting.Bytes(facts.Bytes), dialog.CopyFacts[0].Value);
Assert.False(dialog.IsShortOfSpace);
diff --git a/tests/Chronos.App.Tests/Workspace/WorkspaceOpenPolicyTests.cs b/tests/Chronos.App.Tests/Workspace/WorkspaceOpenPolicyTests.cs
new file mode 100644
index 00000000..3d395033
--- /dev/null
+++ b/tests/Chronos.App.Tests/Workspace/WorkspaceOpenPolicyTests.cs
@@ -0,0 +1,185 @@
+using System.IO;
+using Chronos.App.Infrastructure;
+using Chronos.App.Shell;
+using Chronos.App.Tests.Editor;
+using Chronos.App.Tests.Windows;
+using Chronos.App.Workspace;
+using Chronos.Core.Archiving;
+using Chronos.Core.Codecs.Jpeg;
+using Chronos.Core.Localization;
+using Chronos.Core.Metadata;
+using Chronos.Core.Model;
+using Chronos.Core.Project;
+using Chronos.Core.Windows.Shell;
+using Chronos.Data;
+using Chronos.Data.Archiving;
+using Chronos.Data.Projects;
+using Chronos.Data.Workspaces;
+using Chronos.Testing;
+
+namespace Chronos.App.Tests.Workspace;
+
+///
+/// Phase 5c in Edit: "Open in workspace" offers the originals (Settings › Workspace › Media first, the proxies when the
+/// drive has no room), and Settings › Workspace › Open project picks the project a sequence with several opens.
+///
+[WindowsOnly]
+[Serial]
+[Timeout(300_000)]
+[Category("workspace")]
+public sealed class WorkspaceOpenPolicyTests(TestContext context)
+{
+ private static readonly SequenceProject Main = new() { Id = Guid.CreateVersion7(), SequenceId = Guid.CreateVersion7(), Name = ProjectService.MainName, IsMain = true };
+
+ [Test]
+ public void TheOpenDialogStartsWithTheMediaOfTheSettingsWhenTheyFit()
+ {
+ UiLanguage.Apply("en");
+ var room = new WorkspaceCopyFacts(10_000_000, 100_000_000_000, 4_000_000_000);
+ using var proxies = new OpenInWorkspaceDialogViewModel("Harbour", [Main], _ => { }, copy: room);
+ Assert.True(proxies is { IsProxies: true, IsOriginals: false, CanCopyOriginals: true });
+ Assert.Equal(BundleMedia.Proxies, proxies.Media);
+ Assert.Contains(proxies.CopyFacts, f => f.Label == Loc.L("Editor.OpenWorkspace.Originals"));
+
+ using var originals = new OpenInWorkspaceDialogViewModel("Harbour", [Main], _ => { }, copy: room, defaultMedia: BundleMedia.Originals);
+ Assert.True(originals is { IsOriginals: true, IsProxies: false, IsShortOfSpace: false });
+ Assert.Equal(Loc.L("Tooltips.Editor.OpenWorkspace.OriginalsReady"), originals.OriginalsToolTip);
+ originals.OpenCommand.Execute(null);
+ Assert.Equal(new OpenChoice(Main.Id, null, BundleMedia.Originals), originals.Choice.Result);
+
+ // The drive has room for the proxies, not the originals: they are offered disabled, why in the tooltip.
+ var tight = new WorkspaceCopyFacts(10_000_000, WorkspaceJobs.CopySpaceReserve + 20_000_000, 4_000_000_000);
+ using var refused = new OpenInWorkspaceDialogViewModel("Harbour", [Main], _ => { }, copy: tight, defaultMedia: BundleMedia.Originals);
+ Assert.True(refused is { IsProxies: true, CanCopyOriginals: false, IsShortOfSpace: false });
+ refused.IsOriginals = true;
+ Assert.False(refused.IsOriginals, "never chosen without room");
+ Assert.Equal(Loc.L("Tooltips.Editor.OpenWorkspace.NoRoomOriginals"), refused.OriginalsToolTip);
+ refused.OpenCommand.Execute(null);
+ Assert.Equal(BundleMedia.Proxies, refused.Choice.Result!.Media);
+
+ // Not measured (a server out of reach): offered, the copy measures again before it writes.
+ using var unmeasured = new OpenInWorkspaceDialogViewModel("Harbour", [Main], _ => { }, server: true);
+ Assert.True(unmeasured.CanCopyOriginals);
+ Assert.Equal(Loc.L("Tooltips.Editor.OpenWorkspace.OriginalsReady.Server"), unmeasured.OriginalsToolTip);
+ unmeasured.IsOriginals = true;
+ Assert.Equal(BundleMedia.Originals, unmeasured.Media);
+ }
+
+ [Test]
+ public Task TheOriginalsAreCopiedAndTheOpenProjectFollowsTheSettings() => ScreenHost.RunAsync(async () =>
+ {
+ var directory = context.CreateTempDirectory("workspace-open-policy");
+ var environment = AppEnvironment.From(AppPaths.Resolve(Directory.CreateDirectory(Path.Combine(directory, "app")).FullName,
+ Directory.CreateDirectory(Path.Combine(directory, "local")).FullName, []));
+ UiLanguage.Apply("en");
+ var settings = new AppSettings
+ {
+ Language = "en", FirstRunCompleted = true, CheckForUpdates = false, WorkspaceDefaultMedia = BundleMedia.Originals,
+ };
+ var services = new AppServices(environment, settings, () => { }, new NoDialogs());
+ await services.AttachAsync(await ChronosDatabase.OpenAsync(DatabaseOptions.Sqlite(environment.DatabasePath)), startEngine: true);
+ var shell = new ShellViewModel(services);
+ try
+ {
+ var sequenceId = await SequenceAsync(services, Path.Combine(directory, "archive"));
+ var section = shell.Sequence;
+
+ // The originals, as the settings say: copied beside the proxies, and the project's menu offers Proxies only.
+ shell.OpenSequence(sequenceId);
+ await WorkspaceTestBed.UntilAsync(() => section.Dialog is OpenInWorkspaceDialogViewModel, "Open in workspace asks the first time");
+ var ask = (OpenInWorkspaceDialogViewModel)section.Dialog!;
+ Assert.True(ask.IsOriginals, "the settings' media first");
+ ask.OpenCommand.Execute(null);
+ await WorkspaceTestBed.UntilAsync(() => section.Editor is not null, "the project opens once its media are copied");
+ var mainId = section.OpenProjectId!.Value;
+ var menu = Assert.NotNull(section.Editor!.Project);
+ Assert.True(menu.HoldsOriginals);
+ var media = services.Workspace.Store.Find(mainId)!.ReadMedia()!;
+ Assert.True(media.HoldsOriginals && media.Frames.All(f => f.HasOriginal), "every frame has its original");
+ Assert.True(settings.WorkspaceOpened.ContainsKey(mainId), "the open is recorded for the clean-up");
+ Assert.True(await section.ConfirmCloseAsync());
+ await section.CloseAsync();
+ shell.KeepSequence(null);
+
+ // A second project here, opened last.
+ var vertical = (await services.Archives!.Projects.CreateAsync(sequenceId, "Vertical", cancellationToken: context.Cancellation)).Id;
+ var checkout = await services.Workspace.CheckoutAsync(sequenceId, vertical, null);
+ await WorkspaceTestBed.UntilAsync(() => services.Database!.Jobs.GetAsync(checkout.Copy.Id).GetAwaiter().GetResult()?.State == JobState.Done, "its media are copied");
+ services.Workspace.Remember(sequenceId, vertical);
+
+ // Last used: Vertical.
+ settings.WorkspaceOpenProject = WorkspaceOpenPolicy.LastUsed;
+ Assert.Equal(vertical, await OpenAsync(shell, sequenceId));
+
+ // Main, whichever opened last.
+ settings.WorkspaceOpenProject = WorkspaceOpenPolicy.Main;
+ Assert.Equal(mainId, await OpenAsync(shell, sequenceId));
+
+ // Ask: the projects here are listed, the one opened last first; closing it opens nothing.
+ settings.WorkspaceOpenProject = WorkspaceOpenPolicy.Ask;
+ shell.OpenSequence(sequenceId);
+ await WorkspaceTestBed.UntilAsync(() => section.Dialog is OpenProjectDialogViewModel, "Open which project? asks");
+ var which = (OpenProjectDialogViewModel)section.Dialog!;
+ Assert.Equal(2, which.Projects.Count);
+ Assert.Equal(mainId, which.SelectedProject!.Entry.ProjectId);
+ which.CloseCommand.Execute(null);
+ await WorkspaceTestBed.UntilAsync(() => section.Dialog is null, "closed without a choice");
+ Assert.Null(section.Editor);
+ shell.KeepSequence(null);
+ shell.OpenSequence(sequenceId);
+ await WorkspaceTestBed.UntilAsync(() => section.Dialog is OpenProjectDialogViewModel, "Open which project? asks again");
+ which = (OpenProjectDialogViewModel)section.Dialog!;
+ which.SelectedProject = which.Projects.Single(p => p.Entry.ProjectId == vertical);
+ which.OpenCommand.Execute(null);
+ await WorkspaceTestBed.UntilAsync(() => section.Editor is not null, "the project chosen opens");
+ Assert.Equal(vertical, section.OpenProjectId);
+ }
+ finally
+ {
+ await shell.Sequence.CloseAsync();
+ shell.Dispose();
+ await services.DisposeAsync();
+ }
+ });
+
+ /// Opens the sequence in Edit as the settings say and returns the project that opened, closed again.
+ private static async Task OpenAsync(ShellViewModel shell, Guid sequenceId)
+ {
+ var section = shell.Sequence;
+ shell.OpenSequence(sequenceId);
+ await WorkspaceTestBed.UntilAsync(() => section.Editor is not null, "the project opens");
+ Assert.Null(section.Dialog);
+ var opened = section.OpenProjectId!.Value;
+ Assert.True(await section.ConfirmCloseAsync());
+ await section.CloseAsync();
+ shell.KeepSequence(null);
+ return opened;
+ }
+
+ /// An archive with one sequence of six synthetic photos, indexed.
+ private async Task SequenceAsync(AppServices services, string root)
+ {
+ var archives = services.Archives!;
+ var archive = await archives.CreateAsync(root, "Test");
+ var sequence = EditorFixture.Sequence(6);
+ var folder = Path.Combine(root, "Synthetic");
+ Directory.CreateDirectory(folder);
+ for (var i = 0; i < sequence.Count; i++)
+ {
+ using var image = sequence.Render(i);
+ var exif = ExifWriter.BuildJpegApp1(sequence.Metadata[i]);
+ File.WriteAllBytes(Path.Combine(folder, $"IMG_{i + 1:0000}.JPG"), JpegEncoder.Encode(image, new JpegEncodeOptions { Quality = 90, App1Segments = [exif[4..]] }));
+ }
+ await archives.Indexer.ScanAsync(archive, ScanMode.Full, null, _ => ValueTask.CompletedTask, context.Cancellation);
+ return (await services.Database!.Sequences.ListLocationsAsync(archive.Id, context.Cancellation)).Single().Id;
+ }
+
+ /// Dialogs that pick nothing and decline every confirmation.
+ private sealed class NoDialogs : IAppDialogs
+ {
+ public string? PickFolder(string title, string? initialFolder = null) => null;
+ public string? PickJsonFile(string title) => null;
+ public bool Confirm(ConfirmRequest request) => false;
+ public bool SignIn(string server, string? username, Func> signIn) => false;
+ }
+}
diff --git a/tests/Chronos.App.Tests/Workspace/WorkspaceSectionTests.cs b/tests/Chronos.App.Tests/Workspace/WorkspaceSectionTests.cs
index 85d629a0..aeaebcac 100644
--- a/tests/Chronos.App.Tests/Workspace/WorkspaceSectionTests.cs
+++ b/tests/Chronos.App.Tests/Workspace/WorkspaceSectionTests.cs
@@ -53,7 +53,7 @@ public sealed class WorkspaceSectionTests(TestContext context)
await WorkspaceTestBed.UntilAsync(() => section.Dialog is OpenInWorkspaceDialogViewModel, "Open in workspace asks the first time");
var ask = (OpenInWorkspaceDialogViewModel)section.Dialog!;
Assert.Equal(Loc.L("Editor.Project.Main"), ask.SelectedProject!.Label, "Main, made from the sequence's settings, is offered first");
- Assert.True(ask.IsProxies && !ask.CanCopyOriginals, "proxies; the originals come later");
+ Assert.True(ask.IsProxies && ask.CanCopyOriginals, "the proxies first, the originals offered (Settings › Workspace › Media)");
Assert.True(section.ShowsPicker && !section.IsLoading, "the picker shows behind the dialog, not the progress bar");
ask.OpenCommand.Execute(null);
Assert.Null(section.Dialog);
@@ -143,7 +143,8 @@ public sealed class WorkspaceSectionTests(TestContext context)
shell.OpenSequence(sequenceId);
await WorkspaceTestBed.UntilAsync(() => section.Dialog is OpenInWorkspaceDialogViewModel, "Open in workspace asks the first time");
var ask = (OpenInWorkspaceDialogViewModel)section.Dialog!;
- Assert.SequenceEqual([Loc.L("Editor.OpenWorkspace.Size"), Loc.L("Editor.OpenWorkspace.Free")], ask.CopyFacts.Select(f => f.Label), "the copy's facts reach the dialog");
+ Assert.SequenceEqual([Loc.L("Editor.OpenWorkspace.Size"), Loc.L("Editor.OpenWorkspace.Free"), Loc.L("Editor.OpenWorkspace.Originals")], ask.CopyFacts.Select(f => f.Label),
+ "the copy's facts reach the dialog");
ask.OpenCommand.Execute(null);
await WorkspaceTestBed.UntilAsync(() => toasts.Any(t => t.Title == Loc.L("Editor.Workspace.OpenFailed")), "the refusal is said");
var refused = toasts.Single(t => t.Title == Loc.L("Editor.Workspace.OpenFailed"));
diff --git a/tests/Chronos.Data.Tests/Workspaces/WorkspaceOriginalsTests.cs b/tests/Chronos.Data.Tests/Workspaces/WorkspaceOriginalsTests.cs
new file mode 100644
index 00000000..fe729fa9
--- /dev/null
+++ b/tests/Chronos.Data.Tests/Workspaces/WorkspaceOriginalsTests.cs
@@ -0,0 +1,187 @@
+using System.Security.Cryptography;
+using Chronos.Core.Localization;
+using Chronos.Core.Project;
+using Chronos.Data.Archiving;
+using Chronos.Core.Model;
+using Chronos.Data.Tests.Archiving;
+using Chronos.Data.Tests.Infrastructure;
+using Chronos.Data.Workspaces;
+using Chronos.Testing;
+
+namespace Chronos.Data.Tests.Workspaces;
+
+/// A workspace project's originals (phase 5c): their copy from the archive, checked and resumable, and "Proxies only".
+[Category("workspace")]
+public abstract class WorkspaceOriginalsTests(TestContext context) : DatabaseTest(context)
+{
+ private async Task FixtureAsync(int frames = 3, Func? configure = null)
+ {
+ var archive = Own(await ArchiveFixture.CreateAsync(Context, database: await NewOptionsAsync(multiConnection: true), configure: configure));
+ var fx = Own(await WorkspaceFixture.CreateAsync(Context, archive, frames));
+ // The archive's own proxies: the copy takes those, so a damaged original never changes a proxy.
+ await archive.Service.Images.BuildProxiesAsync(archive.Archive, fx.Sequence, force: false, 0, (_, _) => ValueTask.CompletedTask, fx.Ct);
+ return fx;
+ }
+
+ private static string Sha(string path) => Convert.ToHexStringLower(SHA256.HashData(File.ReadAllBytes(path)));
+
+ private static async Task<(WorkspaceStore Store, WorkspaceProject Project)> CheckoutOriginalsAsync(WorkspaceFixture fx)
+ {
+ var (store, sync) = fx.Workspace();
+ var main = await fx.Archive.Service.Projects.MainAsync(fx.Sequence.Id, fx.Ct);
+ var project = await sync.CheckoutAsync(fx.Home, main.Id, BundleMedia.Originals, fx.Ct);
+ return (store, project);
+ }
+
+ private string Source(WorkspaceFixture fx, int index) => fx.Archive.Paths.FromRelative(fx.Frames.Single(f => f.Index == index).RelativePath);
+
+ [Test]
+ public async Task ACopyOfTheOriginalsHoldsEachPhotoCheckedAgainstTheHome()
+ {
+ var fx = await FixtureAsync();
+ var (store, project) = await CheckoutOriginalsAsync(fx);
+ Assert.Equal(fx.Frames.Sum(f => f.Size), await WorkspaceOriginals.ArchiveOriginalsBytesAsync(fx.Db, fx.Archive.Service, fx.Sequence.Id, cancellationToken: fx.Ct));
+
+ await fx.CopyAsync(store, project.ProjectId, media: BundleMedia.Originals);
+ var media = project.ReadMedia()!;
+ Assert.True(media.HoldsOriginals);
+ Assert.Equal(BundleMedia.Originals, project.ReadBundle().Workspace!.Media.Kind);
+ foreach (var frame in fx.Frames)
+ {
+ var entry = media.Frames.Single(f => f.Id == frame.Id);
+ Assert.NotNull(entry.File);
+ var original = project.MediaFile(Assert.NotNull(entry.Original));
+ Assert.Equal(frame.Sha256!.ToLowerInvariant(), entry.OriginalSha256);
+ Assert.Equal(entry.OriginalSha256, Sha(original));
+ Assert.Equal(frame.Size, entry.OriginalBytes);
+ Assert.Equal(WorkspaceProject.OriginalName(frame.Id, frame.Sha256, Path.GetExtension(frame.RelativePath)), entry.Original);
+ }
+ Assert.Empty(Directory.GetFiles(project.OriginalsDirectory, "*.part"));
+ Assert.Equal(0L, await WorkspaceOriginals.ArchiveOriginalsBytesAsync(fx.Db, fx.Archive.Service, fx.Sequence.Id, project, fx.Ct));
+
+ // The frames read by a local render are the originals.
+ Assert.SequenceEqual(media.Frames.Select(f => f.Original), media.ToOriginalFrames().Select(f => f.RelativePath));
+ }
+
+ [Test]
+ public async Task AnOriginalThatIsGoneOrDamagedAtTheHomeLeavesItsFrameProxied()
+ {
+ var fx = await FixtureAsync();
+ var (store, project) = await CheckoutOriginalsAsync(fx);
+ File.Delete(Source(fx, 0));
+ var damaged = Source(fx, 1);
+ var bytes = File.ReadAllBytes(damaged);
+ bytes[^3] ^= 0x5A;
+ File.WriteAllBytes(damaged, bytes);
+
+ await fx.CopyAsync(store, project.ProjectId, media: BundleMedia.Originals);
+ var media = project.ReadMedia()!;
+ Assert.True(media.HoldsOriginals);
+ var gone = media.Frames.Single(f => f.Index == 0);
+ var mismatched = media.Frames.Single(f => f.Index == 1);
+ Assert.Null(gone.Original);
+ Assert.Null(mismatched.Original);
+ Assert.NotNull(mismatched.File);
+ Assert.NotNull(media.Frames.Single(f => f.Index == 2).Original);
+ Assert.Equal(1, Directory.GetFiles(project.OriginalsDirectory).Length);
+ Assert.True(media.ToOriginalFrames().Count(f => (f.Flags & FrameFlags.Missing) != 0) == 2, "a render counts the two frames without an original");
+ }
+
+ [Test]
+ public async Task ACopyStoppedAmongTheOriginalsResumesWithWhatItHad()
+ {
+ var fx = await FixtureAsync();
+ var (store, project) = await CheckoutOriginalsAsync(fx);
+ await fx.CopyAsync(store, project.ProjectId, media: BundleMedia.Originals);
+ var media = project.ReadMedia()!;
+ var kept = project.MediaFile(media.Frames[0].Original!);
+ var written = File.GetLastWriteTimeUtc(kept);
+ var lost = project.MediaFile(media.Frames[2].Original!);
+ File.Delete(lost);
+ File.WriteAllBytes(Path.Combine(project.OriginalsDirectory, media.Frames[2].Id.ToString("N") + ".part"), [1, 2, 3]);
+
+ var row = (await fx.Db.Sequences.GetAsync(fx.Sequence.Id))!;
+ await fx.CopyAsync(store, project.ProjectId, checkpoint: $"originals:2:{row.FramesVersion}:{row.Folder}", media: BundleMedia.Originals);
+ Assert.Equal(written, File.GetLastWriteTimeUtc(kept));
+ Assert.Equal(media.Frames[2].OriginalSha256, Sha(lost));
+ Assert.Empty(Directory.GetFiles(project.OriginalsDirectory, "*.part"));
+ Assert.Equal(3, project.ReadMedia()!.Frames.Count(f => f.HasOriginal));
+ }
+
+ [Test]
+ public async Task TheOriginalsAreNotCopiedWhenTheDriveLacksTheirRoom()
+ {
+ long? free = null;
+ var fx = await FixtureAsync(configure: options => options with { FreeSpace = _ => free });
+ var (store, project) = await CheckoutOriginalsAsync(fx);
+ await fx.CopyAsync(store, project.ProjectId);
+ free = WorkspaceJobs.CopySpaceReserve + fx.Frames.Sum(f => f.Size) - 1;
+
+ var job = await fx.CopyAsync(store, project.ProjectId, JobState.Failed, media: BundleMedia.Originals);
+ var parts = Loc.L("Errors.Workspace.DiskFull", "\u0001").Split('\u0001');
+ Assert.True(job.Error!.StartsWith(parts[0], StringComparison.Ordinal), job.Error);
+ Assert.False(project.ReadMedia()!.HoldsOriginals);
+ Assert.False(Directory.Exists(project.OriginalsDirectory) && Directory.EnumerateFiles(project.OriginalsDirectory).Any());
+ }
+
+ [Test]
+ public async Task ProxiesOnlyDeletesWhatTheHomeHoldsAndTrashesTheRest()
+ {
+ var fx = await FixtureAsync();
+ var (store, project) = await CheckoutOriginalsAsync(fx);
+ await fx.CopyAsync(store, project.ProjectId, media: BundleMedia.Originals);
+ var media = project.ReadMedia()!;
+ // The home lost frame 1's photo: its only copy is the workspace's, which goes to the trash, not away.
+ var lostAtHome = project.MediaFile(media.Frames[1].Original!);
+ var home = await fx.Home.FramesAsync(fx.Ct);
+ home = home with { Frames = [.. home.Frames.Select(f => f.Index == 1 ? f with { Flags = f.Flags | FrameFlags.Missing } : f)] };
+
+ var plan = WorkspaceOriginals.PlanProxiesOnly(project, home);
+ Assert.Equal((2, 1), (plan.Originals, plan.ToTrash));
+ Assert.Equal(fx.Frames.Where(f => f.Index != 1).Sum(f => f.Size), plan.Bytes);
+ var trashed = await WorkspaceOriginals.ApplyProxiesOnlyAsync(project, plan, home, TimeSpan.FromSeconds(10), fx.Ct);
+ Assert.Equal(1, trashed);
+ Assert.False(Directory.Exists(project.OriginalsDirectory));
+ var after = project.ReadMedia()!;
+ Assert.False(after.HoldsOriginals);
+ Assert.True(after.Frames.All(f => f.Original is null && f.File is not null));
+ Assert.Equal(BundleMedia.Proxies, project.ReadBundle().Workspace!.Media.Kind);
+ var inTrash = Directory.GetFiles(Path.Combine(store.Root, WorkspaceOriginals.TrashFolder), Path.GetFileName(lostAtHome), SearchOption.AllDirectories);
+ Assert.Equal(1, inTrash.Length);
+ Assert.Equal(media.Frames[1].OriginalSha256, Sha(inTrash[0]));
+ }
+
+ [Test]
+ public async Task ProxiesOnlyRefusesAPlanTheFilesChangedSince()
+ {
+ var fx = await FixtureAsync();
+ var (store, project) = await CheckoutOriginalsAsync(fx);
+ await fx.CopyAsync(store, project.ProjectId, media: BundleMedia.Originals);
+ var home = await fx.Home.FramesAsync(fx.Ct);
+ var plan = WorkspaceOriginals.PlanProxiesOnly(project, home);
+ Assert.Equal((3, 0), (plan.Originals, plan.ToTrash));
+
+ var changed = project.MediaFile(project.ReadMedia()!.Frames[0].Original!);
+ File.AppendAllText(changed, "x");
+ var error = await Assert.ThrowsAsync(() => WorkspaceOriginals.ApplyProxiesOnlyAsync(project, plan, home, TimeSpan.FromSeconds(10), fx.Ct));
+ Assert.Equal(Loc.L("Errors.Workspace.MediaChanged"), error.Message);
+ Assert.True(project.ReadMedia()!.HoldsOriginals);
+ Assert.Equal(3, Directory.GetFiles(project.OriginalsDirectory).Length);
+
+ // Planned again, the changed file is no photo the home holds: it goes to the trash.
+ var again = WorkspaceOriginals.PlanProxiesOnly(project, home);
+ Assert.Equal((2, 1), (again.Originals, again.ToTrash));
+ Assert.Equal(1, await WorkspaceOriginals.ApplyProxiesOnlyAsync(project, again, home, TimeSpan.FromSeconds(10), fx.Ct));
+ }
+}
+
+public sealed class SqliteWorkspaceOriginalsTests(TestContext context) : WorkspaceOriginalsTests(context)
+{
+ protected override Backend Backend => Backend.SqliteFile;
+}
+
+[RequiresEnvironment(PgTestServer.Variable)]
+public sealed class PostgresWorkspaceOriginalsTests(TestContext context) : WorkspaceOriginalsTests(context)
+{
+ protected override Backend Backend => Backend.Postgres;
+}
diff --git a/tests/Chronos.Data.Tests/Workspaces/WorkspaceTests.cs b/tests/Chronos.Data.Tests/Workspaces/WorkspaceTests.cs
index b93d3b77..9b43d418 100644
--- a/tests/Chronos.Data.Tests/Workspaces/WorkspaceTests.cs
+++ b/tests/Chronos.Data.Tests/Workspaces/WorkspaceTests.cs
@@ -192,14 +192,17 @@ public abstract class WorkspaceTests(TestContext context) : DatabaseTest(context
}
[Test]
- public async Task CopyingTheOriginalsIsRefusedForNowWithItsReason()
+ public async Task CopyingTheOriginalsCopiesTheProxiesToo()
{
var fx = await FixtureAsync(2);
var (store, sync) = fx.Workspace();
var main = await fx.Archive.Service.Projects.MainAsync(fx.Sequence.Id, fx.Ct);
- await sync.CheckoutAsync(fx.Home, main.Id, BundleMedia.Originals, fx.Ct);
- var job = await fx.CopyAsync(store, main.Id, JobState.Failed, media: BundleMedia.Originals);
- Assert.Equal(Loc.L("Errors.Workspace.OriginalsLater"), job.Error);
+ var project = await sync.CheckoutAsync(fx.Home, main.Id, BundleMedia.Originals, fx.Ct);
+ await fx.CopyAsync(store, main.Id, media: BundleMedia.Originals);
+ var media = project.ReadMedia()!;
+ Assert.True(media.HoldsOriginals);
+ Assert.True(media.Frames.All(f => f.HasOriginal), "every frame has its original");
+ Assert.Equal(media.Frames.Count, (await sync.OpenAsync(main.Id, fx.Ct)).Media!.Frames.Count);
}
[Test]
diff --git a/tests/Chronos.Server.Tests/Workspaces/ServerWorkspaceTests.cs b/tests/Chronos.Server.Tests/Workspaces/ServerWorkspaceTests.cs
index d53335c8..a2b8dd27 100644
--- a/tests/Chronos.Server.Tests/Workspaces/ServerWorkspaceTests.cs
+++ b/tests/Chronos.Server.Tests/Workspaces/ServerWorkspaceTests.cs
@@ -197,14 +197,45 @@ public sealed class ServerWorkspaceTests(TestContext context)
// Another server's job for this project, or this server's for another sequence: refused, nothing downloaded.
var other = await a.EnqueueAsync(WorkspaceJobs.CopyFromServer(sid, Title, "https://elsewhere:8443", a.Store.Root, main.Id));
Assert.Equal(Loc.L("Errors.Workspace.OtherHome"), (await a.WaitAsync(other.Id, JobState.Failed)).Error);
- var originals = await a.EnqueueAsync(WorkspaceJobs.CopyFromServer(sid, Title, a.Server, a.Store.Root, main.Id, BundleMedia.Originals));
- Assert.Equal(Loc.L("Errors.Workspace.OriginalsLater"), (await a.WaitAsync(originals.Id, JobState.Failed)).Error);
a.Connected = false;
var signedOut = await a.EnqueueAsync(WorkspaceJobs.CopyFromServer(sid, Title, a.Server, a.Store.Root, main.Id));
Assert.Equal(Loc.L("Errors.Sync.NotConnected"), (await a.WaitAsync(signedOut.Id, JobState.Failed)).Error);
Assert.Null(a.Store.Find(main.Id)!.ReadMedia());
}
+ [Test]
+ public async Task TheOriginalsAreDownloadedCheckedAndKeptBesideTheProxies()
+ {
+ await using var server = await TestServer.StartAsync(context, services: ArchiveModule.Register);
+ var (_, _, token) = await server.UserAsync("alberto", "editor");
+ var sid = await SequenceAsync(server, token, 3);
+ await using var a = await WorkspaceClient.StartAsync(context, server, token, "PC-A");
+ var home = a.Home(sid);
+ var main = (await home.ListAsync(context.Cancellation)).Single();
+ var project = await a.Sync.CheckoutAsync(home, main.Id, BundleMedia.Originals, context.Cancellation);
+ var library = Listing(a.Archive.RootPath);
+
+ var job = await a.EnqueueAsync(WorkspaceJobs.CopyFromServer(sid, Title, a.Server, a.Store.Root, main.Id, BundleMedia.Originals));
+ job = await a.WaitAsync(job.Id, JobState.Done);
+ Assert.Contains(Loc.L("Archive.Log.WorkspaceOriginalsDownloaded", Title, 3, 0, 0), job.LogTail!);
+ var media = Assert.NotNull(project.ReadMedia());
+ Assert.True(media.HoldsOriginals);
+ Assert.Equal(BundleMedia.Originals, project.ReadBundle().Workspace!.Media.Kind);
+ var manifest = await a.Client.GetManifestAsync(sid, context.Cancellation);
+ foreach (var (frame, entry) in manifest.Frames.OrderBy(f => f.Index).Zip(media.Frames))
+ {
+ Assert.Equal(frame.Sha256.ToLowerInvariant(), entry.OriginalSha256);
+ Assert.Equal(entry.OriginalSha256, Sha(project.MediaFile(Assert.NotNull(entry.Original))));
+ Assert.NotNull(entry.File);
+ }
+ Assert.False(Directory.Exists(Path.Combine(project.OriginalsDirectory, ".staging")));
+ Assert.SequenceEqual(library, Listing(a.Archive.RootPath), "this PC's library is as it was");
+
+ // Again: every original held is kept, hashed again.
+ job = await a.WaitAsync((await a.EnqueueAsync(WorkspaceJobs.CopyFromServer(sid, Title, a.Server, a.Store.Root, main.Id, BundleMedia.Originals))).Id, JobState.Done);
+ Assert.Contains(Loc.L("Archive.Log.WorkspaceOriginalsDownloaded", Title, 0, 3, 0), job.LogTail!);
+ }
+
[Test]
public async Task AProjectDeletedOnTheServerIsGoneAndItsCopyStays()
{