mirror of
https://github.com/gen2brain/cbconvert
synced 2026-06-30 09:11:54 +02:00
Add Combine option, issue #21
This commit is contained in:
+16
-32
@@ -26,13 +26,6 @@ import (
|
||||
|
||||
// convertDocument converts PDF/EPUB document to CBZ.
|
||||
func (c *Converter) convertDocument(ctx context.Context, fileName string) error {
|
||||
var err error
|
||||
|
||||
c.Workdir, err = os.MkdirTemp(os.TempDir(), "cbc")
|
||||
if err != nil {
|
||||
return fmt.Errorf("convertDocument: %w", err)
|
||||
}
|
||||
|
||||
doc, err := fitz.New(fileName)
|
||||
if err != nil {
|
||||
return fmt.Errorf("convertDocument: %w", err)
|
||||
@@ -77,13 +70,6 @@ func (c *Converter) convertDocument(ctx context.Context, fileName string) error
|
||||
|
||||
// convertArchive converts archive to CBZ.
|
||||
func (c *Converter) convertArchive(ctx context.Context, fileName string) error {
|
||||
var err error
|
||||
|
||||
c.Workdir, err = os.MkdirTemp(os.TempDir(), "cbc")
|
||||
if err != nil {
|
||||
return fmt.Errorf("convertArchive: %w", err)
|
||||
}
|
||||
|
||||
contents, err := c.archiveList(fileName)
|
||||
if err != nil {
|
||||
return fmt.Errorf("convertArchive: %w", err)
|
||||
@@ -137,7 +123,7 @@ func (c *Converter) convertArchive(ctx context.Context, fileName string) error {
|
||||
|
||||
if isImage(pathName) {
|
||||
if c.Opts.NoConvert {
|
||||
if err = copyFile(bytes.NewReader(data), filepath.Join(c.Workdir, filepath.Base(pathName))); err != nil {
|
||||
if err = copyFile(bytes.NewReader(data), c.workPath(filepath.Base(pathName))); err != nil {
|
||||
return fmt.Errorf("convertArchive: %w", err)
|
||||
}
|
||||
|
||||
@@ -145,7 +131,7 @@ func (c *Converter) convertArchive(ctx context.Context, fileName string) error {
|
||||
}
|
||||
|
||||
if cover == pathName && c.Opts.NoCover {
|
||||
if err = copyFile(bytes.NewReader(data), filepath.Join(c.Workdir, filepath.Base(pathName))); err != nil {
|
||||
if err = copyFile(bytes.NewReader(data), c.workPath(filepath.Base(pathName))); err != nil {
|
||||
return fmt.Errorf("convertArchive: %w", err)
|
||||
}
|
||||
|
||||
@@ -159,7 +145,7 @@ func (c *Converter) convertArchive(ctx context.Context, fileName string) error {
|
||||
}
|
||||
|
||||
if c.Opts.NoRGB && !isGrayScale(img) {
|
||||
if err = copyFile(bytes.NewReader(data), filepath.Join(c.Workdir, filepath.Base(pathName))); err != nil {
|
||||
if err = copyFile(bytes.NewReader(data), c.workPath(filepath.Base(pathName))); err != nil {
|
||||
return fmt.Errorf("convertArchive: %w", err)
|
||||
}
|
||||
|
||||
@@ -176,8 +162,8 @@ func (c *Converter) convertArchive(ctx context.Context, fileName string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
if !c.Opts.NoNonImage {
|
||||
if err = copyFile(bytes.NewReader(data), filepath.Join(c.Workdir, filepath.Base(pathName))); err != nil {
|
||||
if c.prefix == "" && !c.Opts.NoNonImage {
|
||||
if err = copyFile(bytes.NewReader(data), c.workPath(filepath.Base(pathName))); err != nil {
|
||||
return fmt.Errorf("convertArchive: %w", err)
|
||||
}
|
||||
}
|
||||
@@ -199,13 +185,6 @@ func (c *Converter) convertArchive(ctx context.Context, fileName string) error {
|
||||
|
||||
// convertDirectory converts directory to CBZ.
|
||||
func (c *Converter) convertDirectory(ctx context.Context, dirPath string) error {
|
||||
var err error
|
||||
|
||||
c.Workdir, err = os.MkdirTemp(os.TempDir(), "cbc")
|
||||
if err != nil {
|
||||
return fmt.Errorf("convertDirectory: %w", err)
|
||||
}
|
||||
|
||||
contents, err := imagesFromPath(dirPath)
|
||||
if err != nil {
|
||||
return fmt.Errorf("convertDirectory: %w", err)
|
||||
@@ -232,8 +211,8 @@ func (c *Converter) convertDirectory(ctx context.Context, dirPath string) error
|
||||
return fmt.Errorf("convertDirectory: %w", err)
|
||||
}
|
||||
|
||||
if isNonImage(img) && !c.Opts.NoNonImage {
|
||||
if err = copyFile(file, filepath.Join(c.Workdir, filepath.Base(img))); err != nil {
|
||||
if isNonImage(img) && c.prefix == "" && !c.Opts.NoNonImage {
|
||||
if err = copyFile(file, c.workPath(filepath.Base(img))); err != nil {
|
||||
return fmt.Errorf("convertDirectory: %w", err)
|
||||
}
|
||||
|
||||
@@ -244,7 +223,7 @@ func (c *Converter) convertDirectory(ctx context.Context, dirPath string) error
|
||||
continue
|
||||
} else if isImage(img) {
|
||||
if c.Opts.NoConvert {
|
||||
if err = copyFile(file, filepath.Join(c.Workdir, filepath.Base(img))); err != nil {
|
||||
if err = copyFile(file, c.workPath(filepath.Base(img))); err != nil {
|
||||
return fmt.Errorf("convertDirectory: %w", err)
|
||||
}
|
||||
|
||||
@@ -262,7 +241,7 @@ func (c *Converter) convertDirectory(ctx context.Context, dirPath string) error
|
||||
}
|
||||
|
||||
if c.Opts.NoRGB && !isGrayScale(i) {
|
||||
if err = copyFile(file, filepath.Join(c.Workdir, filepath.Base(img))); err != nil {
|
||||
if err = copyFile(file, c.workPath(filepath.Base(img))); err != nil {
|
||||
return fmt.Errorf("convertDirectory: %w", err)
|
||||
}
|
||||
|
||||
@@ -293,6 +272,11 @@ func (c *Converter) convertDirectory(ctx context.Context, dirPath string) error
|
||||
return nil
|
||||
}
|
||||
|
||||
// workPath returns the path of name inside the workdir, with the combine prefix applied.
|
||||
func (c *Converter) workPath(name string) string {
|
||||
return filepath.Join(c.Workdir, c.prefix+name)
|
||||
}
|
||||
|
||||
// imageConvert converts image.Image.
|
||||
func (c *Converter) imageConvert(ctx context.Context, img image.Image, index int, pathName string) error {
|
||||
err := ctx.Err()
|
||||
@@ -312,9 +296,9 @@ func (c *Converter) imageConvert(ctx context.Context, img image.Image, index int
|
||||
|
||||
var fileName string
|
||||
if pathName != "" {
|
||||
fileName = filepath.Join(c.Workdir, fmt.Sprintf("%s.%s", baseNoExt(pathName), ext))
|
||||
fileName = c.workPath(fmt.Sprintf("%s.%s", baseNoExt(pathName), ext))
|
||||
} else {
|
||||
fileName = filepath.Join(c.Workdir, fmt.Sprintf("%03d.%s", index, ext))
|
||||
fileName = c.workPath(fmt.Sprintf("%03d.%s", index, ext))
|
||||
}
|
||||
|
||||
img = c.imageTransform(img)
|
||||
|
||||
Reference in New Issue
Block a user