feat: avoid converting already converted chapters

This commit is contained in:
Antoine Aflalo
2024-08-26 23:15:14 -04:00
parent 9631979b94
commit 135db7a1f8
6 changed files with 55 additions and 2 deletions

View File

@@ -57,5 +57,17 @@ func WriteChapterToCBZ(chapter *packer.Chapter, outputFilePath string) error {
}
}
if chapter.IsConverted {
convertedWriter, err := zipWriter.Create("Converted.txt")
if err != nil {
return fmt.Errorf("failed to create Converted.txt in .cbz: %w", err)
}
_, err = convertedWriter.Write([]byte(fmt.Sprintf("%s\nThis chapter has been converted by CBZOptimizer.", chapter.ConvertedTime)))
if err != nil {
return fmt.Errorf("failed to write Converted.txt contents: %w", err)
}
}
return nil
}

View File

@@ -3,10 +3,11 @@ package cbz
import (
"CBZOptimizer/packer"
"archive/zip"
"bufio"
"bytes"
"fmt"
"github.com/araddon/dateparse"
"io"
"io/ioutil"
"path/filepath"
"strings"
)
@@ -36,12 +37,28 @@ func LoadChapter(filePath string) (*packer.Chapter, error) {
if ext == ".xml" && strings.ToLower(filepath.Base(f.Name)) == "comicinfo.xml" {
// Read the ComicInfo.xml file content
xmlContent, err := ioutil.ReadAll(rc)
xmlContent, err := io.ReadAll(rc)
if err != nil {
rc.Close()
return nil, fmt.Errorf("failed to read ComicInfo.xml content: %w", err)
}
chapter.ComicInfoXml = string(xmlContent)
} else if ext == ".txt" && strings.ToLower(filepath.Base(f.Name)) == "converted.txt" {
textContent, err := io.ReadAll(rc)
if err != nil {
rc.Close()
return nil, fmt.Errorf("failed to read Converted.xml content: %w", err)
}
scanner := bufio.NewScanner(bytes.NewReader(textContent))
if scanner.Scan() {
convertedTime := scanner.Text()
chapter.ConvertedTime, err = dateparse.ParseAny(convertedTime)
if err != nil {
rc.Close()
return nil, fmt.Errorf("failed to parse converted time: %w", err)
}
chapter.IsConverted = true
}
} else {
// Read the file contents for page
buf := new(bytes.Buffer)