Files
Febe/src/Febe.Core/Export/ExportText.cs
T
Alby96andClaude Opus 5.5 2c0cfcccf2 E4: copie del fotogramma con il loro nome, eventi che non si perdono, visualizzatore che si chiude
«Copia di nuovo» di un _fotogramma lo rifaceva come _t…, e un lavoro in attesa prendeva un istante del file vecchio.
Un'analisi copriva l'evento di una copia finita; il visualizzatore restava sopra la pagina nuova e poteva mostrare un
esito superato; la scheda Esportazione perdeva il fuoco a ogni rilettura.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-01 18:57:58 +01:00

236 lines
12 KiB
C#

using Febe.Core.Codecs.Containers;
using Febe.Core.Codecs.Images;
using Febe.Core.Domain;
using Febe.Core.Engine;
using Febe.Core.Library.Conversion;
using Febe.Core.Storage;
namespace Febe.Core.Export;
/// <summary>
/// Texts of the copies to external folders for Alberto (spec §4.10, §10.4): sentence case, dots for thousands, the same
/// names everywhere. Notes are stored in <c>export_item.note</c> as English codes (<see cref="Notes"/>) and shown with
/// <see cref="Note"/>.
/// </summary>
public static class ExportText
{
/// <summary>The codes of <c>export_item.note</c>.</summary>
public static class Notes
{
public const string SourceMissing = "SourceMissing";
public const string AccountRemoved = "AccountRemoved";
public const string Removed = "Removed";
public const string Superseded = "Superseded";
public const string CopiedForAnotherAccount = "CopiedForAnotherAccount";
public const string SourceChanged = "SourceChanged";
public const string TooLargeForFileSystem = "TooLargeForFileSystem";
public const string NameTaken = "NameTaken";
public const string PathTaken = "PathTaken";
public const string NotExported = "NotExported";
// Tappa E3 (D19): an item's note is a list of codes separated by spaces, «Code» or «Code:Arg».
public const string AsIsHeif = ImageConversionLosses.AsIsHeif;
public const string AsIsAvif = ImageConversionLosses.AsIsAvif;
public const string AsIsRaw = ImageConversionLosses.AsIsRaw;
public const string AsIsAnimated = ImageConversionLosses.AsIsAnimated;
public const string AsIsNotDecodable = ImageConversionLosses.AsIsNotDecodable;
public const string AsIsDeepColor = ImageConversionLosses.AsIsDeepColor;
public const string AsIsCmyk = ImageConversionLosses.AsIsCmyk;
public const string AsIsTooLarge = ImageConversionLosses.AsIsTooLarge;
public const string AsIsNotRemuxable = MediaConversionResult.AsIsNotRemuxable;
public const string FrameNotAvailable = "FrameNotAvailable";
// Tappa E4: «Salva il fotogramma» of a file that changed meanwhile, and is not a still video as far as Febe knows.
public const string NotStill = "NotStill";
public const string TransparencyWhite = ImageConversionLosses.TransparencyWhite;
public const string CopiedInAnotherFormat = "CopiedInAnotherFormat";
public const string LostTracks = Mp4Remuxer.LostTracks;
public const string LostVideoMetadata = Mp4Remuxer.LostVideoMetadata;
}
/// <summary>Name of the state of a file of a job.</summary>
public static string State(ExportItemState state) => state switch
{
ExportItemState.Pending => "In attesa",
ExportItemState.Copied => "Copiato",
ExportItemState.Adopted => "Adottato",
ExportItemState.AlreadyCopied => "Già copiato",
ExportItemState.Skipped => "Saltato",
ExportItemState.Missing => "File non presente in libreria",
ExportItemState.Failed => "Non riuscito",
_ => state.ToString(),
};
/// <summary>Name of the state of a copy.</summary>
public static string CopyState(ExportCopyState state) => state switch
{
ExportCopyState.Copied => "Copiato",
ExportCopyState.Removed => "Tolto",
ExportCopyState.Superseded => "Superato",
ExportCopyState.Failed => "Non riuscito",
_ => state.ToString(),
};
/// <summary>
/// The Italian text of a note: its codes (separated by spaces, D19 of tappa E3) each in Italian, joined with « · »;
/// null for none; an unknown code is shown as it is.
/// </summary>
public static string? Note(string? note)
{
if (string.IsNullOrWhiteSpace(note))
{
return null;
}
var codes = note.Split(' ', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
return string.Join(" · ", codes.Select(One));
}
/// <summary>The codes as one note (D19 of tappa E3): the non-empty ones, without repeats, separated by spaces; null for none.</summary>
public static string? JoinNotes(IEnumerable<string?> codes)
{
ArgumentNullException.ThrowIfNull(codes);
var list = codes.Where(c => !string.IsNullOrWhiteSpace(c)).Select(c => c!.Trim()).Distinct(StringComparer.Ordinal).ToList();
return list.Count == 0 ? null : string.Join(' ', list);
}
/// <summary>The reason of «non si porta in MP4», from the code of <see cref="RemuxRefusal"/>.</summary>
private static string RemuxReason(string reason) => reason switch
{
nameof(RemuxRefusal.NotAVideo) => "Febe non lo riconosce come video",
nameof(RemuxRefusal.ContainerNotRemuxable) => "contenitore",
nameof(RemuxRefusal.VideoCodec) => "codec video",
nameof(RemuxRefusal.AudioCodec) => "codec audio",
nameof(RemuxRefusal.MissingConfiguration) => "configurazione del codec mancante",
nameof(RemuxRefusal.EncodedTrack) => "tracce codificate",
nameof(RemuxRefusal.EditList) => "elenco di modifiche",
nameof(RemuxRefusal.NoSamples) => "nessun fotogramma",
nameof(RemuxRefusal.Unreadable) => "file rovinato",
_ => reason,
};
private static string One(string code)
{
var colon = code.IndexOf(':', StringComparison.Ordinal);
var name = colon < 0 ? code : code[..colon];
var arg = colon < 0 ? null : code[(colon + 1)..];
const string AsIsVideo = "Video copiato com'è: non si porta in MP4 senza ricodificarlo";
return name switch
{
Notes.AsIsNotRemuxable => arg is { Length: > 0 } ? $"{AsIsVideo} ({RemuxReason(arg)})" : AsIsVideo,
Notes.LostTracks => arg is { Length: > 0 } ? $"Tracce tolte: {arg}" : "Tracce tolte",
_ when arg is null => Note1(code) ?? code,
_ => code,
};
}
private static string? Note1(string code) => code switch
{
Notes.SourceMissing => "File non presente in libreria",
Notes.AccountRemoved => "L'account è stato rimosso",
Notes.Removed => "Tolto dalla destinazione: non si ricopia da solo («Copia di nuovo»)",
Notes.Superseded => "Superato da un file nuovo della libreria: non si ricopia da solo",
Notes.CopiedForAnotherAccount => "Già copiato per un altro account («Una copia sola»)",
Notes.SourceChanged => "Il file della libreria è cambiato durante la copia",
Notes.TooLargeForFileSystem => "Più grande di 4 GB: il disco FAT32 non lo accetta",
Notes.NameTaken => "Nome già occupato da un altro file: copiato con un nome libero",
Notes.PathTaken => "Percorso preso nel frattempo: riprova",
Notes.NotExported => "Non si copia qui: «Esporta questo account» è spento, o il file va in un'altra cartella",
Notes.AsIsHeif => "HEIC copiato com'è",
Notes.AsIsAvif => "AVIF copiato com'è",
Notes.AsIsRaw => "Raw copiato com'è",
Notes.AsIsAnimated => "Immagine animata copiata com'è: convertita perderebbe i fotogrammi dopo il primo",
Notes.AsIsNotDecodable => "Copiato com'è: Febe non riesce a leggere l'immagine",
Notes.AsIsDeepColor => "Copiato com'è: più di 8 bit per colore",
Notes.AsIsCmyk => "Copiato com'è: immagine CMYK",
Notes.AsIsTooLarge => "Copiato com'è: troppo grande per il formato scelto",
Notes.FrameNotAvailable => "Fotogramma non disponibile per questo video",
Notes.NotStill => "Il video è cambiato: il file di adesso non è un video fermo, o non è stato analizzato",
Notes.TransparencyWhite => "La trasparenza è diventata bianco",
Notes.CopiedInAnotherFormat => "Già copiato in un altro formato",
ImageConversionLosses.LostTexts => "Non tenuti: testi",
ImageConversionLosses.LostColorChunks => "Non tenuti: dati del colore",
ImageConversionLosses.LostIcc => "Non tenuto: profilo ICC",
ImageConversionLosses.LostIccTooLarge => "Non tenuto: profilo ICC troppo grande",
ImageConversionLosses.LostExifTooLarge => "Non tenuto: EXIF troppo grande per JPG",
ImageConversionLosses.LostXmpTooLarge => "Non tenuto: XMP troppo grande",
ImageConversionLosses.LostExifUnreadable => "Non tenuto: EXIF rovinato",
ImageConversionLosses.LostIptc => "Non tenuti: IPTC",
ImageConversionLosses.LostOtherSegments => "Non tenuti: altri blocchi",
ImageConversionLosses.LostCommentTooLarge => "Non tenuto: commento troppo lungo",
ImageConversionLosses.LostExtendedXmp => "Non tenuto: XMP esteso",
Notes.LostVideoMetadata => "Non tenuti: metadati del video",
_ => null,
};
/// <summary>«1 file copiato», «1.204 file copiati».</summary>
public static string Count(int count, string one, string many) => count == 1 ? "1 " + one : ItalianText.Number(count) + " " + many;
/// <summary>The event written when a job ends: «Copia in Disco USB finita: 120 file copiati, 30 già copiati.»</summary>
public static string CompletionMessage(ExportRunSummary summary, ExportDestination destination)
{
ArgumentNullException.ThrowIfNull(summary);
ArgumentNullException.ThrowIfNull(destination);
int Of(ExportItemState s) => summary.ByState.GetValueOrDefault(s);
var parts = new List<string> { Count(Of(ExportItemState.Copied) + Of(ExportItemState.Adopted), "file copiato", "file copiati") };
if (Of(ExportItemState.AlreadyCopied) > 0)
{
parts.Add(Count(Of(ExportItemState.AlreadyCopied), "già copiato", "già copiati"));
}
if (Of(ExportItemState.Skipped) > 0)
{
parts.Add(Count(Of(ExportItemState.Skipped), "saltato", "saltati"));
}
if (Of(ExportItemState.Missing) > 0)
{
parts.Add(Count(Of(ExportItemState.Missing), "non presente in libreria", "non presenti in libreria"));
}
if (Of(ExportItemState.Failed) > 0)
{
parts.Add(Count(Of(ExportItemState.Failed), "non riuscito", "non riusciti"));
}
return $"Copia in {ExportDestinations.Label(destination)} finita: {string.Join(", ", parts)}.";
}
/// <summary>The event of «Controlla le copie».</summary>
public static string CheckMessage(int checkedCopies, int removed, ExportDestination destination)
{
ArgumentNullException.ThrowIfNull(destination);
var text = $"Controllate {ItalianText.Number(checkedCopies)} copie in {ExportDestinations.Label(destination)}: ";
return text + (removed == 0 ? "ci sono tutte." : $"{Count(removed, "non c'è più (tolta)", "non ci sono più (tolte)")}.");
}
/// <summary>The space is not enough for the next file (decision D16).</summary>
public static string NoSpace(ExportDestination destination, long free, string file, long bytes) =>
$"Su {ExportDestinations.Label(destination)} restano {ItalianText.Bytes(free)}: per copiare {file} ({ItalianText.Bytes(bytes)}) serve più spazio, "
+ $"con un margine di {ItalianText.Bytes(ExportSpace.Margin)}. Libera spazio, poi riprendi.";
/// <summary>The disk said it is full.</summary>
public static string DiskFull(ExportDestination destination) =>
$"Su {ExportDestinations.Label(destination)} non c'è più spazio: libera spazio, poi riprendi.";
/// <summary>A file over the FAT32 limit.</summary>
public static string TooLarge(long bytes) =>
$"Il file pesa {ItalianText.Bytes(bytes)}: su un disco FAT32 un file non può superare 4 GB.";
/// <summary>The destination was retired while a job used it.</summary>
public const string RetiredDestination = "La cartella è stata tolta.";
/// <summary>An error for Alberto, without paths of secrets: the message of a file error, or a generic sentence.</summary>
public static string ErrorText(Exception ex)
{
ArgumentNullException.ThrowIfNull(ex);
return ex switch
{
ExportFolderException folder => folder.Message,
IOException io => $"Errore di lettura o scrittura: {io.Message}",
UnauthorizedAccessException => "Febe non ha il permesso di scrivere nella cartella: controlla i permessi.",
_ => $"Errore inatteso ({ex.GetType().Name}): riprova; se si ripete, segnalalo.",
};
}
}