package taskcontroller import ( "net/http" "code.alt-gnome.ru/aides-infra/aides-repo-api/internal/common/errors" "code.alt-gnome.ru/aides-infra/aides-repo-api/internal/config" "code.alt-gnome.ru/aides-infra/aides-repo-api/internal/services/taskservice" "github.com/go-chi/chi/v5" "github.com/go-chi/render" ) type TaskUploadResponse struct { TaskID string `json:"taskID"` Repo string `json:"repo"` StatusText string `json:"status"` } func (rd *TaskUploadResponse) Render(w http.ResponseWriter, r *http.Request) error { render.Status(r, http.StatusOK) return nil } type TaskController struct { config *config.Config taskService *taskservice.Service } func New(cfg *config.Config, taskService *taskservice.Service) *TaskController { return &TaskController{ config: cfg, taskService: taskService, } } func (c *TaskController) Upload(w http.ResponseWriter, r *http.Request) { taskID := chi.URLParam(r, "taskID") if taskID == "" { render.Render(w, r, &errors.ErrResponse{ HTTPStatusCode: http.StatusBadRequest, StatusText: "taskID is required", }) return } err := r.ParseMultipartForm(10240 << 20) if err != nil { render.Render(w, r, &errors.ErrResponse{ HTTPStatusCode: http.StatusBadRequest, StatusText: "Bad Request", }) return } repo := r.FormValue("repo") if repo == "" { render.Render(w, r, &errors.ErrResponse{ HTTPStatusCode: http.StatusBadRequest, StatusText: "Missing required repo field", }) return } files := r.MultipartForm.File["files"] for _, fileHeader := range files { if fileHeader.Size > (1024 << 20) { // Limit each file size to 10MB render.Render(w, r, &errors.ErrResponse{ HTTPStatusCode: http.StatusBadRequest, StatusText: "File too large", }) return } } err = c.taskService.Upload(&taskservice.TaskUploadInput{ TaskID: taskID, Repo: repo, Files: files, }) if err != nil { render.Render(w, r, &errors.ErrResponse{ HTTPStatusCode: http.StatusInternalServerError, StatusText: "Internal Server Error", Err: err, }) } response := TaskUploadResponse{ TaskID: taskID, Repo: repo, StatusText: "Success!", } if err := render.Render(w, r, &response); err != nil { render.Render(w, r, errors.ErrRender(err)) return } }