RestoreMultipleKeys handles the business logic of restoring keys from a text block.
(groupID uint, keysText string)
| 200 | |
| 201 | // RestoreMultipleKeys handles the business logic of restoring keys from a text block. |
| 202 | func (s *KeyService) RestoreMultipleKeys(groupID uint, keysText string) (*RestoreKeysResult, error) { |
| 203 | keysToRestore := s.ParseKeysFromText(keysText) |
| 204 | if len(keysToRestore) > maxRequestKeys { |
| 205 | return nil, fmt.Errorf("batch size exceeds the limit of %d keys, got %d", maxRequestKeys, len(keysToRestore)) |
| 206 | } |
| 207 | if len(keysToRestore) == 0 { |
| 208 | return nil, fmt.Errorf("no valid keys found in the input text") |
| 209 | } |
| 210 | |
| 211 | var totalRestoredCount int64 |
| 212 | for i := 0; i < len(keysToRestore); i += chunkSize { |
| 213 | end := i + chunkSize |
| 214 | if end > len(keysToRestore) { |
| 215 | end = len(keysToRestore) |
| 216 | } |
| 217 | chunk := keysToRestore[i:end] |
| 218 | restoredCount, err := s.KeyProvider.RestoreMultipleKeys(groupID, chunk) |
| 219 | if err != nil { |
| 220 | return nil, err |
| 221 | } |
| 222 | totalRestoredCount += restoredCount |
| 223 | } |
| 224 | |
| 225 | ignoredCount := len(keysToRestore) - int(totalRestoredCount) |
| 226 | |
| 227 | var totalInGroup int64 |
| 228 | if err := s.DB.Model(&models.APIKey{}).Where("group_id = ?", groupID).Count(&totalInGroup).Error; err != nil { |
| 229 | return nil, err |
| 230 | } |
| 231 | |
| 232 | return &RestoreKeysResult{ |
| 233 | RestoredCount: int(totalRestoredCount), |
| 234 | IgnoredCount: ignoredCount, |
| 235 | TotalInGroup: totalInGroup, |
| 236 | }, nil |
| 237 | } |
| 238 | |
| 239 | // RestoreAllInvalidKeys sets the status of all 'inactive' keys in a group to 'active'. |
| 240 | func (s *KeyService) RestoreAllInvalidKeys(groupID uint) (int64, error) { |
nothing calls this directly
no test coverage detected