aides-repo-api/internal/controllers/taskcontroller/upload.go
Maxim Slipenko 733cebfd81 feat: add database (#7)
Reviewed-on: https://code.alt-gnome.ru/aides-infra/aides-repo-api/pulls/7
Co-authored-by: Maxim Slipenko <no-reply@maxim.slipenko.com>
Co-committed-by: Maxim Slipenko <no-reply@maxim.slipenko.com>
2024-12-14 08:35:45 +00:00

75 lines
1.7 KiB
Go

package taskcontroller
import (
"fmt"
"net/http"
"code.alt-gnome.ru/aides-infra/aides-repo-api/internal/common/errors"
"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"`
StatusText string `json:"status"`
}
func (rd *TaskUploadResponse) Render(w http.ResponseWriter, r *http.Request) error {
return nil
}
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
}
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,
Files: files,
})
if err != nil {
fmt.Println(err)
render.Render(w, r, &errors.ErrResponse{
HTTPStatusCode: http.StatusInternalServerError,
StatusText: "Internal Server Error",
Err: err,
})
return
}
response := TaskUploadResponse{
TaskID: taskID,
StatusText: "Success!",
}
if err := render.Render(w, r, &response); err != nil {
render.Render(w, r, errors.ErrRender(err))
return
}
}