package files import ( "context" "donat-widget/internal/model" "donat-widget/pkg/validator" "github.com/labstack/echo/v4" "log/slog" "net/http" ) // AddNewFile godoc // @Summary Add new File // @Description Add new File // @Tags Files // @Accept json // @Produce json // @Security BearerAuth // @Param new_file formData file false "New file" // @Success 200 {string} string "GetWidgetDb has been uploaded successfully!" // @Failure 400 {object} echo.HTTPError "Bad request" // @Failure 401 {object} echo.HTTPError "Unauthorized or expired token" // @Failure 422 {object} echo.HTTPError "Validation error" // @Router /files [post] func AddNewFile(fileService model.FileService) echo.HandlerFunc { return func(request echo.Context) error { ctx := context.Background() var body model.CreateWidgetBody err := validator.ParseAndValidate(&body, request) authData, err := fileService.CheckToken(request) if err != nil { slog.Error(err.Error()) return echo.NewHTTPError(http.StatusUnauthorized, err.Error()) } newFile, err := request.FormFile("new_file") if err != nil { slog.Error(err.Error()) return echo.NewHTTPError(http.StatusBadRequest, "Can't upload file") } _, err = fileService.AddNewFile( ctx, *newFile, authData.AccountID, "widget", ) if err != nil { slog.Error(err.Error()) return request.JSON(http.StatusInternalServerError, err.Error()) } return request.JSON(http.StatusOK, "") } } // GetFile godoc // @Summary Get a file // @Description Retrieve a file by its ID // @Tags Files // @Accept json // @Produce octet-stream // @Param file_id path string true "File ID" format:"uuid" // @Success 200 {file} binary "File content" // @Failure 400 {object} echo.HTTPError "Bad request" // @Failure 401 {object} echo.HTTPError "Unauthorized or expired token" // @Failure 404 {object} echo.HTTPError "File not found" // @Router /files/{file_id} [get] func GetFile(fileService model.FileService) echo.HandlerFunc { return func(request echo.Context) error { ctx := context.Background() fileID := request.Param("file_id") file, fileType, err := fileService.GetByID(ctx, fileID) if err != nil { slog.Error(err.Error()) return echo.NewHTTPError(http.StatusNotFound, "File not found") } return request.Blob(http.StatusOK, fileType, file) } } // GetWidgetFiles godoc // @Summary Get all widget files // @Description Retrieve all widget files, filtered by type if specified // @Tags Files // @Accept json // @Produce json // @Security BearerAuth // @Param type query string false "File type (audio or image)" Enums(audio, image) // @Success 200 {array} model.DataFile "List of files" // @Failure 400 {object} echo.HTTPError "Bad request" // @Failure 401 {object} echo.HTTPError "Unauthorized or expired token" // @Router /files/widgets [get] func GetWidgetFiles(fileService model.FileService) echo.HandlerFunc { return func(request echo.Context) error { ctx := context.Background() fileType := request.QueryParam("type") if fileType != "" && fileType != "audio" && fileType != "image" { return echo.NewHTTPError(http.StatusUnprocessableEntity, "Invalid file type") } authData, err := fileService.CheckToken(request) if err != nil { slog.Error(err.Error()) return echo.NewHTTPError(http.StatusUnauthorized, err.Error()) } widgetFiles, err := fileService.WidgetsFiles( ctx, fileType, authData.AccountID, ) if err != nil { slog.Error(err.Error()) return echo.NewHTTPError(http.StatusInternalServerError, "Failed to retrieve files") } return request.JSON(http.StatusOK, widgetFiles) } }