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, ) 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 int true "File ID" // @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) } }