From 457ea86831a3f8fbc6b8869e60606710b06e8a81 Mon Sep 17 00:00:00 2001 From: milarin Date: Fri, 7 Apr 2023 14:33:39 +0200 Subject: [PATCH] initial commit --- .gitignore | 2 ++ go.mod | 11 +++++++ go.sum | 10 ++++++ main.go | 94 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 117 insertions(+) create mode 100644 .gitignore create mode 100644 go.mod create mode 100644 go.sum create mode 100644 main.go diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9f63718 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +*.pdf +*.cbz diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..b3d365f --- /dev/null +++ b/go.mod @@ -0,0 +1,11 @@ +module git.milar.in/milarin/cbz2pdf + +go 1.20 + +require github.com/fatih/color v1.15.0 + +require ( + github.com/mattn/go-colorable v0.1.13 // indirect + github.com/mattn/go-isatty v0.0.17 // indirect + golang.org/x/sys v0.6.0 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..2624c9d --- /dev/null +++ b/go.sum @@ -0,0 +1,10 @@ +github.com/fatih/color v1.15.0 h1:kOqh6YHBtK8aywxGerMG2Eq3H6Qgoqeo13Bk2Mv/nBs= +github.com/fatih/color v1.15.0/go.mod h1:0h5ZqXfHYED7Bhv2ZJamyIOUej9KtShiJESRwBDUSsw= +github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= +github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg= +github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= +github.com/mattn/go-isatty v0.0.17 h1:BTarxUcIeDqL27Mc+vyvdWYSL28zpIhv3RoTdsLMPng= +github.com/mattn/go-isatty v0.0.17/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= +golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.6.0 h1:MVltZSvRTcU2ljQOhs94SXPftV6DCNnZViHeQps87pQ= +golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= diff --git a/main.go b/main.go new file mode 100644 index 0000000..b56d0a8 --- /dev/null +++ b/main.go @@ -0,0 +1,94 @@ +package main + +import ( + "archive/zip" + "flag" + "fmt" + "io" + "os" + "os/exec" + "path/filepath" + + "github.com/fatih/color" +) + +var ( //flags + FlagOutputFile = flag.String("o", "output.pdf", "output file name") +) +var ( // colors + ColorRed = color.New(color.FgRed) +) + +func main() { + flag.Parse() + + if _, err := exec.LookPath("img2pdf"); err != nil { + ColorRed.Fprintln(os.Stderr, "img2pdf not installed") + os.Exit(1) + } + + inputFilePath := flag.Arg(0) + if inputFilePath == "" { + ColorRed.Fprintln(os.Stderr, "please provide an input file path") + os.Exit(1) + } + + inputFile, err := os.Open(inputFilePath) + if err != nil { + ColorRed.Fprintf(os.Stderr, "input file path could not be read: %s\n", err.Error()) + os.Exit(1) + } + defer inputFile.Close() + + fi, err := inputFile.Stat() + if err != nil { + ColorRed.Fprintf(os.Stderr, "input file path could not be read: %s\n", err.Error()) + os.Exit(1) + } + + zipr, err := zip.NewReader(inputFile, fi.Size()) + if err != nil { + ColorRed.Fprintf(os.Stderr, "input file path could not be read: %s\n", err.Error()) + os.Exit(1) + } + + tempFiles := make([]string, 0, len(zipr.File)) + for _, file := range zipr.File { + ext := filepath.Ext(file.Name) + tempFile, err := os.CreateTemp("", fmt.Sprintf("*%s", ext)) + if err != nil { + ColorRed.Fprintf(os.Stderr, "temporary file could not be created: %s\n", err.Error()) + os.Exit(1) + } + defer os.Remove(tempFile.Name()) + defer tempFile.Close() + tempFiles = append(tempFiles, tempFile.Name()) + + imgFile, err := file.Open() + if err != nil { + ColorRed.Fprintf(os.Stderr, "compressed file could not be read: %s\n", err.Error()) + os.Exit(1) + } + defer imgFile.Close() + + if _, err := io.Copy(tempFile, imgFile); err != nil { + ColorRed.Fprintf(os.Stderr, "coud not create image file: %s\n", err.Error()) + os.Exit(1) + } + + tempFile.Close() + } + + args := append([]string{"--output", *FlagOutputFile}, tempFiles...) + img2pdf := exec.Command("img2pdf", args...) + + if err := img2pdf.Start(); err != nil { + ColorRed.Fprintf(os.Stderr, "img2pdf could not be started: %s\n", err.Error()) + os.Exit(1) + } + + if err := img2pdf.Wait(); err != nil { + ColorRed.Fprintf(os.Stderr, "img2pdf returned an error: %s\n", err.Error()) + os.Exit(1) + } +}