mirror of
https://github.com/Belphemur/CBZOptimizer.git
synced 2025-10-14 12:38:50 +02:00
refactor: change the optimize command
This commit is contained in:
@@ -2,9 +2,9 @@ package cmd
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/belphemur/CBZOptimizer/cbz"
|
|
||||||
"github.com/belphemur/CBZOptimizer/converter"
|
"github.com/belphemur/CBZOptimizer/converter"
|
||||||
"github.com/belphemur/CBZOptimizer/converter/constant"
|
"github.com/belphemur/CBZOptimizer/converter/constant"
|
||||||
|
"github.com/belphemur/CBZOptimizer/manga"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"github.com/thediveo/enumflag/v2"
|
"github.com/thediveo/enumflag/v2"
|
||||||
"os"
|
"os"
|
||||||
@@ -95,42 +95,10 @@ func ConvertCbzCommand(cmd *cobra.Command, args []string) error {
|
|||||||
go func() {
|
go func() {
|
||||||
defer wg.Done()
|
defer wg.Done()
|
||||||
for path := range fileChan {
|
for path := range fileChan {
|
||||||
fmt.Printf("Processing file: %s\n", path)
|
err := manga.Optimize(chapterConverter, path, quality, override)
|
||||||
|
|
||||||
// Load the chapter
|
|
||||||
chapter, err := cbz.LoadChapter(path)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
errorChan <- fmt.Errorf("failed to load chapter: %v", err)
|
errorChan <- fmt.Errorf("error processing file %s: %w", path, err)
|
||||||
continue
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if chapter.IsConverted {
|
|
||||||
fmt.Printf("Chapter already converted: %s\n", path)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
// Convert the chapter
|
|
||||||
convertedChapter, err := chapterConverter.ConvertChapter(chapter, quality, func(msg string) {
|
|
||||||
fmt.Println(msg)
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
errorChan <- fmt.Errorf("failed to convert chapter: %v", err)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
convertedChapter.SetConverted()
|
|
||||||
|
|
||||||
// Write the converted chapter back to a CBZ file
|
|
||||||
outputPath := path
|
|
||||||
if !override {
|
|
||||||
outputPath = strings.TrimSuffix(path, ".cbz") + "_converted.cbz"
|
|
||||||
}
|
|
||||||
err = cbz.WriteChapterToCBZ(convertedChapter, outputPath)
|
|
||||||
if err != nil {
|
|
||||||
errorChan <- fmt.Errorf("failed to write converted chapter: %v", err)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
fmt.Printf("Converted file written to: %s\n", outputPath)
|
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
}
|
}
|
47
manga/optimize.go
Normal file
47
manga/optimize.go
Normal file
@@ -0,0 +1,47 @@
|
|||||||
|
package manga
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/belphemur/CBZOptimizer/cbz"
|
||||||
|
"github.com/belphemur/CBZOptimizer/converter"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Optimize optimizes a CBZ file using the specified converter.
|
||||||
|
func Optimize(chapterConverter converter.Converter, path string, quality uint8, override bool) error {
|
||||||
|
fmt.Printf("Processing file: %s\n", path)
|
||||||
|
|
||||||
|
// Load the chapter
|
||||||
|
chapter, err := cbz.LoadChapter(path)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to load chapter: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if chapter.IsConverted {
|
||||||
|
fmt.Printf("Chapter already converted: %s\n", path)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Convert the chapter
|
||||||
|
convertedChapter, err := chapterConverter.ConvertChapter(chapter, quality, func(msg string) {
|
||||||
|
fmt.Println(msg)
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to convert chapter: %v", err)
|
||||||
|
}
|
||||||
|
convertedChapter.SetConverted()
|
||||||
|
|
||||||
|
// Write the converted chapter back to a CBZ file
|
||||||
|
outputPath := path
|
||||||
|
if !override {
|
||||||
|
outputPath = strings.TrimSuffix(path, ".cbz") + "_converted.cbz"
|
||||||
|
}
|
||||||
|
err = cbz.WriteChapterToCBZ(convertedChapter, outputPath)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to write converted chapter: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Printf("Converted file written to: %s\n", outputPath)
|
||||||
|
return nil
|
||||||
|
|
||||||
|
}
|
Reference in New Issue
Block a user