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
|
||||
|
||||
@@ -77,7 +77,8 @@ public sealed record MediaDto(
|
||||
string? Folder,
|
||||
string? Thumb,
|
||||
bool Available,
|
||||
string State);
|
||||
string State,
|
||||
bool Damaged = false);
|
||||
|
||||
/// <summary>Everything the viewer shows of an entry.</summary>
|
||||
public sealed record EntryDto(
|
||||
|
||||
@@ -237,6 +237,7 @@ public static class LibraryApi
|
||||
folder = string.IsNullOrEmpty(directory) ? accountFolder : Path.Combine(accountFolder, directory);
|
||||
}
|
||||
|
||||
var path = files.Resolve(account, m);
|
||||
return new MediaDto(
|
||||
m.Id,
|
||||
m.Position,
|
||||
@@ -248,8 +249,9 @@ public static class LibraryApi
|
||||
relative is null ? null : Path.GetFileName(LibraryLayout.ToPlatform(relative)),
|
||||
folder,
|
||||
m.HasThumbnail && m.Sha256 is not null ? m.Sha256 : m.Kind == MediaKind.Video ? "video" : null,
|
||||
files.Resolve(account, m) is not null,
|
||||
ApiMapping.State(m.State));
|
||||
path is not null,
|
||||
ApiMapping.State(m.State),
|
||||
IsDamaged(m, path));
|
||||
}).ToList();
|
||||
|
||||
return new EntryDto(
|
||||
@@ -272,6 +274,14 @@ public static class LibraryApi
|
||||
media);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// «File danneggiato o incompleto» (spec §4.6, §8.4): a photo on disk with no thumbnail whose bytes are damaged or
|
||||
/// truncated. A photo with a thumbnail was decoded when it was stored, so only those without one are looked at,
|
||||
/// one file each time the viewer opens it; the file is only read, never touched (rule 12).
|
||||
/// </summary>
|
||||
private static bool IsDamaged(Media media, string? path) =>
|
||||
media.Kind == MediaKind.Photo && !media.HasThumbnail && path is not null && DamagedImage.IsFileDamaged(path);
|
||||
|
||||
private static async Task<(Account Account, Media Media)> MediaAsync(IFebeStorage storage, long mediaId, CancellationToken ct)
|
||||
{
|
||||
var media = await storage.Media.GetAsync(mediaId, ct).ConfigureAwait(false) ?? throw new NotFoundException("File non trovato.");
|
||||
|
||||
@@ -143,6 +143,11 @@ export function openViewer({ tile, source, origin, onDeleted }) {
|
||||
h('p', t(media.kind === 'video' ? 'vis.videoMancante' : 'vis.mancante'))));
|
||||
return;
|
||||
}
|
||||
if (media.damaged) {
|
||||
// Spec §4.6, §8.4: a truncated or damaged photo is never shown half; the file stays, its actions too.
|
||||
fill(stage, h('div.mancante', icon('image', 'l'), h('p', h('b', t('vis.danneggiato'))), h('p', t('vis.danneggiatoTesto'))));
|
||||
return;
|
||||
}
|
||||
const src = `/api/media/${media.id}/file`;
|
||||
if (media.kind === 'video') {
|
||||
fill(stage, h('video', { src, poster: thumb, controls: true, autoplay: true, muted: true, playsinline: true, preload: 'metadata', 'aria-label': t('libreria.tipiTessera.' + entry.kind) }));
|
||||
|
||||
@@ -202,6 +202,8 @@
|
||||
"elimina": "Elimina dalla libreria",
|
||||
"mancante": "Il file non è sul disco: forse è stato spostato o cancellato fuori da Febe.",
|
||||
"videoMancante": "Il video non è sul disco: qui si vede solo la sua copertina.",
|
||||
"danneggiato": "File danneggiato o incompleto",
|
||||
"danneggiatoTesto": "Febe non riesce a leggere questa foto: forse lo scaricamento o la copia si sono interrotti. Il file resta nella libreria così com'è.",
|
||||
"tipoFile": "{tipo}, {media} {n} di {totale}",
|
||||
"tipoSingolo": "{tipo}, {media}",
|
||||
"seguito": "Seguito",
|
||||
|
||||
Reference in new issue
Block a user