Files
at-container-registry/pkg/hold/pds/scan.go
T

60 lines
1.7 KiB
Go

package pds
import (
"context"
"fmt"
"atcr.io/pkg/atproto"
"github.com/ipfs/go-cid"
)
// CreateScanRecord creates or updates a scan result record in the hold's PDS
// Uses a deterministic rkey based on the manifest digest, so re-scans upsert
func (p *HoldPDS) CreateScanRecord(ctx context.Context, record *atproto.ScanRecord) (string, cid.Cid, error) {
if record.Type != atproto.ScanCollection {
return "", cid.Undef, fmt.Errorf("invalid record type: %s", record.Type)
}
if record.Manifest == "" {
return "", cid.Undef, fmt.Errorf("manifest AT-URI is required")
}
// Extract the digest from the manifest AT-URI to use as rkey
manifestDigest, err := atproto.ParseManifestURI(record.Manifest)
if err != nil {
return "", cid.Undef, fmt.Errorf("invalid manifest AT-URI: %w", err)
}
rkey := atproto.ScanRecordKey(manifestDigest)
// Upsert: re-scans update the existing record
rpath, recordCID, _, err := p.repomgr.UpsertRecord(
ctx,
p.uid,
atproto.ScanCollection,
rkey,
record,
)
if err != nil {
return "", cid.Undef, fmt.Errorf("failed to upsert scan record: %w", err)
}
return rpath, recordCID, nil
}
// GetScanRecord retrieves a scan result record by manifest digest
func (p *HoldPDS) GetScanRecord(ctx context.Context, manifestDigest string) (cid.Cid, *atproto.ScanRecord, error) {
rkey := atproto.ScanRecordKey(manifestDigest)
recordCID, val, err := p.repomgr.GetRecord(ctx, p.uid, atproto.ScanCollection, rkey, cid.Undef)
if err != nil {
return cid.Undef, nil, fmt.Errorf("failed to get scan record: %w", err)
}
scanRecord, ok := val.(*atproto.ScanRecord)
if !ok {
return cid.Undef, nil, fmt.Errorf("unexpected type for scan record: %T", val)
}
return recordCID, scanRecord, nil
}