From ede8d62572293cd1c89c91e65b0f89c2315b2769 Mon Sep 17 00:00:00 2001 From: Antoine Aflalo <197810+Belphemur@users.noreply.github.com> Date: Wed, 3 Sep 2025 22:15:10 -0400 Subject: [PATCH] fix: Keep page as they are if we can't decode them and disable conversion --- pkg/converter/converter_test.go | 23 ++++++++--------------- pkg/converter/webp/webp_converter.go | 21 ++++++++------------- pkg/converter/webp/webp_converter_test.go | 2 +- 3 files changed, 17 insertions(+), 29 deletions(-) diff --git a/pkg/converter/converter_test.go b/pkg/converter/converter_test.go index 08e1cd5..35c9246 100644 --- a/pkg/converter/converter_test.go +++ b/pkg/converter/converter_test.go @@ -18,6 +18,7 @@ func TestConvertChapter(t *testing.T) { name string genTestChapter func(path string, isSplit bool) (*manga.Chapter, []string, error) split bool + expectError bool }{ { name: "All split pages", @@ -28,6 +29,7 @@ func TestConvertChapter(t *testing.T) { name: "Big Pages, no split", genTestChapter: genHugePage, split: false, + expectError: true, }, { name: "No split pages", @@ -43,11 +45,13 @@ func TestConvertChapter(t *testing.T) { name: "Mix of Huge and small page", genTestChapter: genMixSmallHuge, split: false, + expectError: true, }, { name: "Two corrupted pages", genTestChapter: genTwoCorrupted, split: false, + expectError: true, }, } // Load test genTestChapter from testdata @@ -77,7 +81,7 @@ func TestConvertChapter(t *testing.T) { } convertedChapter, err := converter.ConvertChapter(context.Background(), chapter, quality, tc.split, progress) - if err != nil { + if err != nil && !tc.expectError { t.Fatalf("failed to convert genTestChapter: %v", err) } @@ -247,12 +251,7 @@ func genTwoCorrupted(path string, isSplit bool) (*manga.Chapter, []string, error } } if isCorrupted { - img := image.NewRGBA(image.Rect(0, 0, 1, 17000)) // Too tall for WebP without split - buf = new(bytes.Buffer) - err = jpeg.Encode(buf, img, nil) - if err != nil { - return nil, nil, err - } + buf = bytes.NewBufferString("corrupted data") // Invalid data, can't decode as image ext = ".jpg" } else { img := image.NewRGBA(image.Rect(0, 0, 300, 1000)) @@ -271,15 +270,9 @@ func genTwoCorrupted(path string, isSplit bool) (*manga.Chapter, []string, error pages = append(pages, page) } - // Expected: small pages to .webp, corrupted large pages to .jpg (kept as is) + // Expected: small pages to .webp, corrupted pages to .jpg (kept as is) expectedExtensions := []string{".webp", ".webp", ".jpg", ".webp", ".jpg"} - if isSplit { - // With split, even large pages get split and converted - // Small pages: 1 each -> 3 - // Large pages: 9 each (17000 height, cropHeight 2000) -> 2*9=18 - // Total: 21 - expectedExtensions = []string{".webp", ".webp", ".webp", ".webp", ".webp", ".webp", ".webp", ".webp", ".webp", ".webp", ".webp", ".webp", ".webp", ".webp", ".webp", ".webp", ".webp", ".webp", ".webp", ".webp", ".webp"} - } + // Even with split, corrupted pages can't be decoded so stay as is return &manga.Chapter{ FilePath: path, diff --git a/pkg/converter/webp/webp_converter.go b/pkg/converter/webp/webp_converter.go index 8519829..1358f3c 100644 --- a/pkg/converter/webp/webp_converter.go +++ b/pkg/converter/webp/webp_converter.go @@ -171,21 +171,15 @@ func (converter *Converter) ConvertChapter(ctx context.Context, chapter *manga.C var pageIgnoredError *converterrors.PageIgnoredError if errors.As(err, &pageIgnoredError) { log.Info().Err(err).Msg("Page ignored due to image decode error") - } else { - select { - case errChan <- err: - case <-ctx.Done(): - return - } } - if img != nil { - wgConvertedPages.Add(1) - select { - case pagesChan <- manga.NewContainer(page, img, format, false): - case <-ctx.Done(): - return - } + + wgConvertedPages.Add(1) + select { + case pagesChan <- manga.NewContainer(page, img, format, false): + case <-ctx.Done(): + return } + return } @@ -264,6 +258,7 @@ func (converter *Converter) ConvertChapter(ctx context.Context, chapter *manga.C log.Debug(). Str("chapter", chapter.FilePath). Int("error_count", len(errList)). + Err(errors.Join(errList...)). Msg("Conversion completed with errors") } else { log.Debug(). diff --git a/pkg/converter/webp/webp_converter_test.go b/pkg/converter/webp/webp_converter_test.go index c421182..48ba729 100644 --- a/pkg/converter/webp/webp_converter_test.go +++ b/pkg/converter/webp/webp_converter_test.go @@ -154,7 +154,7 @@ func TestConverter_ConvertChapter(t *testing.T) { name: "Tall image without split", pages: []*manga.Page{createTestPage(t, 1, 800, webpMaxHeight+100, "png")}, split: false, - expectError: false, + expectError: true, numExpected: 1, }, }