Skip to content

Commit

Permalink
perf: use comment of the zip to know if it's converted instead of txt…
Browse files Browse the repository at this point in the history
… file
  • Loading branch information
Belphemur committed Aug 29, 2024
1 parent dbef43d commit 5357ece
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 17 deletions.
14 changes: 3 additions & 11 deletions cbz/cbz_creator.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ func WriteChapterToCBZ(chapter *manga.Chapter, outputFilePath string) error {

// Create a new ZIP writer
zipWriter := zip.NewWriter(zipFile)
err = zipWriter.SetComment("Created by CBZOptimizer")
if err != nil {
return err
}
Expand Down Expand Up @@ -71,18 +70,11 @@ func WriteChapterToCBZ(chapter *manga.Chapter, outputFilePath string) error {
}

if chapter.IsConverted {
convertedWriter, err := zipWriter.CreateHeader(&zip.FileHeader{
Name: "Converted.txt",
Method: zip.Deflate,
Modified: time.Now(),
})
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)))
convertedString := fmt.Sprintf("%s\nThis chapter has been converted by CBZOptimizer.", chapter.ConvertedTime)
err = zipWriter.SetComment(convertedString)
if err != nil {
return fmt.Errorf("failed to write Converted.txt contents: %w", err)
return fmt.Errorf("failed to write comment: %w", err)
}
}

Expand Down
19 changes: 14 additions & 5 deletions cbz/cbz_creator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,22 @@ package cbz
import (
"archive/zip"
"bytes"
"fmt"
"github.com/belphemur/CBZOptimizer/manga"
"os"
"testing"
"time"
)

func TestWriteChapterToCBZ(t *testing.T) {
currentTime := time.Now()

// Define test cases
testCases := []struct {
name string
chapter *manga.Chapter
expectedFiles []string
name string
chapter *manga.Chapter
expectedFiles []string
expectedComment string
}{
//test case where there is only one page and ComicInfo and the chapter is converted
{
Expand All @@ -29,9 +33,10 @@ func TestWriteChapterToCBZ(t *testing.T) {
},
ComicInfoXml: "<Series>Boundless Necromancer</Series>",
IsConverted: true,
ConvertedTime: time.Now(),
ConvertedTime: currentTime,
},
expectedFiles: []string{"0000.jpg", "ComicInfo.xml", "Converted.txt"},
expectedFiles: []string{"0000.jpg", "ComicInfo.xml"},
expectedComment: fmt.Sprintf("%s\nThis chapter has been converted by CBZOptimizer.", currentTime),
},
//test case where there is only one page and no
{
Expand Down Expand Up @@ -125,6 +130,10 @@ func TestWriteChapterToCBZ(t *testing.T) {
}
}

if tc.expectedComment != "" && r.Comment != tc.expectedComment {
t.Errorf("Expected comment %s, but found %s", tc.expectedComment, r.Comment)
}

// Check if there are no unexpected files
if len(filesInArchive) != len(tc.expectedFiles) {
t.Errorf("Expected %d files, but found %d", len(tc.expectedFiles), len(filesInArchive))
Expand Down
13 changes: 12 additions & 1 deletion cbz/cbz_loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,17 @@ func LoadChapter(filePath string) (*manga.Chapter, error) {
chapter := &manga.Chapter{
FilePath: filePath,
}
// Check for comment
if r.Comment != "" {
scanner := bufio.NewScanner(strings.NewReader(r.Comment))
if scanner.Scan() {
convertedTime := scanner.Text()
chapter.ConvertedTime, err = dateparse.ParseAny(convertedTime)
if err == nil {
chapter.IsConverted = true
}
}
}

for _, f := range r.File {
if f.FileInfo().IsDir() {
Expand All @@ -45,7 +56,7 @@ func LoadChapter(filePath string) (*manga.Chapter, error) {
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" {
} else if !chapter.IsConverted && ext == ".txt" && strings.ToLower(filepath.Base(f.Name)) == "converted.txt" {
textContent, err := io.ReadAll(rc)
if err != nil {
rc.Close()
Expand Down

0 comments on commit 5357ece

Please sign in to comment.