mirror of
https://github.com/Belphemur/CBZOptimizer.git
synced 2025-10-13 20:18:52 +02:00
fix: Keep page as they are if we can't decode them and disable conversion
This commit is contained in:
@@ -18,6 +18,7 @@ func TestConvertChapter(t *testing.T) {
|
|||||||
name string
|
name string
|
||||||
genTestChapter func(path string, isSplit bool) (*manga.Chapter, []string, error)
|
genTestChapter func(path string, isSplit bool) (*manga.Chapter, []string, error)
|
||||||
split bool
|
split bool
|
||||||
|
expectError bool
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
name: "All split pages",
|
name: "All split pages",
|
||||||
@@ -28,6 +29,7 @@ func TestConvertChapter(t *testing.T) {
|
|||||||
name: "Big Pages, no split",
|
name: "Big Pages, no split",
|
||||||
genTestChapter: genHugePage,
|
genTestChapter: genHugePage,
|
||||||
split: false,
|
split: false,
|
||||||
|
expectError: true,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "No split pages",
|
name: "No split pages",
|
||||||
@@ -43,11 +45,13 @@ func TestConvertChapter(t *testing.T) {
|
|||||||
name: "Mix of Huge and small page",
|
name: "Mix of Huge and small page",
|
||||||
genTestChapter: genMixSmallHuge,
|
genTestChapter: genMixSmallHuge,
|
||||||
split: false,
|
split: false,
|
||||||
|
expectError: true,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "Two corrupted pages",
|
name: "Two corrupted pages",
|
||||||
genTestChapter: genTwoCorrupted,
|
genTestChapter: genTwoCorrupted,
|
||||||
split: false,
|
split: false,
|
||||||
|
expectError: true,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
// Load test genTestChapter from testdata
|
// 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)
|
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)
|
t.Fatalf("failed to convert genTestChapter: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -247,12 +251,7 @@ func genTwoCorrupted(path string, isSplit bool) (*manga.Chapter, []string, error
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if isCorrupted {
|
if isCorrupted {
|
||||||
img := image.NewRGBA(image.Rect(0, 0, 1, 17000)) // Too tall for WebP without split
|
buf = bytes.NewBufferString("corrupted data") // Invalid data, can't decode as image
|
||||||
buf = new(bytes.Buffer)
|
|
||||||
err = jpeg.Encode(buf, img, nil)
|
|
||||||
if err != nil {
|
|
||||||
return nil, nil, err
|
|
||||||
}
|
|
||||||
ext = ".jpg"
|
ext = ".jpg"
|
||||||
} else {
|
} else {
|
||||||
img := image.NewRGBA(image.Rect(0, 0, 300, 1000))
|
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)
|
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"}
|
expectedExtensions := []string{".webp", ".webp", ".jpg", ".webp", ".jpg"}
|
||||||
if isSplit {
|
// Even with split, corrupted pages can't be decoded so stay as is
|
||||||
// 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"}
|
|
||||||
}
|
|
||||||
|
|
||||||
return &manga.Chapter{
|
return &manga.Chapter{
|
||||||
FilePath: path,
|
FilePath: path,
|
||||||
|
@@ -171,21 +171,15 @@ func (converter *Converter) ConvertChapter(ctx context.Context, chapter *manga.C
|
|||||||
var pageIgnoredError *converterrors.PageIgnoredError
|
var pageIgnoredError *converterrors.PageIgnoredError
|
||||||
if errors.As(err, &pageIgnoredError) {
|
if errors.As(err, &pageIgnoredError) {
|
||||||
log.Info().Err(err).Msg("Page ignored due to image decode error")
|
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)
|
wgConvertedPages.Add(1)
|
||||||
select {
|
select {
|
||||||
case pagesChan <- manga.NewContainer(page, img, format, false):
|
case pagesChan <- manga.NewContainer(page, img, format, false):
|
||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
return
|
return
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -264,6 +258,7 @@ func (converter *Converter) ConvertChapter(ctx context.Context, chapter *manga.C
|
|||||||
log.Debug().
|
log.Debug().
|
||||||
Str("chapter", chapter.FilePath).
|
Str("chapter", chapter.FilePath).
|
||||||
Int("error_count", len(errList)).
|
Int("error_count", len(errList)).
|
||||||
|
Err(errors.Join(errList...)).
|
||||||
Msg("Conversion completed with errors")
|
Msg("Conversion completed with errors")
|
||||||
} else {
|
} else {
|
||||||
log.Debug().
|
log.Debug().
|
||||||
|
@@ -154,7 +154,7 @@ func TestConverter_ConvertChapter(t *testing.T) {
|
|||||||
name: "Tall image without split",
|
name: "Tall image without split",
|
||||||
pages: []*manga.Page{createTestPage(t, 1, 800, webpMaxHeight+100, "png")},
|
pages: []*manga.Page{createTestPage(t, 1, 800, webpMaxHeight+100, "png")},
|
||||||
split: false,
|
split: false,
|
||||||
expectError: false,
|
expectError: true,
|
||||||
numExpected: 1,
|
numExpected: 1,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user