Files
genpdf/gosrc/main.go
pandeptwidyaop d77e676692
Some checks failed
Main CI/CD / Build and push (push) Has been cancelled
update error message
2024-08-15 22:01:34 +08:00

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
}