refactor: dry conversion code

This commit is contained in:
Evan Buss
2024-07-12 19:36:08 +00:00
parent b683d0c9b6
commit f4408abeae
4 changed files with 116 additions and 98 deletions

View File

@@ -1,14 +1,9 @@
package convert
const (
MOBI_MIME = "application/x-mobipocket-ebook"
EPUB_MIME = "application/epub+zip"
)
type Converter interface {
// Whether or not the converter is available
// Usually based on the availability of the underlying tool
Available() bool
// Convert the input file to the output file
Convert(input string, output string) error
Convert(input string) (string, error)
}

View File

@@ -1,29 +1,40 @@
package convert
import (
"fmt"
"os/exec"
"path/filepath"
"strings"
"sync"
)
type KepubConverter struct {
mutex sync.Mutex
mutex sync.Mutex
available bool
availableOnce sync.Once
}
func (kc *KepubConverter) Available() bool {
path, err := exec.LookPath("kepubify")
if err != nil {
return false
}
return path != ""
kc.availableOnce.Do(func() {
fmt.Println("TEST")
path, err := exec.LookPath("kepubify")
kc.available = err == nil && path != ""
})
return kc.available
}
func (kc *KepubConverter) Convert(input string, output string) error {
func (kc *KepubConverter) Convert(input string) (string, error) {
kc.mutex.Lock()
defer kc.mutex.Unlock()
cmd := exec.Command("kepubify", "-v", "-u", "-o", output, input)
dir := filepath.Dir(input)
kepubFile := filepath.Join(dir, strings.Replace(filepath.Base(input), ".epub", ".kepub.epub", 1))
cmd := exec.Command("kepubify", "-v", "-u", "-o", kepubFile, input)
if err := cmd.Run(); err != nil {
return err
return "", err
}
return nil
return kepubFile, nil
}

View File

@@ -5,34 +5,38 @@ import (
"fmt"
"os/exec"
"path/filepath"
"strings"
"sync"
)
type MobiConverter struct {
mutex sync.Mutex
mutex sync.Mutex
available bool
availableOnce sync.Once
}
func (mc *MobiConverter) Available() bool {
path, err := exec.LookPath("kindlegen")
if err != nil {
return false
}
return path != ""
mc.availableOnce.Do(func() {
path, err := exec.LookPath("kindlegen")
mc.available = err == nil && path != ""
})
return mc.available
}
func (mc *MobiConverter) Convert(input string, output string) error {
func (mc *MobiConverter) Convert(input string) (string, error) {
mc.mutex.Lock()
defer mc.mutex.Unlock()
// KindleGen doesn't allow the input file to be in a different directory
// So set the working directory to the input file.
outDir, _ := filepath.Abs(filepath.Dir(input))
mobiFile := filepath.Join(outDir, strings.Replace(filepath.Base(input), ".epub", ".mobi", 1))
// And remove the directory from file paths
cmd := exec.Command("kindlegen",
filepath.Base(input),
"-dont_append_source", "-c1", "-o",
filepath.Base(output),
filepath.Base(mobiFile),
)
cmd.Dir = outDir
@@ -45,13 +49,13 @@ func (mc *MobiConverter) Convert(input string, output string) error {
if exiterr, ok := err.(*exec.ExitError); ok {
if exiterr.ExitCode() != 1 {
fmt.Println(fmt.Sprint(err) + ": " + out.String() + ":" + stderr.String())
return err
return "", err
}
} else {
fmt.Println(fmt.Sprint(err) + ": " + out.String() + ":" + stderr.String())
return err
return "", err
}
}
return nil
return mobiFile, nil
}