Il visualizzatore dice «File danneggiato o incompleto» (risposta 3 del 25 novembre)
I codec di casa rifiutavano già un PNG o un JPEG troncato: niente miniatura (la tessera neutra nel mosaico), niente impronta visiva, file tenuto con il suo SHA-256. Ma il visualizzatore mostrava il file a metà, come lo disegna il browser. Ora la foto di un elemento ha «damaged» quando è sul disco, non ha miniatura e i codec la riconoscono ma non riescono a decodificarla (DamagedImage): al posto dell'immagine il visualizzatore scrive «File danneggiato o incompleto», e le azioni sul file restano. Il controllo si fa all'apertura, solo per le foto senza miniatura, senza colonne nuove nel database: vale anche per le librerie già scaricate e non pesa con 200.000 elementi. HEIC, formati sconosciuti e immagini oltre i limiti non contano come danneggiati. Il file si legge soltanto. Prove: rilevamento su PNG e JPEG troncati, interi, HEIC e sconosciuti; scaricamento di foto troncate (stato, SHA-256, niente impronta né miniatura, file intatto); API della libreria (tessera neutra, «damaged», file ancora scaricabile). Guida (Problemi comuni) e una riga in decisioni.md. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
a0e7404297
commit
5dfbe85614
10 files changed
+227
-3
No files matched your search
@@ -0,0 +1,42 @@
|
||||
namespace Febe.Core.Library;
|
||||
|
||||
/// <summary>
|
||||
/// «File danneggiato o incompleto» (spec §4.6, §8.4; Alberto's answer of 25 November 2026): a photo truncated or
|
||||
/// damaged, downloaded or imported, is refused and said so. It has no thumbnail (the neutral tile) and no perceptual
|
||||
/// hash, so it is never a probable duplicate; its SHA-256 still counts. The file stays in the library (rule 12): this
|
||||
/// class only looks, it never deletes, replaces or downloads again.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// The check decodes the picture, so it is meant for a single file at a time (the viewer) and only for photos that
|
||||
/// have no thumbnail: a photo with a thumbnail was decoded when it was stored.
|
||||
/// </remarks>
|
||||
public static class DamagedImage
|
||||
{
|
||||
/// <summary>
|
||||
/// True when the bytes are a photo in a format Febe reads that cannot be decoded because it is damaged or
|
||||
/// truncated; false for a photo that decodes, for a format Febe does not read (HEIC, an unknown one) and for a
|
||||
/// picture too large to decode.
|
||||
/// </summary>
|
||||
public static bool IsDamaged(ReadOnlyMemory<byte> data) => ImageDecoding.IsDamaged(data);
|
||||
|
||||
/// <summary>
|
||||
/// <see cref="IsDamaged(ReadOnlyMemory{byte})"/> for a file on disk, read once; false when it cannot be read or is
|
||||
/// larger than <see cref="ImageDecoding.MaxFileBytes"/>.
|
||||
/// </summary>
|
||||
public static bool IsFileDamaged(string path)
|
||||
{
|
||||
ArgumentException.ThrowIfNullOrEmpty(path);
|
||||
byte[]? data;
|
||||
try
|
||||
{
|
||||
using var stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite | FileShare.Delete);
|
||||
data = ImageDecoding.ReadAll(stream);
|
||||
}
|
||||
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return data is not null && ImageDecoding.IsDamaged(data);
|
||||
}
|
||||
}
|
||||
@@ -75,6 +75,42 @@ internal static class ImageDecoding
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// True when <paramref name="data"/> is in a format Febe decodes but cannot be decoded because it is damaged or
|
||||
/// truncated (spec §8.4, «File danneggiato o incompleto»). False for a picture that decodes, for what Febe does not
|
||||
/// read (unknown formats, HEIC, the JPEG processes it leaves out) and for a picture over the limits: those are not
|
||||
/// damaged, Febe just has no picture of them.
|
||||
/// </summary>
|
||||
internal static bool IsDamaged(ReadOnlyMemory<byte> data)
|
||||
{
|
||||
if (data.IsEmpty || !ImageDecoder.CanDecode(ImageFormatSniffer.Detect(data.Span)))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
// A reduced JPEG decode still reads every scan to its end, so it finds a cut as the full decode does.
|
||||
using var image = ImageDecoder.DecodeAtLeast(data, DamageCheckSide, out _, Limits, pooled: true);
|
||||
return false;
|
||||
}
|
||||
catch (ImageLimitException)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
catch (ImageFormatNotSupportedException)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
catch (ImageDecodeException)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>The smallest long side <see cref="IsDamaged"/> lets a JPEG be decoded at: the cheapest reduction.</summary>
|
||||
private const int DamageCheckSide = 1;
|
||||
|
||||
/// <summary>
|
||||
/// Bytes read first by <see cref="ReadSize(Stream)"/> for JPEG, GIF and BMP, whose headers (a JPEG EXIF block is
|
||||
/// at most 64 KiB) fit; when they do not (large ICC profiles) the rest of the file is read as well. PNG and WebP
|
||||
|
||||
Reference in new issue
Block a user