forked from Ivasoft/opds-proxy
feat: mobi conversion
Will automatically convert .epub to .mobi on Kindle
This commit is contained in:
@@ -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": [],
|
||||
|
||||
@@ -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
13
main.go
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user