50 lines
891 B
Go
50 lines
891 B
Go
package controller
|
|
|
|
import (
|
|
"github.com/google/uuid"
|
|
"github.com/labstack/echo/v4"
|
|
"gitrepo.ru/neonxp/nquest/api"
|
|
"gitrepo.ru/neonxp/nquest/pkg/service"
|
|
)
|
|
|
|
type File struct {
|
|
FileService *service.File
|
|
}
|
|
|
|
// (POST /file/upload)
|
|
func (u *File) AdminUploadFile(c echo.Context) error {
|
|
// user := contextlib.GetUser(c)
|
|
fh, err := c.FormFile("file")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
fo, err := fh.Open()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
id, err := u.FileService.Upload(
|
|
c.Request().Context(),
|
|
fh.Filename,
|
|
fh.Header.Get("Content-Type"),
|
|
int(fh.Size),
|
|
fo,
|
|
)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return c.JSON(200, &api.UploadResponse{
|
|
Uuid: id,
|
|
})
|
|
}
|
|
|
|
// (GET /file/{uid})
|
|
func (u *File) GetFile(c echo.Context, uid uuid.UUID) error {
|
|
f, err := u.FileService.GetFile(c.Request().Context(), uid)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return c.Blob(200, f.ContentType, f.Body)
|
|
}
|