UploadTaskDocument @Title UploadTaskDocument @Tag Task API @Description upload document for a task and parse its text @Param id query string true "The id (owner/name) of the task" @Param file formData string true "The base64 encoded file data" @Param type formData string true "The file type/extensio
()
| 36 | // @Success 200 {object} controllers.Response The Response object |
| 37 | // @router /upload-task-document [post] |
| 38 | func (c *ApiController) UploadTaskDocument() { |
| 39 | userName, ok := c.RequireSignedIn() |
| 40 | if !ok { |
| 41 | return |
| 42 | } |
| 43 | |
| 44 | taskId := c.Input().Get("id") |
| 45 | fileBase64 := c.GetString("file") |
| 46 | fileType := c.GetString("type") |
| 47 | fileName := c.GetString("name") |
| 48 | |
| 49 | if taskId == "" || fileBase64 == "" || fileType == "" || fileName == "" { |
| 50 | c.ResponseError(c.T("application:Missing required parameters")) |
| 51 | return |
| 52 | } |
| 53 | |
| 54 | // Get the task to verify ownership |
| 55 | task, err := object.GetTask(taskId) |
| 56 | if err != nil { |
| 57 | c.ResponseError(err.Error()) |
| 58 | return |
| 59 | } |
| 60 | if task == nil { |
| 61 | c.ResponseError(c.T("general:The task does not exist")) |
| 62 | return |
| 63 | } |
| 64 | |
| 65 | // Check ownership for non-admins |
| 66 | if !c.IsAdmin() { |
| 67 | if task.Owner != userName { |
| 68 | c.ResponseError(c.T("auth:Unauthorized operation")) |
| 69 | return |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | // Validate file extension - only .docx and .pdf allowed |
| 74 | ext := strings.ToLower(filepath.Ext(fileName)) |
| 75 | allowedExtensions := []string{".docx", ".pdf"} |
| 76 | isValid := false |
| 77 | for _, allowed := range allowedExtensions { |
| 78 | if ext == allowed { |
| 79 | isValid = true |
| 80 | break |
| 81 | } |
| 82 | } |
| 83 | if !isValid { |
| 84 | c.ResponseError(c.T("resource:Only docx and pdf files are allowed")) |
| 85 | return |
| 86 | } |
| 87 | |
| 88 | // Decode base64 file data |
| 89 | index := strings.Index(fileBase64, ",") |
| 90 | if index == -1 { |
| 91 | c.ResponseError(c.T("resource:Invalid file data format")) |
| 92 | return |
| 93 | } |
| 94 | |
| 95 | fileBytes, err := base64.StdEncoding.DecodeString(fileBase64[index+1:]) |
nothing calls this directly
no test coverage detected