nquest/pkg/controller/file.go

51 lines
891 B
Go
Raw Normal View History

2024-01-20 21:37:49 +03:00
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)
2024-01-28 22:19:41 +03:00
func (u *File) AdminUploadFile(c echo.Context) error {
2024-01-20 21:37:49 +03:00
// 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)
}