59 lines
941 B
Go
59 lines
941 B
Go
package main
|
|
|
|
import (
|
|
"astra-file-storage/config"
|
|
uuid2 "github.com/google/uuid"
|
|
"io"
|
|
"net/http"
|
|
"os"
|
|
|
|
"github.com/labstack/echo/v4"
|
|
"github.com/labstack/echo/v4/middleware"
|
|
)
|
|
|
|
type FileSuccess struct {
|
|
FileName string `json:"fileName"`
|
|
}
|
|
|
|
func upload(c echo.Context) error {
|
|
file, err := c.FormFile("file")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
src, err := file.Open()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer src.Close()
|
|
|
|
uuid := uuid2.New().String()
|
|
|
|
fileName := uuid + "-" + file.Filename
|
|
dst, err := os.Create("public/" + fileName)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer dst.Close()
|
|
|
|
// Copy
|
|
if _, err = io.Copy(dst, src); err != nil {
|
|
return err
|
|
}
|
|
|
|
return c.JSON(http.StatusOK, FileSuccess{
|
|
FileName: fileName,
|
|
})
|
|
}
|
|
|
|
func main() {
|
|
e := echo.New()
|
|
|
|
e.Use(middleware.Logger())
|
|
e.Use(middleware.Recover())
|
|
|
|
e.Static("/", "public")
|
|
e.POST("/upload", upload)
|
|
|
|
e.Logger.Fatal(e.Start(":" + config.Env.Server.Port))
|
|
}
|