mirror of
https://github.com/Belphemur/CBZOptimizer.git
synced 2025-10-14 04:28:51 +02:00
feat(webp): add partial success to conversion
So we only keep images that couldn't be optimized and return the chapter
This commit is contained in:
@@ -12,6 +12,9 @@ import (
|
|||||||
type Converter interface {
|
type Converter interface {
|
||||||
// Format of the converter
|
// Format of the converter
|
||||||
Format() (format constant.ConversionFormat)
|
Format() (format constant.ConversionFormat)
|
||||||
|
// ConvertChapter converts a manga chapter to the specified format.
|
||||||
|
//
|
||||||
|
// Returns partial success where some pages are converted and some are not.
|
||||||
ConvertChapter(chapter *manga.Chapter, quality uint8, split bool, progress func(message string, current uint32, total uint32)) (*manga.Chapter, error)
|
ConvertChapter(chapter *manga.Chapter, quality uint8, split bool, progress func(message string, current uint32, total uint32)) (*manga.Chapter, error)
|
||||||
PrepareConverter() error
|
PrepareConverter() error
|
||||||
}
|
}
|
||||||
|
@@ -14,34 +14,46 @@ import (
|
|||||||
func TestConvertChapter(t *testing.T) {
|
func TestConvertChapter(t *testing.T) {
|
||||||
|
|
||||||
testCases := []struct {
|
testCases := []struct {
|
||||||
name string
|
name string
|
||||||
genTestChapter func(path string) (*manga.Chapter, error)
|
genTestChapter func(path string) (*manga.Chapter, error)
|
||||||
split bool
|
split bool
|
||||||
expectFailure []constant.ConversionFormat
|
expectFailure []constant.ConversionFormat
|
||||||
|
expectPartialSuccess []constant.ConversionFormat
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
name: "All split pages",
|
name: "All split pages",
|
||||||
genTestChapter: genBigPages,
|
genTestChapter: genHugePages,
|
||||||
split: true,
|
split: true,
|
||||||
expectFailure: []constant.ConversionFormat{},
|
expectFailure: []constant.ConversionFormat{},
|
||||||
|
expectPartialSuccess: []constant.ConversionFormat{},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "Big Pages, no split",
|
name: "Big Pages, no split",
|
||||||
genTestChapter: genBigPages,
|
genTestChapter: genHugePages,
|
||||||
split: false,
|
split: false,
|
||||||
expectFailure: []constant.ConversionFormat{constant.WebP},
|
expectFailure: []constant.ConversionFormat{constant.WebP},
|
||||||
|
expectPartialSuccess: []constant.ConversionFormat{},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "No split pages",
|
name: "No split pages",
|
||||||
genTestChapter: genSmallPages,
|
genTestChapter: genSmallPages,
|
||||||
split: false,
|
split: false,
|
||||||
expectFailure: []constant.ConversionFormat{},
|
expectFailure: []constant.ConversionFormat{},
|
||||||
|
expectPartialSuccess: []constant.ConversionFormat{},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "Mix of split and no split pages",
|
name: "Mix of split and no split pages",
|
||||||
genTestChapter: genMixSmallBig,
|
genTestChapter: genMixSmallBig,
|
||||||
split: true,
|
split: true,
|
||||||
expectFailure: []constant.ConversionFormat{},
|
expectFailure: []constant.ConversionFormat{},
|
||||||
|
expectPartialSuccess: []constant.ConversionFormat{},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Mix of Huge and small page",
|
||||||
|
genTestChapter: genMixSmallHuge,
|
||||||
|
split: false,
|
||||||
|
expectFailure: []constant.ConversionFormat{},
|
||||||
|
expectPartialSuccess: []constant.ConversionFormat{constant.WebP},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
// Load test genTestChapter from testdata
|
// Load test genTestChapter from testdata
|
||||||
@@ -72,6 +84,10 @@ func TestConvertChapter(t *testing.T) {
|
|||||||
|
|
||||||
convertedChapter, err := converter.ConvertChapter(chapter, quality, tc.split, progress)
|
convertedChapter, err := converter.ConvertChapter(chapter, quality, tc.split, progress)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
if convertedChapter != nil && slices.Contains(tc.expectPartialSuccess, converter.Format()) {
|
||||||
|
t.Logf("Partial success to convert genTestChapter: %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
if slices.Contains(tc.expectFailure, converter.Format()) {
|
if slices.Contains(tc.expectFailure, converter.Format()) {
|
||||||
t.Logf("Expected failure to convert genTestChapter: %v", err)
|
t.Logf("Expected failure to convert genTestChapter: %v", err)
|
||||||
return
|
return
|
||||||
@@ -85,6 +101,10 @@ func TestConvertChapter(t *testing.T) {
|
|||||||
t.Fatalf("no pages were converted")
|
t.Fatalf("no pages were converted")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if len(convertedChapter.Pages) != len(chapter.Pages) {
|
||||||
|
t.Fatalf("converted chapter has different number of pages")
|
||||||
|
}
|
||||||
|
|
||||||
for _, page := range convertedChapter.Pages {
|
for _, page := range convertedChapter.Pages {
|
||||||
if page.Extension != ".webp" {
|
if page.Extension != ".webp" {
|
||||||
t.Errorf("page %d was not converted to webp format", page.Index)
|
t.Errorf("page %d was not converted to webp format", page.Index)
|
||||||
@@ -96,7 +116,7 @@ func TestConvertChapter(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func genBigPages(path string) (*manga.Chapter, error) {
|
func genHugePages(path string) (*manga.Chapter, error) {
|
||||||
file, err := os.Open(path)
|
file, err := os.Open(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -182,3 +202,32 @@ func genMixSmallBig(path string) (*manga.Chapter, error) {
|
|||||||
Pages: pages,
|
Pages: pages,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func genMixSmallHuge(path string) (*manga.Chapter, error) {
|
||||||
|
file, err := os.Open(path)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer file.Close()
|
||||||
|
|
||||||
|
var pages []*manga.Page
|
||||||
|
for i := 0; i < 10; i++ { // Assuming there are 5 pages for the test
|
||||||
|
img := image.NewRGBA(image.Rect(0, 0, 300, 2000*(i+1)))
|
||||||
|
buf := new(bytes.Buffer)
|
||||||
|
err := jpeg.Encode(buf, img, nil)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
page := &manga.Page{
|
||||||
|
Index: uint16(i),
|
||||||
|
Contents: buf,
|
||||||
|
Extension: ".jpg",
|
||||||
|
}
|
||||||
|
pages = append(pages, page)
|
||||||
|
}
|
||||||
|
|
||||||
|
return &manga.Chapter{
|
||||||
|
FilePath: path,
|
||||||
|
Pages: pages,
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
13
converter/errors/converter_errors.go
Normal file
13
converter/errors/converter_errors.go
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
package errors
|
||||||
|
|
||||||
|
type PageIgnoredError struct {
|
||||||
|
s string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e *PageIgnoredError) Error() string {
|
||||||
|
return e.s
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewPageIgnored(text string) error {
|
||||||
|
return &PageIgnoredError{text}
|
||||||
|
}
|
@@ -2,8 +2,10 @@ package webp
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/belphemur/CBZOptimizer/converter/constant"
|
"github.com/belphemur/CBZOptimizer/converter/constant"
|
||||||
|
converterrors "github.com/belphemur/CBZOptimizer/converter/errors"
|
||||||
"github.com/belphemur/CBZOptimizer/manga"
|
"github.com/belphemur/CBZOptimizer/manga"
|
||||||
"github.com/oliamb/cutter"
|
"github.com/oliamb/cutter"
|
||||||
"golang.org/x/exp/slices"
|
"golang.org/x/exp/slices"
|
||||||
@@ -109,17 +111,22 @@ func (converter *Converter) ConvertChapter(chapter *manga.Chapter, quality uint8
|
|||||||
splitNeeded, img, format, err := converter.checkPageNeedsSplit(page, split)
|
splitNeeded, img, format, err := converter.checkPageNeedsSplit(page, split)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
errChan <- err
|
errChan <- err
|
||||||
|
// Partial error in this case, we want the page, but not converting it
|
||||||
|
if img != nil {
|
||||||
|
wgConvertedPages.Add(1)
|
||||||
|
pagesChan <- manga.NewContainer(page, img, format, false)
|
||||||
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if !splitNeeded {
|
if !splitNeeded {
|
||||||
wgConvertedPages.Add(1)
|
wgConvertedPages.Add(1)
|
||||||
pagesChan <- manga.NewContainer(page, img, format)
|
pagesChan <- manga.NewContainer(page, img, format, true)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
images, err := converter.cropImage(img)
|
images, err := converter.cropImage(img)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
errChan <- fmt.Errorf("error converting page %d of genTestChapter %s to webp: %v", page.Index, chapter.FilePath, err)
|
errChan <- err
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -127,7 +134,7 @@ func (converter *Converter) ConvertChapter(chapter *manga.Chapter, quality uint8
|
|||||||
for i, img := range images {
|
for i, img := range images {
|
||||||
page := &manga.Page{Index: page.Index, IsSplitted: true, SplitPartIndex: uint16(i)}
|
page := &manga.Page{Index: page.Index, IsSplitted: true, SplitPartIndex: uint16(i)}
|
||||||
wgConvertedPages.Add(1)
|
wgConvertedPages.Add(1)
|
||||||
pagesChan <- manga.NewContainer(page, img, "N/A")
|
pagesChan <- manga.NewContainer(page, img, "N/A", true)
|
||||||
}
|
}
|
||||||
}(page)
|
}(page)
|
||||||
}
|
}
|
||||||
@@ -142,8 +149,9 @@ func (converter *Converter) ConvertChapter(chapter *manga.Chapter, quality uint8
|
|||||||
errList = append(errList, err)
|
errList = append(errList, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var aggregatedError error = nil
|
||||||
if len(errList) > 0 {
|
if len(errList) > 0 {
|
||||||
return nil, fmt.Errorf("encountered errors: %v", errList)
|
aggregatedError = errors.Join(errList...)
|
||||||
}
|
}
|
||||||
|
|
||||||
slices.SortFunc(pages, func(a, b *manga.Page) int {
|
slices.SortFunc(pages, func(a, b *manga.Page) int {
|
||||||
@@ -156,7 +164,7 @@ func (converter *Converter) ConvertChapter(chapter *manga.Chapter, quality uint8
|
|||||||
|
|
||||||
runtime.GC()
|
runtime.GC()
|
||||||
|
|
||||||
return chapter, nil
|
return chapter, aggregatedError
|
||||||
}
|
}
|
||||||
|
|
||||||
func (converter *Converter) cropImage(img image.Image) ([]image.Image, error) {
|
func (converter *Converter) cropImage(img image.Image) ([]image.Image, error) {
|
||||||
@@ -203,7 +211,7 @@ func (converter *Converter) checkPageNeedsSplit(page *manga.Page, splitRequested
|
|||||||
height := bounds.Dy()
|
height := bounds.Dy()
|
||||||
|
|
||||||
if height >= webpMaxHeight && !splitRequested {
|
if height >= webpMaxHeight && !splitRequested {
|
||||||
return false, img, format, fmt.Errorf("page[%d] height %d exceeds maximum height %d of webp format", page.Index, height, webpMaxHeight)
|
return false, img, format, converterrors.NewPageIgnored(fmt.Sprintf("page %d is too tall to be converted to webp format", page.Index))
|
||||||
}
|
}
|
||||||
return height >= converter.maxHeight && splitRequested, img, format, nil
|
return height >= converter.maxHeight && splitRequested, img, format, nil
|
||||||
}
|
}
|
||||||
@@ -212,6 +220,9 @@ func (converter *Converter) convertPage(container *manga.PageContainer, quality
|
|||||||
if container.Format == "webp" {
|
if container.Format == "webp" {
|
||||||
return container, nil
|
return container, nil
|
||||||
}
|
}
|
||||||
|
if !container.IsToBeConverted {
|
||||||
|
return container, nil
|
||||||
|
}
|
||||||
converted, err := converter.convert(container.Image, uint(quality))
|
converted, err := converter.convert(container.Image, uint(quality))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@@ -10,8 +10,10 @@ type PageContainer struct {
|
|||||||
Image image.Image
|
Image image.Image
|
||||||
// Format is a string representing the format of the image (e.g., "png", "jpeg", "webp").
|
// Format is a string representing the format of the image (e.g., "png", "jpeg", "webp").
|
||||||
Format string
|
Format string
|
||||||
|
// IsToBeConverted is a boolean flag indicating whether the image needs to be converted to another format.
|
||||||
|
IsToBeConverted bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewContainer(Page *Page, img image.Image, format string) *PageContainer {
|
func NewContainer(Page *Page, img image.Image, format string, isToBeConverted bool) *PageContainer {
|
||||||
return &PageContainer{Page: Page, Image: img, Format: format}
|
return &PageContainer{Page: Page, Image: img, Format: format, IsToBeConverted: isToBeConverted}
|
||||||
}
|
}
|
||||||
|
@@ -1,9 +1,11 @@
|
|||||||
package utils
|
package utils
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/belphemur/CBZOptimizer/cbz"
|
"github.com/belphemur/CBZOptimizer/cbz"
|
||||||
"github.com/belphemur/CBZOptimizer/converter"
|
"github.com/belphemur/CBZOptimizer/converter"
|
||||||
|
errors2 "github.com/belphemur/CBZOptimizer/converter/errors"
|
||||||
"log"
|
"log"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
@@ -38,8 +40,15 @@ func Optimize(options *OptimizeOptions) error {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to convert chapter: %v", err)
|
var pageIgnoredError *errors2.PageIgnoredError
|
||||||
|
if !errors.As(err, &pageIgnoredError) {
|
||||||
|
return fmt.Errorf("failed to convert chapter: %v", err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
if convertedChapter == nil {
|
||||||
|
return fmt.Errorf("failed to convert chapter")
|
||||||
|
}
|
||||||
|
|
||||||
convertedChapter.SetConverted()
|
convertedChapter.SetConverted()
|
||||||
|
|
||||||
// Write the converted chapter back to a CBZ file
|
// Write the converted chapter back to a CBZ file
|
||||||
|
Reference in New Issue
Block a user