59 lines
1.1 KiB
Go
59 lines
1.1 KiB
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, quest uuid.UUID) 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),
|
|
quest,
|
|
fo,
|
|
)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return c.JSON(200, &api.UploadResponse{
|
|
Uuid: id,
|
|
})
|
|
}
|
|
|
|
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)
|
|
}
|