2024-12-14 08:35:45 +00:00
|
|
|
package taskcontroller
|
|
|
|
|
|
|
|
import (
|
2024-12-26 13:42:54 +00:00
|
|
|
"errors"
|
2024-12-14 08:35:45 +00:00
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/go-chi/chi/v5"
|
|
|
|
"github.com/go-chi/render"
|
2024-12-15 06:36:33 +00:00
|
|
|
|
2025-01-07 14:54:16 +00:00
|
|
|
commonErrors "code.aides.space/aides-infra/aides-repo-api/internal/common/errors"
|
2024-12-26 13:42:54 +00:00
|
|
|
|
2025-01-07 14:54:16 +00:00
|
|
|
"code.aides.space/aides-infra/aides-repo-api/internal/logger"
|
|
|
|
"code.aides.space/aides-infra/aides-repo-api/internal/services/taskservice"
|
2024-12-14 08:35:45 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type TaskUploadResponse struct {
|
|
|
|
TaskID string `json:"taskID"`
|
|
|
|
StatusText string `json:"status"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rd *TaskUploadResponse) Render(w http.ResponseWriter, r *http.Request) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-12-15 08:39:18 +00:00
|
|
|
// Upload handles file uploads for a specific task.
|
|
|
|
//
|
|
|
|
// @Summary Upload files to a task
|
|
|
|
// @Description Upload multiple files associated with a specific task ID. Each file must be less than 10MB.
|
2024-12-17 16:28:37 +00:00
|
|
|
// @Tags tasks
|
2024-12-26 13:42:54 +00:00
|
|
|
// @Security ApiKeyAuth
|
2024-12-15 08:39:18 +00:00
|
|
|
// @Accept multipart/form-data
|
|
|
|
// @Produce json
|
|
|
|
// @Param taskID path string true "Task ID"
|
|
|
|
// @Param files formData file true "Files to upload"
|
|
|
|
// @Success 200 {object} TaskUploadResponse "Successful file upload"
|
|
|
|
// @Failure 400 {object} errors.ErrResponse "Bad Request or File too large"
|
|
|
|
// @Failure 500 {object} errors.ErrResponse "Internal Server Error"
|
|
|
|
// @Router /tasks/{taskID}/upload [post]
|
2024-12-14 08:35:45 +00:00
|
|
|
func (c *TaskController) Upload(w http.ResponseWriter, r *http.Request) {
|
|
|
|
taskID := chi.URLParam(r, "taskID")
|
|
|
|
if taskID == "" {
|
2024-12-26 13:42:54 +00:00
|
|
|
render.Render(w, r, &commonErrors.ErrResponse{
|
2024-12-14 08:35:45 +00:00
|
|
|
HTTPStatusCode: http.StatusBadRequest,
|
|
|
|
StatusText: "taskID is required",
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2025-01-08 12:30:48 +00:00
|
|
|
err := r.ParseMultipartForm(10 << 20)
|
2024-12-14 08:35:45 +00:00
|
|
|
if err != nil {
|
2024-12-26 13:42:54 +00:00
|
|
|
render.Render(w, r, &commonErrors.ErrResponse{
|
2024-12-14 08:35:45 +00:00
|
|
|
HTTPStatusCode: http.StatusBadRequest,
|
|
|
|
StatusText: "Bad Request",
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
files := r.MultipartForm.File["files"]
|
|
|
|
for _, fileHeader := range files {
|
2025-01-08 12:30:48 +00:00
|
|
|
if fileHeader.Size > (4096 << 20) {
|
2024-12-26 13:42:54 +00:00
|
|
|
render.Render(w, r, &commonErrors.ErrResponse{
|
2024-12-14 08:35:45 +00:00
|
|
|
HTTPStatusCode: http.StatusBadRequest,
|
|
|
|
StatusText: "File too large",
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
err = c.taskService.Upload(&taskservice.TaskUploadInput{
|
|
|
|
TaskID: taskID,
|
|
|
|
Files: files,
|
|
|
|
})
|
|
|
|
if err != nil {
|
2024-12-15 08:39:18 +00:00
|
|
|
log := logger.GetLogger()
|
|
|
|
log.Error("Error while upload task", map[string]interface{}{
|
|
|
|
"err": err,
|
|
|
|
})
|
2024-12-26 13:42:54 +00:00
|
|
|
|
|
|
|
var e *taskservice.TooOldError
|
|
|
|
if errors.As(err, &e) {
|
|
|
|
render.Render(w, r, &commonErrors.ErrResponse{
|
|
|
|
HTTPStatusCode: http.StatusBadRequest,
|
|
|
|
StatusText: err.Error(),
|
|
|
|
Err: err,
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
render.Render(w, r, &commonErrors.ErrResponse{
|
2024-12-14 08:35:45 +00:00
|
|
|
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 {
|
2024-12-26 13:42:54 +00:00
|
|
|
render.Render(w, r, commonErrors.ErrRender(err))
|
2024-12-14 08:35:45 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|