85 lines
1.6 KiB
Go
85 lines
1.6 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"os"
|
|
|
|
"github.com/h3poteto/pongo2echo"
|
|
"github.com/labstack/echo/v4"
|
|
"github.com/labstack/echo/v4/middleware"
|
|
)
|
|
|
|
type Upload struct {
|
|
Url string `json:"url" xml:"url"`
|
|
ContentType string `json:"ContentType" xml:"ContentType"`
|
|
}
|
|
|
|
func main() {
|
|
// Echo instance
|
|
echo := echo.New()
|
|
|
|
// Render Engine
|
|
render := pongo2echo.NewRenderer()
|
|
render.AddDirectory("server/templates")
|
|
|
|
echo.Renderer = render
|
|
|
|
// Middleware
|
|
echo.Use(middleware.Logger())
|
|
echo.Use(middleware.Recover())
|
|
|
|
echo.Static("/static", "static")
|
|
|
|
// Routes
|
|
echo.GET("/", index)
|
|
echo.POST("/upload/:responseType", upload)
|
|
|
|
// Start server
|
|
echo.Logger.Fatal(echo.Start(":3000"))
|
|
}
|
|
|
|
// Handlers
|
|
func index(ctx echo.Context) error {
|
|
return ctx.Render(http.StatusOK, "index.html", map[string]interface{}{})
|
|
}
|
|
|
|
func upload(ctx echo.Context) error {
|
|
|
|
// File
|
|
file, err := ctx.FormFile("file")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
src, err := file.Open()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer src.Close()
|
|
|
|
// Destination
|
|
dst, err := os.Create(fmt.Sprintf("files/%s", file.Filename))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer dst.Close()
|
|
|
|
// Copy
|
|
if _, err = io.Copy(dst, src); err != nil {
|
|
return err
|
|
}
|
|
|
|
switch responseType := ctx.Param("responseType"); responseType {
|
|
case "json":
|
|
json := &Upload{
|
|
Url: fmt.Sprintf("https://cdn.oki.cx/files/%s", file.Filename),
|
|
ContentType: "deprecated?",
|
|
}
|
|
return ctx.JSON(http.StatusOK, json)
|
|
case "html":
|
|
return ctx.Render(http.StatusOK, "htmlResponse.html", map[string]interface{}{"filename": file.Filename})
|
|
}
|
|
return nil
|
|
}
|