Correzioni della revisione della catena 0.3.0, ognuna con la sua prova
- Collegamento: corretta la SyntaxError di connection.js (una «let saved» dichiarata due
volte nello stesso blocco fermava tutta la pagina). Nuova prova ScriptSyntaxTests con un
controllo scritto in casa che legge ogni modulo di wwwroot: parentesi, dichiarazioni
doppie nello stesso blocco, importazioni che il modulo di arrivo non esporta.
- HEIF: il riconoscimento dei formati prende ogni marchio che HeifDecoder accetta (mif2,
MiHE, marchi solo compatibili), così rilevazione, decodifica, misure, miniatura e
impronta concordano.
- Docker, lasciare la pagina del browser: un nuovo contratto del browser cancella l'uscita
annotata (_leftAt) anche prima che la vista si riapra, e la copia della sessione aspetta
un altro comando del collegamento al più LetGoWait invece che senza limite.
- JPEG e PNG: senza EOI o IEND la decodifica fallisce («File danneggiato o incompleto»),
la sola lettura delle misure resta tollerante.
- Visualizzatore: IsDamaged legge prima l'intestazione e il file intero solo quando può
servire, in modo asincrono e con una piccola memoria per file, così una scheda con
molti file non blocca il server.
- Importazione (§9.3): le immagini danneggiate si importano come le altre ma si segnano
nella revisione, nel resoconto e nel CSV.
- Pulizia: le prove con il limite di 5 minuti non corrono più in parallelo fra loro.
- Pila dei caratteri come l'appendice B, senza Liberation; testo di Informazioni sulle
immagini fatte in casa; guida (Problemi comuni, Sicurezza) allineata all'uscita in
Docker dalla pagina del browser.
- docs/decisioni.md: le righe del 25 novembre 2026 con i motivi, compresa l'unione 537517b
tenuta così com'è.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
3d234f8791
commit
2068dd28f3
35 files changed
+1366
-75
No files matched your search
@@ -1,3 +1,5 @@
|
||||
using System.Collections.Concurrent;
|
||||
|
||||
namespace Febe.Core.Library;
|
||||
|
||||
/// <summary>
|
||||
@@ -7,11 +9,18 @@ namespace Febe.Core.Library;
|
||||
/// 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.
|
||||
/// The check decodes the picture, so it is meant for a single file at a time (the viewer, the import of one file) and
|
||||
/// only for photos that have no thumbnail: a photo with a thumbnail was decoded when it was stored. The first bytes are
|
||||
/// looked at before the rest is read: a format Febe does not decode, a HEIC and a picture over the decoding limit are
|
||||
/// never damaged, so their file is not read whole for nothing.
|
||||
/// </remarks>
|
||||
public static class DamagedImage
|
||||
{
|
||||
/// <summary>How many answers <see cref="IsFileDamagedAsync"/> keeps; the whole memory is dropped when it is full.</summary>
|
||||
private const int RememberedAnswers = 512;
|
||||
|
||||
private static readonly ConcurrentDictionary<(string Path, long Length, DateTime Written), bool> Answers = new();
|
||||
|
||||
/// <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 (AVIF, an unknown one), for HEIC (see ImageDecoding.IsDamaged) and for a
|
||||
@@ -20,23 +29,120 @@ public static class DamagedImage
|
||||
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"/>.
|
||||
/// <see cref="IsDamaged(ReadOnlyMemory{byte})"/> for a file on disk; false when it cannot be read or is larger than
|
||||
/// <see cref="ImageDecoding.MaxFileBytes"/>. Reads the first bytes and, only when the picture may be damaged, the rest.
|
||||
/// </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);
|
||||
var length = stream.Length;
|
||||
if (length <= 0 || length > ImageDecoding.MaxFileBytes)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
var head = (int)Math.Min(length, ImageDecoding.HeaderBytes);
|
||||
var data = new byte[head];
|
||||
stream.ReadExactly(data);
|
||||
if (!ImageDecoding.MayBeDamaged(data))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (length > head)
|
||||
{
|
||||
var whole = new byte[length];
|
||||
data.CopyTo(whole, 0);
|
||||
stream.ReadExactly(whole.AsSpan(head));
|
||||
data = whole;
|
||||
}
|
||||
|
||||
return ImageDecoding.IsDamaged(data);
|
||||
}
|
||||
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// <see cref="IsFileDamaged"/> with asynchronous reads, for a request: the answer is kept for the same file (path,
|
||||
/// length and time of the last write), so stepping back and forth in the viewer does not read and decode it again.
|
||||
/// </summary>
|
||||
public static async Task<bool> IsFileDamagedAsync(string path, CancellationToken ct)
|
||||
{
|
||||
ArgumentException.ThrowIfNullOrEmpty(path);
|
||||
(string, long, DateTime) key;
|
||||
try
|
||||
{
|
||||
var info = new FileInfo(path);
|
||||
if (!info.Exists)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
key = (info.FullName, info.Length, info.LastWriteTimeUtc);
|
||||
}
|
||||
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return data is not null && ImageDecoding.IsDamaged(data);
|
||||
if (Answers.TryGetValue(key, out var known))
|
||||
{
|
||||
return known;
|
||||
}
|
||||
|
||||
var answer = await ReadAndCheckAsync(path, ct).ConfigureAwait(false);
|
||||
if (answer is not { } damaged)
|
||||
{
|
||||
return false; // not readable now: not remembered
|
||||
}
|
||||
|
||||
if (Answers.Count >= RememberedAnswers)
|
||||
{
|
||||
Answers.Clear();
|
||||
}
|
||||
|
||||
Answers[key] = damaged;
|
||||
return damaged;
|
||||
}
|
||||
|
||||
private static async Task<bool?> ReadAndCheckAsync(string path, CancellationToken ct)
|
||||
{
|
||||
try
|
||||
{
|
||||
await using var stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite | FileShare.Delete, 81920, FileOptions.Asynchronous | FileOptions.SequentialScan);
|
||||
var length = stream.Length;
|
||||
if (length <= 0 || length > ImageDecoding.MaxFileBytes)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
var head = (int)Math.Min(length, ImageDecoding.HeaderBytes);
|
||||
var data = new byte[head];
|
||||
await stream.ReadExactlyAsync(data, ct).ConfigureAwait(false);
|
||||
if (!ImageDecoding.MayBeDamaged(data))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (length > head)
|
||||
{
|
||||
var whole = new byte[length];
|
||||
data.CopyTo(whole, 0);
|
||||
await stream.ReadExactlyAsync(whole.AsMemory(head), ct).ConfigureAwait(false);
|
||||
data = whole;
|
||||
}
|
||||
|
||||
return ImageDecoding.IsDamaged(data);
|
||||
}
|
||||
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,3 +1,4 @@
|
||||
using System.Buffers.Binary;
|
||||
using Febe.Core.Codecs.Heif;
|
||||
using Febe.Core.Codecs.Images;
|
||||
|
||||
@@ -112,6 +113,54 @@ internal static class ImageDecoding
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// From the first bytes of a file (at least the header of its format): false when <see cref="IsDamaged"/> would
|
||||
/// say false whatever follows — a format Febe does not decode, a HEIC, a PNG, GIF, BMP or WebP whose header gives
|
||||
/// more pixels than <see cref="MaxDecodedPixels"/> — so that such a file is not read whole only to be refused.
|
||||
/// JPEG decodes at a reduced size, so its header alone never rules it out.
|
||||
/// </summary>
|
||||
internal static bool MayBeDamaged(ReadOnlySpan<byte> head)
|
||||
{
|
||||
var format = head.IsEmpty ? ImageFormat.Unknown : ImageFormatSniffer.Detect(head);
|
||||
if (format == ImageFormat.Heif || !ImageDecoder.CanDecode(format))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return HeaderPixels(format, head) is not { } pixels || pixels <= MaxDecodedPixels;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Width × height as the header of a PNG (IHDR), GIF (logical screen), BMP (information header) or WebP (VP8X,
|
||||
/// VP8L or VP8 frame header) states it, from their public specifications; null when it is not there.
|
||||
/// </summary>
|
||||
internal static long? HeaderPixels(ImageFormat format, ReadOnlySpan<byte> h)
|
||||
{
|
||||
switch (format)
|
||||
{
|
||||
case ImageFormat.Png when h.Length >= 24 && h[12..16].SequenceEqual("IHDR"u8):
|
||||
return (long)BinaryPrimitives.ReadUInt32BigEndian(h[16..]) * BinaryPrimitives.ReadUInt32BigEndian(h[20..]);
|
||||
case ImageFormat.Gif when h.Length >= 10:
|
||||
return (long)BinaryPrimitives.ReadUInt16LittleEndian(h[6..]) * BinaryPrimitives.ReadUInt16LittleEndian(h[8..]);
|
||||
case ImageFormat.Bmp when h.Length >= 22 && BinaryPrimitives.ReadUInt32LittleEndian(h[14..]) == 12:
|
||||
return (long)BinaryPrimitives.ReadUInt16LittleEndian(h[18..]) * BinaryPrimitives.ReadUInt16LittleEndian(h[20..]);
|
||||
case ImageFormat.Bmp when h.Length >= 26:
|
||||
return Math.Abs((long)BinaryPrimitives.ReadInt32LittleEndian(h[18..])) * Math.Abs((long)BinaryPrimitives.ReadInt32LittleEndian(h[22..]));
|
||||
case ImageFormat.WebP when h.Length >= 30 && h[12..16].SequenceEqual("VP8X"u8):
|
||||
return (1L + (h[24] | (h[25] << 8) | (h[26] << 16))) * (1L + (h[27] | (h[28] << 8) | (h[29] << 16)));
|
||||
case ImageFormat.WebP when h.Length >= 25 && h[12..16].SequenceEqual("VP8L"u8) && h[20] == 0x2F:
|
||||
{
|
||||
var bits = BinaryPrimitives.ReadUInt32LittleEndian(h[21..]);
|
||||
return (1L + (bits & 0x3FFF)) * (1L + ((bits >> 14) & 0x3FFF));
|
||||
}
|
||||
|
||||
case ImageFormat.WebP when h.Length >= 30 && h[12..16].SequenceEqual("VP8 "u8) && h[23] == 0x9D && h[24] == 0x01 && h[25] == 0x2A:
|
||||
return (long)(BinaryPrimitives.ReadUInt16LittleEndian(h[26..]) & 0x3FFF) * (BinaryPrimitives.ReadUInt16LittleEndian(h[28..]) & 0x3FFF);
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>The smallest long side <see cref="IsDamaged"/> lets a JPEG be decoded at: the cheapest reduction.</summary>
|
||||
private const int DamageCheckSide = 1;
|
||||
|
||||
|
||||
Reference in new issue
Block a user