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

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)