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,81 @@
|
||||
using System.Security.Cryptography;
|
||||
using Febe.Core.Domain;
|
||||
using Febe.Core.Library;
|
||||
using Febe.Tests.Support;
|
||||
|
||||
namespace Febe.Tests.Library;
|
||||
|
||||
/// <summary>
|
||||
/// «File danneggiato o incompleto» (spec §4.6, §8.4; Alberto's answer of 25 November 2026): a truncated PNG or JPEG
|
||||
/// is refused and said so, with no thumbnail and no perceptual hash, and the file stays in the library as it came.
|
||||
/// </summary>
|
||||
[TestClass]
|
||||
public sealed class DamagedImageTests
|
||||
{
|
||||
[TestMethod]
|
||||
public void ATruncatedPngOrJpegIsDamagedAndWhatFebeDoesNotReadIsNot()
|
||||
{
|
||||
using var scene = TestImages.Scene(4, 320, 240);
|
||||
var jpeg = TestImages.Jpeg(scene, 85);
|
||||
var png = TestImages.Png(scene);
|
||||
|
||||
Assert.IsFalse(DamagedImage.IsDamaged(jpeg), "un JPEG intero");
|
||||
Assert.IsFalse(DamagedImage.IsDamaged(png), "un PNG intero");
|
||||
Assert.IsTrue(DamagedImage.IsDamaged(jpeg[..(jpeg.Length / 2)]), "un JPEG che finisce a metà della scansione");
|
||||
Assert.IsTrue(DamagedImage.IsDamaged(png[..(png.Length / 2)]), "un PNG che finisce a metà dei dati");
|
||||
Assert.IsTrue(DamagedImage.IsDamaged(png[..40]), "un PNG che finisce dentro l'intestazione");
|
||||
|
||||
// Not damaged: Febe just has no picture of them.
|
||||
Assert.IsFalse(DamagedImage.IsDamaged(ReadOnlyMemory<byte>.Empty));
|
||||
Assert.IsFalse(DamagedImage.IsDamaged(new byte[100]), "un formato sconosciuto");
|
||||
byte[] heic = [0, 0, 0, 24, .. "ftypheic"u8, 0, 0, 0, 0, .. "mif1heic"u8, .. new byte[64]];
|
||||
Assert.IsFalse(DamagedImage.IsDamaged(heic), "HEIC: Febe non lo legge, ma non è rovinato");
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void AFileOnDiskIsReadOnceAndLeftAsItIs()
|
||||
{
|
||||
using var temp = new TempFolder();
|
||||
using var scene = TestImages.Scene(5, 200, 150);
|
||||
var cut = TestImages.Jpeg(scene)[..900];
|
||||
var path = temp.Combine("foto.jpg");
|
||||
File.WriteAllBytes(path, cut);
|
||||
var written = File.GetLastWriteTimeUtc(path);
|
||||
|
||||
Assert.IsTrue(DamagedImage.IsFileDamaged(path));
|
||||
CollectionAssert.AreEqual(cut, File.ReadAllBytes(path), "il file resta com'è");
|
||||
Assert.AreEqual(written, File.GetLastWriteTimeUtc(path));
|
||||
Assert.IsFalse(DamagedImage.IsFileDamaged(temp.Combine("non-c-e.jpg")), "un file che non c'è non è danneggiato");
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task ADamagedPhotoIsStoredWithItsShaButNoThumbnailOrHashAndStays()
|
||||
{
|
||||
using var temp = new TempFolder();
|
||||
await using var library = await TestLibrary.CreateAsync(temp);
|
||||
var account = await library.Db.Accounts.AddAsync(Make.Account("marta.demo", "100"));
|
||||
using var scene = TestImages.Scene(6, 640, 480);
|
||||
var whole = TestImages.Jpeg(scene, 88);
|
||||
var cutJpeg = whole[..(whole.Length * 2 / 3)];
|
||||
var png = TestImages.Png(scene);
|
||||
var cutPng = png[..(png.Length / 2)];
|
||||
|
||||
var saved = await library.SaveAsync(account, Make.Entry(account.Id, "701", Make.T0), [new TestFile(Make.Photo(1), cutJpeg, ".jpg"), new TestFile(Make.Photo(2), cutPng, ".png")]);
|
||||
var intact = await library.SaveAsync(account, Make.Entry(account.Id, "702", Make.T0 + TimeSpan.FromHours(1)), [new TestFile(Make.Photo(1), whole, ".jpg")]);
|
||||
|
||||
Assert.AreEqual(EntryState.Downloaded, saved.Entry.State, "scaricato come gli altri: niente si riscarica da sé");
|
||||
foreach (var (media, bytes) in saved.Media.Zip([cutJpeg, cutPng]))
|
||||
{
|
||||
Assert.AreEqual(MediaState.Downloaded, media.State);
|
||||
Assert.IsNull(media.PerceptualHash, "nessuna impronta visiva: mai doppione probabile per impronta");
|
||||
Assert.IsFalse(media.HasThumbnail, "la tessera neutra");
|
||||
Assert.AreEqual(Convert.ToHexStringLower(SHA256.HashData(bytes)), media.Sha256, "lo SHA-256 sì");
|
||||
var path = library.Layout.FullPath(account, media.RelativePath!);
|
||||
CollectionAssert.AreEqual(bytes, await File.ReadAllBytesAsync(path), "il file resta in libreria com'è arrivato");
|
||||
Assert.IsTrue(DamagedImage.IsFileDamaged(path));
|
||||
}
|
||||
|
||||
Assert.IsNotNull(intact.Media[0].PerceptualHash, "la stessa foto intera ha la sua impronta");
|
||||
Assert.IsTrue(intact.Media[0].HasThumbnail);
|
||||
}
|
||||
}
|
||||
@@ -111,6 +111,8 @@ public sealed class LibraryApiTests
|
||||
Assert.AreEqual("foto_1.jpg", media[0].Str("fileName"));
|
||||
Assert.AreEqual(Path.Combine("marta.demo", "post"), media[0].Str("folder"));
|
||||
Assert.IsFalse(media[1].Bool("available"), "a file missing from disk is said, not hidden");
|
||||
Assert.IsFalse(media[0].Bool("damaged"), "a whole photo without a thumbnail yet is not damaged");
|
||||
Assert.IsFalse(media[1].Bool("damaged"));
|
||||
|
||||
using (var file = await server.Client.GetAsync($"/api/media/{media[0].Long("id")}/file?download=1"))
|
||||
{
|
||||
@@ -149,6 +151,49 @@ public sealed class LibraryApiTests
|
||||
Assert.AreEqual(JsonValueKind.Null, after.GetProperty("instagramUrl").ValueKind);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task ADamagedPhotoHasTheNeutralTileTheViewerSaysSoAndItsFileStays()
|
||||
{
|
||||
await using var server = await WebTestServer.StartAsync();
|
||||
var account = await server.Db.Accounts.AddAsync(Make.Account("marta.demo"));
|
||||
var folder = server.Library.Layout.AccountFolder(account);
|
||||
Directory.CreateDirectory(Path.Combine(folder, "post"));
|
||||
var whole = TestImages.Jpeg(TestImages.Scene(7, 240, 180));
|
||||
var cut = whole[..(whole.Length / 2)];
|
||||
var png = TestImages.Png(TestImages.Scene(8, 120, 90));
|
||||
var cutPng = png[..(png.Length / 2)];
|
||||
await File.WriteAllBytesAsync(Path.Combine(folder, "post", "foto_1.jpg"), cut);
|
||||
await File.WriteAllBytesAsync(Path.Combine(folder, "post", "foto_2.png"), cutPng);
|
||||
await File.WriteAllBytesAsync(Path.Combine(folder, "post", "foto_3.jpg"), whole);
|
||||
var added = await server.Db.Entries.AddAsync(
|
||||
Make.Entry(account.Id, "1", Make.T0) with { State = EntryState.Downloaded },
|
||||
[
|
||||
Make.Stored(Make.Photo(1), Make.Sha(1), "post/foto_1.jpg") with { Bytes = cut.Length },
|
||||
Make.Stored(Make.Photo(2), Make.Sha(2), "post/foto_2.png") with { Bytes = cutPng.Length },
|
||||
Make.Stored(Make.Photo(3), Make.Sha(3), "post/foto_3.jpg") with { Bytes = whole.Length },
|
||||
]);
|
||||
var id = added.Stored.Entry.Id;
|
||||
|
||||
var tile = (await server.GetJsonAsync("/api/library")).Items("tiles").Single();
|
||||
Assert.AreEqual(JsonValueKind.Null, tile.GetProperty("cover").GetProperty("thumb").ValueKind, "the neutral tile in the mosaic");
|
||||
|
||||
var media = (await server.GetJsonAsync($"/api/entries/{id}")).Items("media").ToList();
|
||||
Assert.IsTrue(media[0].Bool("damaged"), "a JPEG cut in its scan: «File danneggiato o incompleto»");
|
||||
Assert.IsTrue(media[1].Bool("damaged"), "a PNG cut in its data");
|
||||
Assert.IsFalse(media[2].Bool("damaged"));
|
||||
Assert.IsTrue(media.All(m => m.Bool("available")), "the file is in the library: its actions stay");
|
||||
|
||||
// The file stays as it came, and can still be downloaded (rule 12: Febe never deletes or replaces it).
|
||||
using (var file = await server.Client.GetAsync($"/api/media/{media[0].Long("id")}/file?download=1"))
|
||||
{
|
||||
Assert.AreEqual(HttpStatusCode.OK, file.StatusCode);
|
||||
CollectionAssert.AreEqual(cut, await file.Content.ReadAsByteArrayAsync());
|
||||
}
|
||||
|
||||
CollectionAssert.AreEqual(cut, await File.ReadAllBytesAsync(Path.Combine(folder, "post", "foto_1.jpg")));
|
||||
Assert.AreEqual(MediaState.Downloaded, (await server.Db.Media.GetAsync(media[0].Long("id")))!.State);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task DeletionCanLetTheEntryComeBack()
|
||||
{
|
||||
|
||||
Reference in new issue
Block a user