From c6a8caf3174b50931924b9ff17d30e055fa6d74d Mon Sep 17 00:00:00 2001 From: Antoine Aflalo <197810+Belphemur@users.noreply.github.com> Date: Tue, 27 Aug 2024 13:37:53 -0400 Subject: [PATCH] refactor: change the optimize command --- ...ert_cbz_command.go => optimize_command.go} | 38 ++------------- ...mmand_test.go => optimize_command_test.go} | 0 manga/optimize.go | 47 +++++++++++++++++++ 3 files changed, 50 insertions(+), 35 deletions(-) rename cmd/{convert_cbz_command.go => optimize_command.go} (77%) rename cmd/{convert_cbz_command_test.go => optimize_command_test.go} (100%) create mode 100644 manga/optimize.go diff --git a/cmd/convert_cbz_command.go b/cmd/optimize_command.go similarity index 77% rename from cmd/convert_cbz_command.go rename to cmd/optimize_command.go index d9f2818..e443f9f 100644 --- a/cmd/convert_cbz_command.go +++ b/cmd/optimize_command.go @@ -2,9 +2,9 @@ package cmd import ( "fmt" - "github.com/belphemur/CBZOptimizer/cbz" "github.com/belphemur/CBZOptimizer/converter" "github.com/belphemur/CBZOptimizer/converter/constant" + "github.com/belphemur/CBZOptimizer/manga" "github.com/spf13/cobra" "github.com/thediveo/enumflag/v2" "os" @@ -95,42 +95,10 @@ func ConvertCbzCommand(cmd *cobra.Command, args []string) error { go func() { defer wg.Done() for path := range fileChan { - fmt.Printf("Processing file: %s\n", path) - - // Load the chapter - chapter, err := cbz.LoadChapter(path) + err := manga.Optimize(chapterConverter, path, quality, override) if err != nil { - errorChan <- fmt.Errorf("failed to load chapter: %v", err) - continue + errorChan <- fmt.Errorf("error processing file %s: %w", path, err) } - - 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) } }() } diff --git a/cmd/convert_cbz_command_test.go b/cmd/optimize_command_test.go similarity index 100% rename from cmd/convert_cbz_command_test.go rename to cmd/optimize_command_test.go diff --git a/manga/optimize.go b/manga/optimize.go new file mode 100644 index 0000000..43b303d --- /dev/null +++ b/manga/optimize.go @@ -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 + +}