diff --git a/i18n/en/Import.resx b/i18n/en/Import.resx index 994d42f8..051b03ba 100644 --- a/i18n/en/Import.resx +++ b/i18n/en/Import.resx @@ -34,6 +34,7 @@ No photo on the card matches a verified copy.long Not enough free space at the destination.long Fix the name templates first.long + The whole folder is one sequence per format.long Delete Delete {0:N0} photo from the card?long Delete {0:N0} photos from the card?long @@ -118,9 +119,12 @@ {0} is no longer available.long Import into folder Import from folder + Sessions + Whole folder After import Destination Duplicates + Group Names Scan File template @@ -176,6 +180,8 @@ Verify hashes Adds to · {0}long adds + {0:N0} camera + {0:N0} cameras Camera Frames · interval Sequence diff --git a/i18n/en/Tooltips.resx b/i18n/en/Tooltips.resx index 13c6080c..f21ab5fe 100644 --- a/i18n/en/Tooltips.resx +++ b/i18n/en/Tooltips.resx @@ -303,6 +303,7 @@ After the import, offer the Windows format dialog once every copy is verified Open the Windows format dialog Minutes without photos that start a new session + Split the folder into sessions by camera, format and pauses, as for a card Photos imported from this card before (same name, size and time) Leave the card untouched Keep every photo on the card @@ -331,6 +332,7 @@ Look inside every subfolder of the source Insert a token into the template used last Hash while copying and read the destination back before the source is touched + One sequence per file format for the whole folder, subfolders included when scanned; cameras and pauses are ignored Stop this job; Retry continues later from its last checkpoint Hide finished jobs from the list; they stay in the history Every job diff --git a/i18n/it/Import.it.resx b/i18n/it/Import.it.resx index 4f4659a8..53caab1d 100644 --- a/i18n/it/Import.it.resx +++ b/i18n/it/Import.it.resx @@ -34,6 +34,7 @@ Nessuna foto della scheda corrisponde a una copia verificata. Spazio libero insufficiente nella destinazione. Correggi prima i modelli dei nomi. + La cartella intera è una sequenza per formato. Elimina Eliminare {0:N0} foto dalla scheda? Eliminare {0:N0} foto dalla scheda? @@ -116,9 +117,12 @@ {0} non è più disponibile. Importa nella cartella Importa dalla cartella + Sessioni + Cartella intera Dopo l'importazione Destinazione Doppioni + Raggruppa Nomi Scansione Modello file @@ -172,6 +176,8 @@ Verifica hash Si aggiunge a · {0} esistente + {0:N0} fotocamera + {0:N0} fotocamere Fotocamera Fotogrammi · intervallo Sequenza diff --git a/i18n/it/Tooltips.it.resx b/i18n/it/Tooltips.it.resx index c77b143f..02e5343c 100644 --- a/i18n/it/Tooltips.it.resx +++ b/i18n/it/Tooltips.it.resx @@ -302,6 +302,7 @@ Dopo l'importazione, proponi la formattazione di Windows quando ogni copia è verificata Apri la formattazione di Windows Minuti senza foto che avviano una nuova sessione + Dividi la cartella in sessioni per fotocamera, formato e pause, come per una scheda Foto già importate da questa scheda (stesso nome, dimensione e data) Lascia intatta la scheda Mantieni tutte le foto sulla scheda @@ -330,6 +331,7 @@ Cerca in ogni sottocartella della sorgente Inserisci un segnaposto nell'ultimo modello usato Calcola l'hash durante la copia e rileggi la destinazione prima di toccare la sorgente + Una sequenza per formato di file per l'intera cartella, sottocartelle comprese se incluse; fotocamere e pause sono ignorate Ferma questa attività; Riprova la continua più tardi dall'ultimo punto di ripresa Nasconde le attività concluse dall'elenco; restano nella cronologia Tutte le attività diff --git a/src/Chronos.App/Import/ImportChoices.cs b/src/Chronos.App/Import/ImportChoices.cs index d343721b..76142619 100644 --- a/src/Chronos.App/Import/ImportChoices.cs +++ b/src/Chronos.App/Import/ImportChoices.cs @@ -15,13 +15,18 @@ public enum ImportDestinationKind /// /// The Import form (Import.html): scan, sessions, names, duplicates and after-import choices. Remembered per card as -/// its ; a session gap of 0 minutes in a profile means "do not split into sessions". +/// its ; a session gap of 0 minutes in a profile means "do not split into sessions". The +/// grouping is not in a profile: a card always imports by sessions, and a picked folder starts with them. /// public sealed record ImportChoices { public ImportDestinationKind Destination { get; init; } = ImportDestinationKind.Archive; public string? FolderPath { get; init; } public bool IncludeSubfolders { get; init; } = true; + + /// "Group" (a picked folder only): its sessions, or the whole folder as one sequence per format. + public ImportGrouping Grouping { get; init; } = ImportGrouping.Sessions; + public bool SplitSessions { get; init; } = true; public int GapMinutes { get; init; } = 45; public int MinimumFrames { get; init; } = 8; @@ -78,6 +83,7 @@ public sealed record ImportChoices IncludeSubfolders = IncludeSubfolders, VerifyCopies = VerifyCopies, Duplicates = Duplicates, + Grouping = Grouping, Titles = titles ?? new Dictionary(), SplitBefore = splits ?? new HashSet(), MergeAt = merges ?? new HashSet(), @@ -88,6 +94,7 @@ public sealed record ImportChoices previous.Destination != Destination || (Destination == ImportDestinationKind.Folder && !string.Equals(previous.FolderPath, FolderPath, StringComparison.OrdinalIgnoreCase)) || previous.IncludeSubfolders != IncludeSubfolders + || previous.Grouping != Grouping || previous.SplitSessions != SplitSessions || (SplitSessions && previous.GapMinutes != GapMinutes) || previous.MinimumFrames != MinimumFrames diff --git a/src/Chronos.App/Import/ImportPlans.cs b/src/Chronos.App/Import/ImportPlans.cs index c1551a3f..791befeb 100644 --- a/src/Chronos.App/Import/ImportPlans.cs +++ b/src/Chronos.App/Import/ImportPlans.cs @@ -1,4 +1,5 @@ using Chronos.App.Infrastructure; +using Chronos.App.Settings; using Chronos.Core.Archiving; using Chronos.Core.Import; using Chronos.Core.Localization; @@ -40,7 +41,8 @@ public sealed record PlanTotals(int Sessions, int Photos, int CopyCount, long Co } /// One row of the session table: what the planner grouped and where it goes. -/// Title the sequence gets when the user types none (its folder name). +/// The title the user typed; null when none. +/// Title the sequence gets when the user types none (a whole folder's name, else its folder name). /// The session starts a new sequence (false: it continues one imported before). /// Card path of the session's first photo (where a merge joins it to the previous session). /// Number of the earlier session of the same camera and format it can merge into; null for none. @@ -116,10 +118,19 @@ public static class ImportPlans ], }; - /// The plan with the typed session titles (when no template uses {title}, so no name changes). + /// + /// The plan with the typed session titles (when no template uses {title}, so no name changes); a session + /// without one keeps its default title (a whole folder's name). + /// public static ImportPlan WithTitles(ImportPlan plan, IReadOnlyDictionary titles) => plan with { - Sessions = [.. plan.Sessions.Select(s => s with { Title = titles.TryGetValue(s.SessionKey, out var t) && !string.IsNullOrWhiteSpace(t) ? t.Trim() : null })], + Sessions = + [ + .. plan.Sessions.Select(s => s with + { + Title = titles.TryGetValue(s.SessionKey, out var t) && !string.IsNullOrWhiteSpace(t) ? t.Trim() : s.DefaultTitle, + }), + ], }; /// Folders of sequences already in the archive (a session there continues it). @@ -135,20 +146,35 @@ public static class ImportPlans ImportDestinationKind.Folder => s.Folder, _ => Loc.L(isNew ? "Import.Session.New" : "Import.Session.Adds", s.Folder), }; - return new PlanSessionRow(s.Number, s.SessionKey, s.Title, leaf, TimeText(s), FramesText(s), Formatting.Bytes(s.Items.Sum(i => i.Size)), - PathTemplate.CameraName(s.CameraMake, s.CameraModel), target, isNew, s.Items.Count > 0 ? s.Items[0].SourcePath : string.Empty, + // The field shows a typed title only; the default (a whole folder's name, else the folder's) is its placeholder. + var typed = s.Title is { } title && title != s.DefaultTitle ? title : null; + return new PlanSessionRow(s.Number, s.SessionKey, typed, s.DefaultTitle ?? leaf, TimeText(s), FramesText(s), + Formatting.Bytes(s.Items.Sum(i => i.Size)), CameraText(plan, s), target, isNew, s.Items.Count > 0 ? s.Items[0].SourcePath : string.Empty, MergeTarget(plan, s)?.Number); }), ]; + /// + /// "Canon EOS R5"; for a whole folder, one row per format, the format too and how many bodies took the photos: + /// "Canon EOS R5 · CR3", "2 cameras · JPEG". + /// + private static string CameraText(ImportPlan plan, PlannedSession s) + { + var camera = PathTemplate.CameraName(s.CameraMake, s.CameraModel); + if (plan.Grouping != ImportGrouping.WholeFolder) return camera; + var bodies = s.Cameras > 1 ? Loc.Plural("Import.Session.Cameras", s.Cameras) : camera; + return string.Join(Loc.Separator, new[] { bodies, FormatNames.Of(s.Format) }.Where(t => t.Length > 0)); + } + /// /// The session joins when the user merges it (BRIEF §13): the one before it of the same /// camera body and format, whose last photo precedes its first; sessions of other cameras or formats never merge. + /// A whole folder's parts (split by the user) join the part before them of the same format, whatever the camera. /// public static PlannedSession? MergeTarget(ImportPlan plan, PlannedSession session) => plan.Sessions.TakeWhile(s => !ReferenceEquals(s, session)) - .LastOrDefault(s => s.CameraMake == session.CameraMake && s.CameraModel == session.CameraModel && s.CameraSerial == session.CameraSerial - && s.Format == session.Format); + .LastOrDefault(s => s.Format == session.Format && (plan.Grouping == ImportGrouping.WholeFolder + || (s.CameraMake == session.CameraMake && s.CameraModel == session.CameraModel && s.CameraSerial == session.CameraSerial))); /// "Show plan": every file and where it goes, with the conflicts the plan resolved or reports. public static PlanDetails Details(ImportPlan plan, ImportDestinationKind destination, bool verify) diff --git a/src/Chronos.App/Sections/ImportView.xaml b/src/Chronos.App/Sections/ImportView.xaml index 3cd01bdf..bced0ad8 100644 --- a/src/Chronos.App/Sections/ImportView.xaml +++ b/src/Chronos.App/Sections/ImportView.xaml @@ -355,17 +355,27 @@ + + + + + + + + - - + + - + diff --git a/src/Chronos.App/Sections/ImportViewModel.cs b/src/Chronos.App/Sections/ImportViewModel.cs index 23b91557..22c53277 100644 --- a/src/Chronos.App/Sections/ImportViewModel.cs +++ b/src/Chronos.App/Sections/ImportViewModel.cs @@ -120,8 +120,10 @@ public sealed class ImportViewModel : SectionViewModel // The path is checked only while Folder is the destination; typing one chooses Folder, as "…" does. FolderPath = new EditableField(string.Empty, text => IsFolderDestination ? ArchiveLocations.Validate(text) : null, text => Choices = _choices with { FolderPath = text, Destination = ImportDestinationKind.Folder }, () => Loc.L("Tooltips.Import.FolderPath")); - GapMinutes = NumberField(1, 24 * 60, () => _choices.GapMinutes, n => Choices = _choices with { GapMinutes = n }, "Tooltips.Import.Gap"); - MinimumFrames = NumberField(1, 1000, () => _choices.MinimumFrames, n => Choices = _choices with { MinimumFrames = n }, "Tooltips.Import.MinimumFrames"); + GapMinutes = NumberField(1, 24 * 60, () => _choices.GapMinutes, n => Choices = _choices with { GapMinutes = n }, + () => SessionToolTip("Tooltips.Import.Gap")); + MinimumFrames = NumberField(1, 1000, () => _choices.MinimumFrames, n => Choices = _choices with { MinimumFrames = n }, + () => SessionToolTip("Tooltips.Import.MinimumFrames")); FolderTemplate = new EditableField(_choices.FolderTemplate, t => NamingPreview.Validate(t, isFolder: true), t => Choices = _choices with { FolderTemplate = t }, () => Loc.L("Tooltips.Import.FolderTemplate")); FileTemplate = new EditableField(_choices.FileTemplate, t => NamingPreview.Validate(t, isFolder: false), @@ -264,6 +266,11 @@ public sealed class ImportViewModel : SectionViewModel if (previous == value) return; _choices = value; if (value.Destination != previous.Destination) FolderPath.Refresh(); + if (value.Grouping != previous.Grouping) + { + GapMinutes.Refresh(); + MinimumFrames.Refresh(); + } OnPropertyChanged(string.Empty); if (value.NeedsNewPlan(previous) || (value.TitlesChangeNames != previous.TitlesChangeNames && _titles.Count > 0)) SchedulePlan(); else ApplyPlan(); @@ -320,6 +327,37 @@ public sealed class ImportViewModel : SectionViewModel set => Choices = _choices with { IncludeSubfolders = value }; } + /// "Group" is offered for a picked folder only (BRIEF §12.4): a card or a drive always splits into sessions. + public bool CanGroupFolder => _selected?.Kind == SourceKind.Folder; + + public bool GroupBySessions + { + get => !IsWholeFolder; + set + { + if (value) SetGrouping(ImportGrouping.Sessions); + } + } + + /// The picked folder is one sequence per format: camera bodies and pauses are ignored. + public bool GroupWholeFolder + { + get => IsWholeFolder; + set + { + if (value && CanGroupFolder) SetGrouping(ImportGrouping.WholeFolder); + } + } + + private bool IsWholeFolder => CanGroupFolder && _choices.Grouping == ImportGrouping.WholeFolder; + + /// Sessions, their gap and their minimum do not apply to a whole folder. + public bool SessionsApply => !IsWholeFolder; + + public bool GapApplies => SessionsApply && _choices.SplitSessions; + + public string SessionsToolTip => SessionToolTip("Tooltips.Import.Sessions"); + public bool SplitSessions { get => _choices.SplitSessions; @@ -340,6 +378,18 @@ public sealed class ImportViewModel : SectionViewModel public IReadOnlyList Tokens { get; } public ICommand InsertTokenCommand { get; } + private void SetGrouping(ImportGrouping grouping) + { + if (_choices.Grouping == grouping) return; + // Splits and merges were made on the other grouping's sessions. + _splits.Clear(); + _merges.Clear(); + Choices = _choices with { Grouping = grouping }; + } + + /// A session control's tooltip, or why it does not apply to a whole folder. + private string SessionToolTip(string key) => IsWholeFolder ? Loc.L("Import.Blocked.WholeFolder") : Loc.L(key); + /// The view tells which template field the "{ }" menu writes into. public void SetActiveTemplate(bool folder) => _activeTemplateIsFolder = folder; @@ -966,7 +1016,8 @@ public sealed class ImportViewModel : SectionViewModel { if (!cts.IsCancellationRequested) SetStatus(ImportPhase.Planning, Loc.Plural("Import.Status.ReadingCount", n)); }); - var options = _choices.ToOptions(_titles, _splits, _merges); + // A card or a drive is always planned by sessions, whatever a folder chose before. + var options = _choices.ToOptions(_titles, _splits, _merges) with { Grouping = IsWholeFolder ? ImportGrouping.WholeFolder : ImportGrouping.Sessions }; var cardId = _selected.Kind == SourceKind.Card ? await RegisteredCardIdAsync() : null; try { @@ -1456,11 +1507,11 @@ public sealed class ImportViewModel : SectionViewModel else dispatcher.BeginInvoke(action); } - private static EditableField NumberField(int min, int max, Func current, Action commit, string toolTipKey) => + private static EditableField NumberField(int min, int max, Func current, Action commit, Func toolTip) => new(current().ToString(Loc.Culture), text => int.TryParse(text.Trim(), NumberStyles.Integer, Loc.Culture, out var n) && n >= min && n <= max ? null : Loc.L("Errors.Number.Range", min.ToString("N0", Loc.Culture), max.ToString("N0", Loc.Culture)), text => commit(int.Parse(text.Trim(), NumberStyles.Integer, Loc.Culture)), - () => Loc.L(toolTipKey)); + toolTip); } diff --git a/src/Chronos.Core/Import/ImportPlan.cs b/src/Chronos.Core/Import/ImportPlan.cs index 84e12bc3..aa514edd 100644 --- a/src/Chronos.Core/Import/ImportPlan.cs +++ b/src/Chronos.Core/Import/ImportPlan.cs @@ -14,6 +14,19 @@ public enum DuplicatePolicy ImportAgain, } +/// How the planner turns the photos of a source into sequences (BRIEF §12.4, §13). +public enum ImportGrouping +{ + /// One session per camera body and file format, split where the pause between two photos exceeds the gap. + Sessions, + + /// + /// The whole source (a folder picked on this PC) is one sequence per file format: camera bodies and pauses are + /// ignored. RAW+JPEG of the same shots stays two sequences, so no frame is ever doubled. + /// + WholeFolder, +} + /// Card import settings (BRIEF §12.4, §13); the defaults are the brief's. public sealed record ImportOptions { @@ -25,6 +38,9 @@ public sealed record ImportOptions public bool VerifyCopies { get; init; } = true; public DuplicatePolicy Duplicates { get; init; } = DuplicatePolicy.Skip; + /// Sessions, or the whole source as one sequence per format (the session gap and minimum then do not apply). + public ImportGrouping Grouping { get; init; } = ImportGrouping.Sessions; + /// Card paths where the user split a session (a new session starts at that photo). public IReadOnlySet SplitBefore { get; init; } = new HashSet(); @@ -65,7 +81,10 @@ public sealed record PlannedItem public string? Sha256 { get; init; } } -/// A session of the plan: one camera body and file format, one sequence in the archive. +/// +/// A session of the plan: one camera body and file format (a whole folder: one file format), one sequence in the +/// archive. Its camera is the one of its first photo. +/// public sealed record PlannedSession { /// 1-based, in order of start time across the import. @@ -78,6 +97,10 @@ public sealed record PlannedSession public string? CameraMake { get; init; } public string? CameraModel { get; init; } public string? CameraSerial { get; init; } + + /// Camera bodies among its photos: 1 for a session, possibly more for a whole folder. + public int Cameras { get; init; } = 1; + public ImageFileFormat Format { get; init; } public DateTime Start { get; init; } public DateTime End { get; init; } @@ -86,6 +109,13 @@ public sealed record PlannedSession public required string Folder { get; init; } public string? Title { get; init; } + + /// + /// The title when the user types none: the source folder's name for a whole folder; null names the sequence + /// after its destination folder. + /// + public string? DefaultTitle { get; init; } + public IReadOnlyList Items { get; init; } = []; } @@ -99,6 +129,10 @@ public sealed record ImportPlan public required string SourceRoot { get; init; } public required string DestinationRoot { get; init; } public DateTimeOffset Created { get; init; } + + /// How the photos were grouped into . + public ImportGrouping Grouping { get; init; } + public IReadOnlyList Sessions { get; init; } = []; public IReadOnlyList Problems { get; init; } = []; diff --git a/src/Chronos.Core/Import/ImportPlanner.cs b/src/Chronos.Core/Import/ImportPlanner.cs index b60cba64..80775b14 100644 --- a/src/Chronos.Core/Import/ImportPlanner.cs +++ b/src/Chronos.Core/Import/ImportPlanner.cs @@ -2,6 +2,7 @@ using System.Globalization; using System.Security.Cryptography; using System.Text; using Chronos.Core.Archiving; +using Chronos.Core.Imaging; using Chronos.Core.Naming; namespace Chronos.Core.Import; @@ -33,16 +34,29 @@ public static class ImportPlanner { var folderTemplate = PathTemplate.Parse(options.FolderTemplate); var fileTemplate = PathTemplate.Parse(options.FileTemplate); + var wholeFolder = options.Grouping == ImportGrouping.WholeFolder; + var folderName = wholeFolder ? FolderName(sourceRoot) : null; var drafts = new List<(string Key, List Photos)>(); - foreach (var body in photos.GroupBy(BodyKey, StringComparer.Ordinal)) + // A whole folder groups by file format alone, never by camera body: RAW+JPEG of the same shots must stay two + // sequences, or each frame would appear twice in one. + Func groupKey = wholeFolder ? FormatKey : BodyKey; + foreach (var group in photos.GroupBy(groupKey, StringComparer.Ordinal)) { - var ordered = body.OrderBy(p => p.CaptureTime).ThenBy(p => p.File.RelativePath, StringComparer.Ordinal).ToList(); - var ranges = SessionSplitter.Split(ordered.ConvertAll(p => p.CaptureTime), options.SessionGap, options.MinimumSessionFrames, - i => options.SplitBefore.Contains(ordered[i].File.RelativePath) ? true : options.MergeAt.Contains(ordered[i].File.RelativePath) ? false : null); - foreach (var range in ranges) + var ordered = group.OrderBy(p => p.CaptureTime).ThenBy(p => p.File.RelativePath, StringComparer.Ordinal).ToList(); + var times = ordered.ConvertAll(p => p.CaptureTime); + bool? UserBreak(int i) => + options.SplitBefore.Contains(ordered[i].File.RelativePath) ? true : options.MergeAt.Contains(ordered[i].File.RelativePath) ? false : null; + // Only the user's splits break a whole folder: no pause starts a session and no group is too small to stand. + var ranges = wholeFolder + ? SessionSplitter.Split(times, TimeSpan.MaxValue, 1, UserBreak) + : SessionSplitter.Split(times, options.SessionGap, options.MinimumSessionFrames, UserBreak); + for (var r = 0; r < ranges.Count; r++) { - var members = ordered.GetRange(range.Start, range.Count); - drafts.Add((SessionKey(body.Key, members[0].CaptureTime), members)); + var members = ordered.GetRange(ranges[r].Start, ranges[r].Count); + var key = wholeFolder + ? FolderKey(sourceRoot, members[0].Metadata.Format, r == 0 ? null : members[0].CaptureTime) + : SessionKey(group.Key, members[0].CaptureTime); + drafts.Add((key, members)); } } drafts.Sort((a, b) => a.Photos[0].CaptureTime.CompareTo(b.Photos[0].CaptureTime) is var c and not 0 ? c : string.CompareOrdinal(a.Key, b.Key)); @@ -53,7 +67,7 @@ public static class ImportPlanner { var (key, members) = drafts[s]; var first = members[0]; - options.Titles.TryGetValue(key, out var title); + var title = options.Titles.GetValueOrDefault(key) ?? folderName; var context = Context(first, first.CaptureTime, s + 1, 1, title); var folder = await lookup.FindSessionFolderAsync(key, cancellationToken).ConfigureAwait(false) ?? UniqueFolder(folderTemplate.ExpandFolder(context), claimedFolders, lookup); @@ -74,11 +88,13 @@ public static class ImportPlanner CameraMake = first.Metadata.Make, CameraModel = first.Metadata.Model, CameraSerial = first.Metadata.BodySerial, + Cameras = CountCameras(members), Format = first.Metadata.Format, Start = first.CaptureTime, End = members[^1].CaptureTime, Folder = folder, Title = title, + DefaultTitle = folderName, Items = items, }); } @@ -88,6 +104,7 @@ public static class ImportPlanner SourceRoot = Path.GetFullPath(sourceRoot), DestinationRoot = Path.GetFullPath(destinationRoot), Created = DateTimeOffset.UtcNow, + Grouping = options.Grouping, Sessions = sessions, Problems = problems, AvailableBytes = FreeSpace(destinationRoot), @@ -139,12 +156,41 @@ public static class ImportPlanner }; /// Camera body and file format: RAW+JPEG shooting yields two sequences instead of doubled frames. - private static string BodyKey(CardPhoto p) => $"{p.Metadata.Make}|{p.Metadata.Model}|{p.Metadata.BodySerial}|{p.Metadata.Format}"; + private static string BodyKey(CardPhoto p) => $"{CameraKey(p)}|{p.Metadata.Format}"; + + private static string CameraKey(CardPhoto p) => $"{p.Metadata.Make}|{p.Metadata.Model}|{p.Metadata.BodySerial}"; + + /// Camera bodies that took the photos; a photo without a camera in its metadata is no body of its own. + private static int CountCameras(List photos) => + System.Math.Max(1, photos.Where(p => p.Metadata.Model is not null).Select(CameraKey).Distinct(StringComparer.Ordinal).Count()); + + /// File format alone (a whole folder): RAW+JPEG still yields two sequences instead of doubled frames. + private static string FormatKey(CardPhoto p) => p.Metadata.Format.ToString(); private static string SessionKey(string bodyKey, DateTime start) => "card-" + Convert.ToHexStringLower(SHA256.HashData(Encoding.UTF8.GetBytes(bodyKey)))[..8] + "-" + start.ToString("yyyyMMdd-HHmmss", CultureInfo.InvariantCulture); + /// + /// The key of a whole folder's sequence: the same folder of this PC and the same format always give the same key, + /// so an interrupted import resumes into its sequence and a later import of the folder adds to it. A part the user + /// split off also carries the time of its first photo. + /// + internal static string FolderKey(string sourceRoot, ImageFileFormat format, DateTime? splitAt = null) + { + var path = Path.TrimEndingDirectorySeparator(Path.GetFullPath(sourceRoot)); + // Windows compares paths without case: D:\Trip and d:\trip are one folder. + if (OperatingSystem.IsWindows()) path = path.ToUpperInvariant(); + // The same path on another PC is another folder: a server files every client's uploads by these keys. + var identity = $"{Environment.MachineName.ToUpperInvariant()}|{path}|{format}"; + var key = "folder-" + Convert.ToHexStringLower(SHA256.HashData(Encoding.UTF8.GetBytes(identity)))[..16]; + return splitAt is { } start ? key + "-" + start.ToString("yyyyMMdd-HHmmss", CultureInfo.InvariantCulture) : key; + } + + /// The picked folder's own name, the default title of a whole folder; null for a drive root. + private static string? FolderName(string sourceRoot) => + Path.GetFileName(Path.TrimEndingDirectorySeparator(Path.GetFullPath(sourceRoot))) is { Length: > 0 } name ? name : null; + private static string UniqueFolder(string folder, HashSet claimed, IImportLookup lookup) { if (folder.Length == 0) folder = "Import"; diff --git a/tests/Chronos.App.Tests/Import/ImportChoicesTests.cs b/tests/Chronos.App.Tests/Import/ImportChoicesTests.cs index 9ca5550d..d62f8b97 100644 --- a/tests/Chronos.App.Tests/Import/ImportChoicesTests.cs +++ b/tests/Chronos.App.Tests/Import/ImportChoicesTests.cs @@ -94,6 +94,16 @@ public sealed class ImportChoicesTests Assert.SequenceEqual([fresh.Id], joined.Without(own.Id).Cards); } + [Test] + public void TheGroupingTravelsToThePlannerButNeverIntoACardProfile() + { + var whole = new ImportChoices { Grouping = ImportGrouping.WholeFolder }; + Assert.Equal(ImportGrouping.WholeFolder, whole.ToOptions().Grouping); + Assert.Equal(ImportGrouping.Sessions, new ImportChoices().ToOptions().Grouping, "a source starts with its sessions"); + Assert.True(whole.NeedsNewPlan(new ImportChoices()), "the grouping changes which photos form which sequence"); + Assert.Equal(ImportGrouping.Sessions, ImportChoices.FromProfile(whole.ToProfile()).Grouping, "a card always imports by sessions"); + } + [Test] public void SplitsAndMergesTravelToThePlanner() { diff --git a/tests/Chronos.App.Tests/Import/ImportPlansTests.cs b/tests/Chronos.App.Tests/Import/ImportPlansTests.cs index ced65eb7..5888edc3 100644 --- a/tests/Chronos.App.Tests/Import/ImportPlansTests.cs +++ b/tests/Chronos.App.Tests/Import/ImportPlansTests.cs @@ -1,5 +1,6 @@ using System.Globalization; using Chronos.App.Import; +using Chronos.Core.Imaging; using Chronos.Core.Import; using Chronos.Core.Localization; using Chronos.Testing; @@ -175,6 +176,51 @@ public sealed class ImportPlansTests Assert.Equal(1, split[2].MergesInto, "the earlier session of the same body, skipping the other camera"); } + [Test] + public void AWholeFolderRowShowsItsFormatAndCamerasWithTheFolderNameAsDefaultTitle() + { + var jpeg = Session(1, "2026/2026-09-28 Canon EOS R5", Item(1, Start), Item(2, Start.AddHours(5))) with + { + SessionKey = "folder-jpeg", + Format = ImageFileFormat.Jpeg, + Cameras = 2, + Title = "Etna", + DefaultTitle = "Etna", + }; + var raw = Session(2, "2026/2026-09-28 Canon EOS R5 (2)", Item(3, Start), Item(4, Start.AddHours(5))) with + { + SessionKey = "folder-raw", + Title = "Etna", + DefaultTitle = "Etna", + }; + var plan = Plan(null, null, jpeg, raw) with { Grouping = ImportGrouping.WholeFolder }; + + var rows = ImportPlans.Sessions(plan, ImportDestinationKind.Archive, new HashSet()); + Assert.Equal("2 cameras · JPEG", rows[0].CameraText); + Assert.Equal("Canon EOS R5 · CR2", rows[1].CameraText); + Assert.Null(rows[0].Title, "the folder's name is the placeholder, not typed text"); + Assert.Equal("Etna", rows[0].DefaultTitle); + Assert.Null(rows[1].MergesInto, "another format never merges"); + var bySessions = ImportPlans.Sessions(plan with { Grouping = ImportGrouping.Sessions }, ImportDestinationKind.Archive, new HashSet()); + Assert.Equal("Canon EOS R5", bySessions[1].CameraText); + + var titled = ImportPlans.WithTitles(plan, new Dictionary { ["folder-jpeg"] = "Etna at night" }); + Assert.Equal("Etna at night", titled.Sessions[0].Title); + Assert.Equal("Etna", titled.Sessions[1].Title, "a sequence without a typed title keeps the folder's name"); + Assert.Equal("Etna at night", ImportPlans.Sessions(titled, ImportDestinationKind.Archive, new HashSet())[0].Title); + } + + [Test] + public void AWholeFolderPartMergesBackIntoThePartBeforeItOfTheSameFormatWhateverItsCamera() + { + var first = Session(1, "2026/Etna", Item(1, Start)); + var second = Session(2, "2026/Etna (2)", Item(2, Start.AddHours(1))) with { CameraMake = "Nikon", CameraModel = "NIKON Z 8" }; + var plan = Plan(null, null, first, second) with { Grouping = ImportGrouping.WholeFolder }; + Assert.Equal(1, ImportPlans.Sessions(plan, ImportDestinationKind.Archive, new HashSet())[1].MergesInto); + Assert.Null(ImportPlans.Sessions(plan with { Grouping = ImportGrouping.Sessions }, ImportDestinationKind.Archive, new HashSet())[1].MergesInto, + "sessions of another camera never merge"); + } + [Test] public void ShowPlanOffersASplitOnEveryPhotoButTheFirstOfItsSession() { diff --git a/tests/Chronos.Core.Tests/Import/ImportPlannerTests.cs b/tests/Chronos.Core.Tests/Import/ImportPlannerTests.cs index c624dc32..162af08e 100644 --- a/tests/Chronos.Core.Tests/Import/ImportPlannerTests.cs +++ b/tests/Chronos.Core.Tests/Import/ImportPlannerTests.cs @@ -1,6 +1,8 @@ using Chronos.Core.Archiving; +using Chronos.Core.Imaging; using Chronos.Core.Import; using Chronos.Core.Localization; +using Chronos.Core.Metadata; using Chronos.Core.Tests.Archiving; using Chronos.Testing; using static Chronos.Core.Tests.Import.ImportTestKit; @@ -163,6 +165,118 @@ public sealed class ImportPlannerTests(TestContext context) Assert.SequenceEqual([5, 5, 10], split.Sessions.Select(s => s.Items.Count)); } + [Test] + public async Task AWholeFolderIsOneSequenceWhateverTheCameraBodiesAndPauses() + { + var folder = Path.Combine(context.CreateTempDirectory("source"), "Etna eruption"); + TestPhotos.WriteSeries(folder, Start, 10, Interval, prefix: "CAN_"); + TestPhotos.WriteSeries(folder, Start.AddHours(3), 10, Interval, firstSeed: 20, prefix: "CAN_", firstNumber: 11); + TestPhotos.WriteSeries(folder, Start.AddSeconds(5), 3, Interval, firstSeed: 40, prefix: "NIK_", make: "NIKON CORPORATION", model: "NIKON Z 8"); + var destination = context.CreateTempDirectory("archive"); + Assert.Equal(3, (await PlanAsync(folder, destination)).Sessions.Count, "by sessions: two Canon sessions and the Nikon"); + + var plan = await PlanAsync(folder, destination, new ImportOptions { Grouping = ImportGrouping.WholeFolder, FolderTemplate = "{year}/{title}" }); + Assert.Equal(ImportGrouping.WholeFolder, plan.Grouping); + var session = plan.Sessions.Single(); + Assert.Equal(23, session.Items.Count); + Assert.Equal(2, session.Cameras); + Assert.Equal("Etna eruption", session.Title, "the folder's name is the default title"); + Assert.Equal("Etna eruption", session.DefaultTitle); + Assert.Equal("2026/Etna eruption", session.Folder); + Assert.SequenceEqual(session.Items.OrderBy(i => i.CapturedAt).Select(i => i.SourcePath), session.Items.Select(i => i.SourcePath), + "both bodies in capture order"); + Assert.Equal("2026-09-28_21-01-30_0023.JPG", session.Items[^1].FileName); + } + + [Test] + public async Task AWholeFolderOfRawAndJpegIsTwoSequencesSoNoFrameIsDoubled() + { + var photos = new List(); + for (var i = 0; i < 6; i++) + { + // An hour between shots: far beyond the session gap, ignored for a whole folder. + var at = Start.AddHours(i); + photos.Add(Photo($"IMG_{i:0000}.JPG", at, ImageFileFormat.Jpeg)); + photos.Add(Photo($"IMG_{i:0000}.CR3", at, ImageFileFormat.Cr3)); + } + var plan = await ImportPlanner.PlanAsync(context.CreateTempDirectory("raw+jpeg"), context.CreateTempDirectory("archive"), photos, [], + new ImportOptions { Grouping = ImportGrouping.WholeFolder }, new FakeLookup()); + + Assert.Equal(2, plan.Sessions.Count, "one sequence per format"); + Assert.SequenceEqual([ImageFileFormat.Jpeg, ImageFileFormat.Cr3], plan.Sessions.Select(s => s.Format).OrderBy(f => f)); + foreach (var session in plan.Sessions) + { + Assert.Equal(6, session.Items.Count); + Assert.Equal(1, session.Items.Select(i => Path.GetExtension(i.SourcePath)).Distinct().Count(), "one format per sequence"); + } + Assert.Equal(12, plan.Items.Select(i => i.SourcePath).Distinct().Count(), "every photo is planned once"); + Assert.NotEqual(plan.Sessions[0].SessionKey, plan.Sessions[1].SessionKey); + Assert.NotEqual(plan.Sessions[0].Folder, plan.Sessions[1].Folder); + } + + [Test] + public async Task AWholeFolderTakesItsSubfoldersOnlyWhenAsked() + { + var folder = Path.Combine(context.CreateTempDirectory("source"), "Construction"); + TestPhotos.WriteSeries(folder, Start, 4, Interval); + TestPhotos.WriteSeries(Path.Combine(folder, "week 2"), Start.AddDays(7), 4, Interval, firstSeed: 50, firstNumber: 5); + TestPhotos.WriteSeries(Path.Combine(folder, "week 2", "monday"), Start.AddDays(8), 4, Interval, firstSeed: 70, firstNumber: 9); + var destination = context.CreateTempDirectory("archive"); + var whole = new ImportOptions { Grouping = ImportGrouping.WholeFolder }; + + var all = (await PlanAsync(folder, destination, whole)).Sessions.Single(); + Assert.Equal(12, all.Items.Count); + Assert.Equal("week 2/monday/IMG_0012.JPG", all.Items[^1].SourcePath); + var top = (await PlanAsync(folder, destination, whole with { IncludeSubfolders = false })).Sessions.Single(); + Assert.Equal(4, top.Items.Count); + Assert.Equal(all.SessionKey, top.SessionKey, "the same folder's sequence, whatever is scanned in it"); + } + + [Test] + public async Task AWholeFolderKeepsItsKeySoAnInterruptedImportResumesIntoItsSequence() + { + var folder = Path.Combine(context.CreateTempDirectory("source"), "Etna"); + TestPhotos.WriteSeries(folder, Start, 10, Interval); + var destination = context.CreateTempDirectory("archive"); + var whole = new ImportOptions { Grouping = ImportGrouping.WholeFolder }; + var first = (await PlanAsync(folder, destination, whole)).Sessions.Single(); + Assert.True(first.SessionKey.StartsWith("folder-", StringComparison.Ordinal), first.SessionKey); + Assert.Equal(first.SessionKey, ImportPlanner.FolderKey(folder + Path.DirectorySeparatorChar, ImageFileFormat.Jpeg), + "a trailing separator names the same folder"); + Assert.NotEqual(first.SessionKey, ImportPlanner.FolderKey(folder, ImageFileFormat.Cr3)); + Assert.NotEqual(first.SessionKey, ImportPlanner.FolderKey(Path.Combine(folder, "other"), ImageFileFormat.Jpeg)); + + // Photos added later, earlier ones too, keep the folder's key. + TestPhotos.WriteSeries(folder, Start.AddDays(-1), 2, Interval, firstSeed: 90, prefix: "OLD_"); + Assert.Equal(first.SessionKey, (await PlanAsync(folder, destination, whole)).Sessions.Single().SessionKey); + + var lookup = new FakeLookup(); + lookup.SessionFolders[first.SessionKey] = "2026/Etna"; + Assert.Equal("2026/Etna", (await PlanAsync(folder, destination, whole, lookup)).Sessions.Single().Folder, "the sequence of the interrupted import"); + + var typed = whole with { Titles = new Dictionary { [first.SessionKey] = "Etna at night" } }; + var titled = (await PlanAsync(folder, destination, typed)).Sessions.Single(); + Assert.Equal("Etna at night", titled.Title); + Assert.Equal("Etna", titled.DefaultTitle); + } + + [Test] + public async Task AWholeFolderSplitsOnlyWhereTheUserSplitsIt() + { + var folder = Path.Combine(context.CreateTempDirectory("source"), "Harbour"); + TestPhotos.WriteSeries(folder, Start, 10, Interval); + var destination = context.CreateTempDirectory("archive"); + var split = new ImportOptions { Grouping = ImportGrouping.WholeFolder, SplitBefore = new HashSet { "IMG_0009.JPG" } }; + + var plan = await PlanAsync(folder, destination, split); + Assert.SequenceEqual([8, 2], plan.Sessions.Select(s => s.Items.Count), "a part the user split off stays, however short"); + Assert.Equal(ImportPlanner.FolderKey(folder, ImageFileFormat.Jpeg), plan.Sessions[0].SessionKey); + Assert.NotEqual(plan.Sessions[0].SessionKey, plan.Sessions[1].SessionKey); + Assert.Equal(plan.Sessions[1].SessionKey, (await PlanAsync(folder, destination, split)).Sessions[1].SessionKey); + var merged = split with { SplitBefore = new HashSet(), MergeAt = new HashSet { "IMG_0009.JPG" } }; + Assert.Equal(10, (await PlanAsync(folder, destination, merged)).Sessions.Single().Items.Count, "merging the part back undoes the split"); + } + [Test] public async Task APhotoWithoutExifIsPlannedAtItsFileTime() { @@ -177,4 +291,8 @@ public sealed class ImportPlannerTests(TestContext context) // No camera identity: it forms its own body group rather than joining the Canon session. Assert.SequenceEqual([8, 1], plan.Sessions.Select(s => s.Items.Count)); } + + /// A photo as the scanner reads it, without a file behind it (the planner hashes only duplicate candidates). + private static CardPhoto Photo(string path, DateTime captured, ImageFileFormat format) => new(new CardFile(path, 1_000, captured), + new FrameMetadata { Format = format, Make = "Canon", Model = "Canon EOS R5", BodySerial = "0431", CapturedAt = captured }); } diff --git a/tests/Chronos.Data.Tests/Archiving/ImportServiceTests.cs b/tests/Chronos.Data.Tests/Archiving/ImportServiceTests.cs index aacbb963..e35629b0 100644 --- a/tests/Chronos.Data.Tests/Archiving/ImportServiceTests.cs +++ b/tests/Chronos.Data.Tests/Archiving/ImportServiceTests.cs @@ -105,6 +105,35 @@ public sealed class ImportServiceTests(TestContext context) Assert.Equal(20, (await FramesAsync(fx, plan.Sessions.Single())).Count); } + [Test] + public async Task AWholeFolderBecomesOneSequenceNamedAfterItThatALaterImportAddsTo() + { + await using var fx = await CreateAsync(context); + var folder = Path.Combine(context.CreateTempDirectory("source"), "Etna eruption"); + Photos.WriteSeries(folder, Start, 5); + Photos.WriteSeries(folder, Start.AddHours(4), 5, firstSeed: 20, prefix: "NIK_", make: "NIKON CORPORATION", model: "NIKON Z 8"); + var whole = new ImportOptions { Grouping = ImportGrouping.WholeFolder }; + + var plan = await fx.Service.PlanImportAsync(fx.Archive.Id, folder, whole, null, null, fx.Ct); + var session = plan.Sessions.Single(); + await RunAsync(fx, plan); + var id = await fx.Db.Sequences.FindBySessionKeyAsync(fx.Archive.Id, session.SessionKey); + Assert.Equal("Etna eruption", (await fx.Db.Sequences.GetAsync(id!.Value))!.Title); + Assert.Equal(10, (await FramesAsync(fx, session)).Count); + + // Photos added to the folder later join the same sequence; those imported before are found in the archive. + Photos.WriteSeries(folder, Start.AddDays(1), 3, firstSeed: 40, prefix: "LATE_"); + var later = await fx.Service.PlanImportAsync(fx.Archive.Id, folder, whole, null, null, fx.Ct); + Assert.Equal(session.SessionKey, later.Sessions.Single().SessionKey); + Assert.Equal(session.Folder, later.Sessions.Single().Folder); + Assert.Equal(3, later.CopyCount); + await RunAsync(fx, later); + Assert.Equal(13, (await FramesAsync(fx, session)).Count); + + static Task RunAsync(ArchiveFixture fx, ImportPlan plan) => fx.Service.Imports.ExecuteAsync( + new ImportJobParameters(fx.Archive.Id, plan, null, true, null), Guid.NewGuid(), 0, _ => ValueTask.CompletedTask, null, fx.Ct); + } + [Test] public async Task ErasingTheCardDeletesOnlyVerifiedImports() {