57 lines
1.2 KiB
Go
57 lines
1.2 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
"os"
|
|
"os/exec"
|
|
"path"
|
|
"strings"
|
|
|
|
"github.com/gofiber/fiber/v2"
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
var rootPath string
|
|
|
|
func main() {
|
|
rootPath = os.Getenv("APP_ROOT_PATH")
|
|
if rootPath == "" {
|
|
rootPath = "/app"
|
|
}
|
|
|
|
app := fiber.New()
|
|
|
|
app.Post("ebook", handleEbook)
|
|
|
|
log.Fatal(app.Listen(":80"))
|
|
}
|
|
|
|
func handleEbook(c *fiber.Ctx) error {
|
|
|
|
f, err := c.FormFile("file")
|
|
if err != nil {
|
|
return c.Status(fiber.StatusBadRequest).SendString(fmt.Sprintf("error receiving file: %s", err))
|
|
}
|
|
|
|
sp := path.Join(rootPath, fmt.Sprintf("storage/%s.json", uuid.NewString()))
|
|
|
|
err = c.SaveFile(f, sp)
|
|
if err != nil {
|
|
return c.Status(fiber.StatusInternalServerError).SendString(fmt.Sprintf("failed to save file: %s", err))
|
|
}
|
|
|
|
cmd := exec.Command(path.Join(rootPath, "genpdf"), "generate:ebook", sp)
|
|
|
|
o, err := cmd.CombinedOutput()
|
|
if err != nil {
|
|
return c.Status(fiber.StatusInternalServerError).SendString(fmt.Sprintf("failed to convert ebook: %s", string(o)))
|
|
}
|
|
err = c.Download(strings.TrimSpace(string(o)))
|
|
if err != nil {
|
|
return c.Status(fiber.StatusInternalServerError).SendString(fmt.Sprintf("failed to sending ebook: %s", err))
|
|
}
|
|
|
|
return nil
|
|
}
|