package pp import ( "crypto/sha256" "encoding/hex" "encoding/json" "fmt" "io" "sort" ) type OverLimitReport struct { Added int } func MergeOverLimitManifest(snapshot Snapshot, reader io.Reader) (Snapshot, OverLimitReport, error) { var manifest manifestDocument if err := json.NewDecoder(reader).Decode(&manifest); err != nil { return Snapshot{}, OverLimitReport{}, fmt.Errorf("decode download manifest: %w", err) } result := snapshot result.Items = append([]Item(nil), snapshot.Items...) seen := make(map[string]struct{}, len(result.Items)) for _, item := range result.Items { seen[item.SourceURLHash] = struct{}{} } report := OverLimitReport{} for _, artifact := range manifest.Results { if artifact.Status != string(StatusOverLimit) { continue } if artifact.URL == "" || len(artifact.SourceIDs) == 0 || artifact.Bytes <= LegacyMaxBytes { return Snapshot{}, OverLimitReport{}, fmt.Errorf("invalid over-limit manifest record") } digest := sha256.Sum256([]byte(artifact.URL)) sourceHash := hex.EncodeToString(digest[:]) if _, exists := seen[sourceHash]; exists { continue } result.Items = append(result.Items, Item{ KBID: snapshot.KBID, SourceURLHash: sourceHash, SourceIDs: append([]string(nil), artifact.SourceIDs...), Bytes: artifact.Bytes, Status: StatusOverLimit, }) seen[sourceHash] = struct{}{} report.Added++ } sort.Slice(result.Items, func(i, j int) bool { return result.Items[i].SourceURLHash < result.Items[j].SourceURLHash }) return result, report, nil }