mirror of
https://github.com/Belphemur/CBZOptimizer.git
synced 2026-07-21 19:05:39 +02:00
refactor!: disk-first zero-copy architecture with file-to-file cwebp conversion
BREAKING CHANGE: Complete refactoring of the conversion pipeline. - Replace in-memory Page/PageContainer with disk-based PageFile struct - Extract archives to temp directory instead of loading into memory - Use cwebp InputFile/OutputFile for zero-copy file-to-file conversion - Use cwebp -crop for splitting (no Go-side image decode needed) - Check if already converted before extracting (fast pre-check) - Remove oliamb/cutter dependency (replaced by cwebp -crop) - Remove page_container.go (no longer needed) - Add comprehensive tests with improved coverage Co-authored-by: Belphemur <197810+Belphemur@users.noreply.github.com>
This commit is contained in:
co-authored by
Belphemur
parent
e87e73eb28
commit
164dc577b9
+25
-45
@@ -12,6 +12,8 @@ import (
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
// WriteChapterToCBZ creates a CBZ file from a Chapter by streaming page files
|
||||
// from disk directly into the zip archive. No image data is held in memory.
|
||||
func WriteChapterToCBZ(chapter *manga.Chapter, outputFilePath string) error {
|
||||
log.Debug().
|
||||
Str("chapter_file", chapter.FilePath).
|
||||
@@ -20,8 +22,7 @@ func WriteChapterToCBZ(chapter *manga.Chapter, outputFilePath string) error {
|
||||
Bool("is_converted", chapter.IsConverted).
|
||||
Msg("Starting CBZ file creation")
|
||||
|
||||
// Create a new ZIP file
|
||||
log.Debug().Str("output_path", outputFilePath).Msg("Creating output CBZ file")
|
||||
// Create output file
|
||||
zipFile, err := os.Create(outputFilePath)
|
||||
if err != nil {
|
||||
log.Error().Str("output_path", outputFilePath).Err(err).Msg("Failed to create CBZ file")
|
||||
@@ -29,109 +30,88 @@ func WriteChapterToCBZ(chapter *manga.Chapter, outputFilePath string) error {
|
||||
}
|
||||
defer errs.Capture(&err, zipFile.Close, "failed to close .cbz file")
|
||||
|
||||
// Create a new ZIP writer
|
||||
log.Debug().Str("output_path", outputFilePath).Msg("Creating ZIP writer")
|
||||
// Create ZIP writer
|
||||
zipWriter := zip.NewWriter(zipFile)
|
||||
if err != nil {
|
||||
log.Error().Str("output_path", outputFilePath).Err(err).Msg("Failed to create ZIP writer")
|
||||
return err
|
||||
}
|
||||
defer errs.Capture(&err, zipWriter.Close, "failed to close .cbz writer")
|
||||
|
||||
// Write each page to the ZIP archive
|
||||
log.Debug().Str("output_path", outputFilePath).Int("pages_to_write", len(chapter.Pages)).Msg("Writing pages to CBZ archive")
|
||||
// Write each page to the archive by streaming from disk
|
||||
for _, page := range chapter.Pages {
|
||||
// Construct the file name for the page
|
||||
var fileName string
|
||||
if page.IsSplitted {
|
||||
// Use the format page%03d-%02d for split pages
|
||||
fileName = fmt.Sprintf("%04d-%02d%s", page.Index, page.SplitPartIndex, page.Extension)
|
||||
} else {
|
||||
// Use the format page%03d for non-split pages
|
||||
fileName = fmt.Sprintf("%04d%s", page.Index, page.Extension)
|
||||
}
|
||||
|
||||
log.Debug().
|
||||
Str("output_path", outputFilePath).
|
||||
Uint16("page_index", page.Index).
|
||||
Bool("is_splitted", page.IsSplitted).
|
||||
Uint16("split_part", page.SplitPartIndex).
|
||||
Str("filename", fileName).
|
||||
Uint64("size", page.Size).
|
||||
Str("source", page.FilePath).
|
||||
Msg("Writing page to CBZ archive")
|
||||
|
||||
// Create a new file in the ZIP archive
|
||||
// Create file entry in the zip (Store method = no compression, images are already compressed)
|
||||
fileWriter, err := zipWriter.CreateHeader(&zip.FileHeader{
|
||||
Name: fileName,
|
||||
Method: zip.Store,
|
||||
Modified: time.Now(),
|
||||
})
|
||||
if err != nil {
|
||||
log.Error().Str("output_path", outputFilePath).Str("filename", fileName).Err(err).Msg("Failed to create file in CBZ archive")
|
||||
log.Error().Str("filename", fileName).Err(err).Msg("Failed to create file in CBZ archive")
|
||||
return fmt.Errorf("failed to create file in .cbz: %w", err)
|
||||
}
|
||||
|
||||
// Stream the page contents into the archive. This transparently
|
||||
// handles pages staged on disk (see manga.Page.TempFilePath) so we
|
||||
// never need to hold the whole chapter's contents in memory at once.
|
||||
pageReader, err := page.Open()
|
||||
// Stream the page file from disk into the archive
|
||||
pageFile, err := os.Open(page.FilePath)
|
||||
if err != nil {
|
||||
log.Error().Str("output_path", outputFilePath).Str("filename", fileName).Err(err).Msg("Failed to open page contents")
|
||||
return fmt.Errorf("failed to open page contents: %w", err)
|
||||
log.Error().Str("filename", fileName).Str("source", page.FilePath).Err(err).Msg("Failed to open page file")
|
||||
return fmt.Errorf("failed to open page file: %w", err)
|
||||
}
|
||||
|
||||
bytesWritten, err := io.Copy(fileWriter, pageReader)
|
||||
closeErr := pageReader.Close()
|
||||
bytesWritten, err := io.Copy(fileWriter, pageFile)
|
||||
closeErr := pageFile.Close()
|
||||
if err != nil {
|
||||
log.Error().Str("output_path", outputFilePath).Str("filename", fileName).Err(err).Msg("Failed to write page contents")
|
||||
log.Error().Str("filename", fileName).Err(err).Msg("Failed to write page contents")
|
||||
return fmt.Errorf("failed to write page contents: %w", err)
|
||||
}
|
||||
if closeErr != nil {
|
||||
log.Error().Str("output_path", outputFilePath).Str("filename", fileName).Err(closeErr).Msg("Failed to close page contents reader")
|
||||
return fmt.Errorf("failed to close page contents reader: %w", closeErr)
|
||||
log.Error().Str("filename", fileName).Err(closeErr).Msg("Failed to close page file")
|
||||
return fmt.Errorf("failed to close page file: %w", closeErr)
|
||||
}
|
||||
|
||||
log.Debug().
|
||||
Str("output_path", outputFilePath).
|
||||
Str("filename", fileName).
|
||||
Int64("bytes_written", bytesWritten).
|
||||
Msg("Page written successfully")
|
||||
}
|
||||
|
||||
// Optionally, write the ComicInfo.xml file if present
|
||||
// Write ComicInfo.xml if present
|
||||
if chapter.ComicInfoXml != "" {
|
||||
log.Debug().Str("output_path", outputFilePath).Int("xml_size", len(chapter.ComicInfoXml)).Msg("Writing ComicInfo.xml to CBZ archive")
|
||||
log.Debug().Str("output_path", outputFilePath).Msg("Writing ComicInfo.xml")
|
||||
comicInfoWriter, err := zipWriter.CreateHeader(&zip.FileHeader{
|
||||
Name: "ComicInfo.xml",
|
||||
Method: zip.Deflate,
|
||||
Modified: time.Now(),
|
||||
})
|
||||
if err != nil {
|
||||
log.Error().Str("output_path", outputFilePath).Err(err).Msg("Failed to create ComicInfo.xml in CBZ archive")
|
||||
return fmt.Errorf("failed to create ComicInfo.xml in .cbz: %w", err)
|
||||
}
|
||||
|
||||
bytesWritten, err := comicInfoWriter.Write([]byte(chapter.ComicInfoXml))
|
||||
_, err = comicInfoWriter.Write([]byte(chapter.ComicInfoXml))
|
||||
if err != nil {
|
||||
log.Error().Str("output_path", outputFilePath).Err(err).Msg("Failed to write ComicInfo.xml contents")
|
||||
return fmt.Errorf("failed to write ComicInfo.xml contents: %w", err)
|
||||
return fmt.Errorf("failed to write ComicInfo.xml: %w", err)
|
||||
}
|
||||
log.Debug().Str("output_path", outputFilePath).Int("bytes_written", bytesWritten).Msg("ComicInfo.xml written successfully")
|
||||
} else {
|
||||
log.Debug().Str("output_path", outputFilePath).Msg("No ComicInfo.xml to write")
|
||||
}
|
||||
|
||||
// Set zip comment for converted chapters
|
||||
if chapter.IsConverted {
|
||||
convertedString := fmt.Sprintf("%s\nThis chapter has been converted by CBZOptimizer.", chapter.ConvertedTime)
|
||||
log.Debug().Str("output_path", outputFilePath).Str("comment", convertedString).Msg("Setting CBZ comment for converted chapter")
|
||||
err = zipWriter.SetComment(convertedString)
|
||||
comment := fmt.Sprintf("%s\nThis chapter has been converted by CBZOptimizer.", chapter.ConvertedTime)
|
||||
err = zipWriter.SetComment(comment)
|
||||
if err != nil {
|
||||
log.Error().Str("output_path", outputFilePath).Err(err).Msg("Failed to write CBZ comment")
|
||||
return fmt.Errorf("failed to write comment: %w", err)
|
||||
}
|
||||
log.Debug().Str("output_path", outputFilePath).Msg("CBZ comment set successfully")
|
||||
}
|
||||
|
||||
log.Debug().Str("output_path", outputFilePath).Msg("CBZ file creation completed successfully")
|
||||
log.Debug().Str("output_path", outputFilePath).Msg("CBZ file creation completed")
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user