nquest/pkg/controller/file.go

51 lines
886 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)
func (u *File) UploadFile(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)
}