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

@@ -10,7 +10,8 @@
"installDockerBuildx": true,
"version": "latest",
"dockerDashComposeVersion": "latest"
}
},
"ghcr.io/georgofenbeck/features/lazygit-linuxbinary:1": {}
},
// Use 'forwardPorts' to make a list of ports inside the container available locally.
// "forwardPorts": [],

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
}

13
main.go
View File

@@ -113,6 +113,9 @@ func handleFeed(dir string) http.HandlerFunc {
kepubFile := filepath.Join(dir, strings.Replace(parseFileName(resp), ".epub", ".kepub.epub", 1))
kepubConverter.Convert(epubFile, kepubFile)
if err != nil {
handleError(r, w, "Failed to convert to kepub", err)
}
outFile, _ := os.Open(kepubFile)
defer outFile.Close()
@@ -120,7 +123,7 @@ func handleFeed(dir string) http.HandlerFunc {
outInfo, _ := outFile.Stat()
w.Header().Set("Content-Length", fmt.Sprintf("%d", outInfo.Size()))
w.Header().Set("Content-Disposition", mime.FormatMediaType("attachment", map[string]string{"filename": strings.Replace(parseFileName(resp), ".epub", ".kepub.epub", 1)}))
w.Header().Set("Content-Disposition", mime.FormatMediaType("attachment", map[string]string{"filename": filepath.Base(kepubFile)}))
w.Header().Set("Content-Type", convert.EPUB_MIME)
io.Copy(w, outFile)
@@ -136,7 +139,11 @@ func handleFeed(dir string) http.HandlerFunc {
downloadFile(epubFile, resp)
mobiFile := filepath.Join(dir, strings.Replace(parseFileName(resp), ".epub", ".mobi", 1))
kepubConverter.Convert(epubFile, mobiFile)
err := mobiConverter.Convert(epubFile, mobiFile)
if err != nil {
handleError(r, w, "Failed to convert to mobi", err)
return
}
outFile, _ := os.Open(mobiFile)
defer outFile.Close()
@@ -144,7 +151,7 @@ func handleFeed(dir string) http.HandlerFunc {
outInfo, _ := outFile.Stat()
w.Header().Set("Content-Length", fmt.Sprintf("%d", outInfo.Size()))
w.Header().Set("Content-Disposition", mime.FormatMediaType("attachment", map[string]string{"filename": strings.Replace(parseFileName(resp), ".epub", ".kepub.epub", 1)}))
w.Header().Set("Content-Disposition", mime.FormatMediaType("attachment", map[string]string{"filename": filepath.Base(mobiFile)}))
w.Header().Set("Content-Type", convert.MOBI_MIME)
io.Copy(w, outFile)