feat: mobi conversion

Will automatically convert .epub to .mobi on Kindle
This commit is contained in:
Evan Buss
2024-07-11 19:47:55 +00:00
parent c00f6a9750
commit 1f2183f141
3 changed files with 44 additions and 4 deletions

View File

@@ -1,7 +1,10 @@
package convert
import (
"bytes"
"fmt"
"os/exec"
"path/filepath"
"sync"
)
@@ -21,5 +24,34 @@ func (mc *MobiConverter) Convert(input string, output 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))
// And remove the directory from file paths
cmd := exec.Command("kindlegen",
filepath.Base(input),
"-dont_append_source", "-c1", "-o",
filepath.Base(output),
)
cmd.Dir = outDir
var out bytes.Buffer
var stderr bytes.Buffer
cmd.Stdout = &out
cmd.Stderr = &stderr
err := cmd.Run()
if err != nil {
if exiterr, ok := err.(*exec.ExitError); ok {
if exiterr.ExitCode() != 1 {
fmt.Println(fmt.Sprint(err) + ": " + out.String() + ":" + stderr.String())
return err
}
} else {
fmt.Println(fmt.Sprint(err) + ": " + out.String() + ":" + stderr.String())
return err
}
}
return nil
}