|
5 | 5 | app_errors "gpt-load/internal/errors" |
6 | 6 | "gpt-load/internal/models" |
7 | 7 | "gpt-load/internal/response" |
| 8 | + "io" |
8 | 9 | "log" |
| 10 | + "path/filepath" |
9 | 11 | "strconv" |
10 | 12 | "strings" |
11 | 13 | "time" |
@@ -107,24 +109,79 @@ func (s *Server) AddMultipleKeys(c *gin.Context) { |
107 | 109 | response.Success(c, result) |
108 | 110 | } |
109 | 111 |
|
110 | | -// AddMultipleKeysAsync handles creating new keys from a text block within a specific group. |
| 112 | +// AddMultipleKeysAsync handles creating new keys from a text block or file within a specific group. |
111 | 113 | func (s *Server) AddMultipleKeysAsync(c *gin.Context) { |
112 | | - var req KeyTextRequest |
113 | | - if err := c.ShouldBindJSON(&req); err != nil { |
114 | | - response.Error(c, app_errors.NewAPIError(app_errors.ErrInvalidJSON, err.Error())) |
115 | | - return |
| 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 | + } |
| 171 | + groupID = req.GroupID |
| 172 | + keysText = req.KeysText |
116 | 173 | } |
117 | 174 |
|
118 | | - group, ok := s.findGroupByID(c, req.GroupID) |
| 175 | + group, ok := s.findGroupByID(c, groupID) |
119 | 176 | if !ok { |
120 | 177 | return |
121 | 178 | } |
122 | 179 |
|
123 | | - if !validateKeysText(c, req.KeysText) { |
| 180 | + if !validateKeysText(c, keysText) { |
124 | 181 | return |
125 | 182 | } |
126 | 183 |
|
127 | | - taskStatus, err := s.KeyImportService.StartImportTask(group, req.KeysText) |
| 184 | + taskStatus, err := s.KeyImportService.StartImportTask(group, keysText) |
128 | 185 | if err != nil { |
129 | 186 | response.Error(c, app_errors.NewAPIError(app_errors.ErrTaskInProgress, err.Error())) |
130 | 187 | return |
|
0 commit comments