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-04-29 23:01:24 +03:00
|
|
|
func (u *File) AdminUploadFile(c echo.Context, quest uuid.UUID) 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),
|
2024-04-29 23:01:24 +03:00
|
|
|
quest,
|
2024-01-20 21:37:49 +03:00
|
|
|
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 {
|
2024-04-29 23:01:24 +03:00
|
|
|
f, rdr, err := u.FileService.GetFile(c.Request().Context(), uid)
|
2024-01-20 21:37:49 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-04-29 23:01:24 +03:00
|
|
|
return c.Stream(200, f.ContentType, rdr)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *File) AdminListFiles(c echo.Context, quest uuid.UUID) error {
|
|
|
|
fl, err := u.FileService.GetFilesByQuest(c.Request().Context(), quest)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
list := make([]api.FileItem, 0, len(fl))
|
|
|
|
for _, f := range fl {
|
|
|
|
list = append(list, api.FileItem{
|
|
|
|
Id: f.ID,
|
|
|
|
OriginalName: f.Filename,
|
|
|
|
Size: f.Size,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.JSON(200, list)
|
2024-01-20 21:37:49 +03:00
|
|
|
}
|