AddMultipleKeysAsync handles creating new keys from a text block or file within a specific group.
(c *gin.Context)
| 111 | |
| 112 | // AddMultipleKeysAsync handles creating new keys from a text block or file within a specific group. |
| 113 | func (s *Server) AddMultipleKeysAsync(c *gin.Context) { |
| 114 | var groupID uint |
| 115 | var keysText string |
| 116 | |
| 117 | // Check content type to determine if it's a file upload or JSON request |
| 118 | contentType := c.ContentType() |
| 119 | |
| 120 | if strings.Contains(contentType, "multipart/form-data") { |
| 121 | // Handle file upload |
| 122 | groupIDStr := c.PostForm("group_id") |
| 123 | if groupIDStr == "" { |
| 124 | response.ErrorI18nFromAPIError(c, app_errors.ErrBadRequest, "validation.group_id_required") |
| 125 | return |
| 126 | } |
| 127 | |
| 128 | groupIDInt, err := strconv.Atoi(groupIDStr) |
| 129 | if err != nil || groupIDInt <= 0 { |
| 130 | response.ErrorI18nFromAPIError(c, app_errors.ErrBadRequest, "validation.invalid_group_id_format") |
| 131 | return |
| 132 | } |
| 133 | groupID = uint(groupIDInt) |
| 134 | |
| 135 | // Get uploaded file |
| 136 | file, err := c.FormFile("file") |
| 137 | if err != nil { |
| 138 | response.ErrorI18nFromAPIError(c, app_errors.ErrBadRequest, "validation.file_required") |
| 139 | return |
| 140 | } |
| 141 | |
| 142 | // Validate file extension |
| 143 | ext := strings.ToLower(filepath.Ext(file.Filename)) |
| 144 | if ext != ".txt" { |
| 145 | response.ErrorI18nFromAPIError(c, app_errors.ErrValidation, "validation.only_txt_supported") |
| 146 | return |
| 147 | } |
| 148 | |
| 149 | // Read file content |
| 150 | fileContent, err := file.Open() |
| 151 | if err != nil { |
| 152 | response.ErrorI18nFromAPIError(c, app_errors.ErrBadRequest, "validation.failed_to_open_file") |
| 153 | return |
| 154 | } |
| 155 | defer fileContent.Close() |
| 156 | |
| 157 | // Read file content as string using io.ReadAll |
| 158 | buf, err := io.ReadAll(fileContent) |
| 159 | if err != nil { |
| 160 | response.ErrorI18nFromAPIError(c, app_errors.ErrBadRequest, "validation.failed_to_read_file") |
| 161 | return |
| 162 | } |
| 163 | keysText = string(buf) |
| 164 | } else { |
| 165 | // Handle JSON request (original behavior) |
| 166 | var req KeyTextRequest |
| 167 | if err := c.ShouldBindJSON(&req); err != nil { |
| 168 | response.Error(c, app_errors.NewAPIError(app_errors.ErrInvalidJSON, err.Error())) |
| 169 | return |
| 170 | } |
nothing calls this directly
no test coverage detected