2023-12-19 13:22:13 +01:00
|
|
|
package main
|
|
|
|
|
2023-12-19 14:04:28 +01:00
|
|
|
import (
|
2023-12-20 12:26:29 +01:00
|
|
|
"embed"
|
2023-12-19 21:01:30 +01:00
|
|
|
"errors"
|
2023-12-19 19:59:53 +01:00
|
|
|
"html/template"
|
|
|
|
"io"
|
2023-12-19 21:01:30 +01:00
|
|
|
"log"
|
2023-12-19 19:59:53 +01:00
|
|
|
"net/http"
|
2023-12-19 21:01:30 +01:00
|
|
|
"os"
|
2023-12-19 14:04:28 +01:00
|
|
|
|
2023-12-19 19:59:53 +01:00
|
|
|
"github.com/labstack/echo/v4"
|
2023-12-20 12:26:29 +01:00
|
|
|
"github.com/labstack/echo/v4/middleware"
|
2023-12-19 14:04:28 +01:00
|
|
|
)
|
2023-12-19 13:22:13 +01:00
|
|
|
|
2023-12-20 12:26:29 +01:00
|
|
|
//go:embed all:public/views/*.html
|
|
|
|
var templates embed.FS
|
|
|
|
|
|
|
|
//go:embed all:static/*
|
|
|
|
var static embed.FS
|
|
|
|
|
2023-12-19 19:59:53 +01:00
|
|
|
func main() {
|
|
|
|
t := &Template{
|
2023-12-20 12:26:29 +01:00
|
|
|
templates: template.Must(template.ParseFS(templates, "public/views/*.html")),
|
2023-12-19 19:59:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
e := echo.New()
|
|
|
|
|
|
|
|
e.Renderer = t
|
2023-12-20 17:35:54 +01:00
|
|
|
e.Use(middleware.Logger())
|
2023-12-19 19:59:53 +01:00
|
|
|
|
2023-12-20 12:26:29 +01:00
|
|
|
e.Use(middleware.StaticWithConfig(middleware.StaticConfig{
|
|
|
|
Root: "static",
|
|
|
|
Browse: false,
|
|
|
|
HTML5: true,
|
|
|
|
Filesystem: http.FS(static),
|
|
|
|
}))
|
2023-12-19 20:50:26 +01:00
|
|
|
|
2023-12-19 21:01:30 +01:00
|
|
|
files := "files"
|
|
|
|
|
|
|
|
if _, err := os.Stat(files); errors.Is(err, os.ErrNotExist) {
|
|
|
|
err := os.Mkdir(files, os.ModePerm)
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
e.Static("/files", files)
|
|
|
|
|
2023-12-19 19:59:53 +01:00
|
|
|
e.GET("/", Index)
|
|
|
|
|
2023-12-19 21:41:36 +01:00
|
|
|
e.POST("/", Upload)
|
|
|
|
|
2023-12-19 21:17:59 +01:00
|
|
|
e.GET("/files/", Files)
|
|
|
|
|
2023-12-19 19:59:53 +01:00
|
|
|
e.Logger.Fatal(e.Start(":1323"))
|
|
|
|
}
|
|
|
|
|
|
|
|
type Template struct {
|
|
|
|
templates *template.Template
|
|
|
|
}
|
|
|
|
|
2023-12-20 17:36:45 +01:00
|
|
|
type IndexData struct {
|
|
|
|
Host string
|
|
|
|
UploadOnly bool
|
|
|
|
}
|
|
|
|
|
2023-12-19 19:59:53 +01:00
|
|
|
func (t *Template) Render(w io.Writer, name string, data interface{}, c echo.Context) error {
|
|
|
|
return t.templates.ExecuteTemplate(w, name, data)
|
|
|
|
}
|
|
|
|
|
|
|
|
func Index(c echo.Context) error {
|
2023-12-20 17:36:45 +01:00
|
|
|
data := IndexData{
|
|
|
|
Host: c.Request().Host,
|
|
|
|
UploadOnly: false,
|
|
|
|
}
|
|
|
|
return c.Render(http.StatusOK, "index", data)
|
2023-12-19 13:22:13 +01:00
|
|
|
}
|
2023-12-19 21:17:59 +01:00
|
|
|
|
|
|
|
func Files(c echo.Context) error {
|
|
|
|
return c.Render(http.StatusOK, "files", "test")
|
|
|
|
}
|
2023-12-19 21:41:36 +01:00
|
|
|
|
|
|
|
func Upload(c echo.Context) error {
|
|
|
|
file, err := c.FormFile("file")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-12-19 21:49:49 +01:00
|
|
|
if _, err := os.Stat("files/" + file.Filename); err == nil {
|
|
|
|
return c.String(http.StatusOK, "A file with the same name already exist's on the server.\n")
|
|
|
|
}
|
|
|
|
|
2023-12-19 21:41:36 +01:00
|
|
|
src, err := file.Open()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
dst, err := os.Create("files/" + file.Filename)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer dst.Close()
|
|
|
|
|
|
|
|
if _, err = io.Copy(dst, src); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
fileUrl := c.Request().Host + "/files/" + file.Filename + "\n"
|
|
|
|
|
|
|
|
return c.String(http.StatusOK, fileUrl)
|
|
|
|
}
|