initial commit
This commit is contained in:
commit
457ea86831
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
*.pdf
|
||||
*.cbz
|
11
go.mod
Normal file
11
go.mod
Normal file
@ -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
|
||||
)
|
10
go.sum
Normal file
10
go.sum
Normal file
@ -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=
|
94
main.go
Normal file
94
main.go
Normal file
@ -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)
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user