2024-04-10 22:10:09 +08:00
|
|
|
package model
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2024-12-23 00:07:44 -08:00
|
|
|
"io"
|
|
|
|
|
2024-04-15 18:02:54 +08:00
|
|
|
v1 "github.com/google/go-containerregistry/pkg/v1"
|
|
|
|
"github.com/samber/lo"
|
2024-12-23 00:07:44 -08:00
|
|
|
|
2024-04-10 22:10:09 +08:00
|
|
|
"nf-repo/internal/model/types"
|
|
|
|
)
|
|
|
|
|
2024-04-15 18:02:54 +08:00
|
|
|
type RepoSimpleManifestLayer struct {
|
|
|
|
MediaType string `json:"mediaType"`
|
|
|
|
Size int `json:"size"`
|
|
|
|
Digest string `json:"digest"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type RepoSimpleManifestBlob struct {
|
|
|
|
SchemaVersion int `json:"schemaVersion"`
|
|
|
|
MediaType string `json:"mediaType"`
|
|
|
|
Config struct {
|
|
|
|
MediaType string `json:"mediaType"`
|
|
|
|
Size int `json:"size"`
|
|
|
|
Digest string `json:"digest"`
|
|
|
|
} `json:"config"`
|
|
|
|
Layers []RepoSimpleManifestLayer `json:"layers"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *RepoSimpleManifestBlob) CountSize() int {
|
|
|
|
return b.Config.Size + lo.Sum(lo.Map(b.Layers, func(item RepoSimpleManifestLayer, _ int) int {
|
|
|
|
return item.Size
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
|
|
|
func ManifestCountSize(m *v1.Manifest) int {
|
|
|
|
return int(m.Config.Size) + lo.Sum(lo.Map(m.Layers, func(item v1.Descriptor, _ int) int {
|
|
|
|
return int(item.Size)
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
|
|
|
type RepoSimpleManifest struct {
|
2024-04-10 22:10:09 +08:00
|
|
|
Blob []byte `json:"blob"`
|
|
|
|
ContentType string `json:"content_type"`
|
|
|
|
}
|
|
|
|
|
2024-04-15 18:02:54 +08:00
|
|
|
type Manifest struct {
|
|
|
|
SchemaVersion int64 `json:"schemaVersion"`
|
|
|
|
MediaType types.MediaType `json:"mediaType,omitempty"`
|
|
|
|
Config Descriptor `json:"config"`
|
|
|
|
Layers []Descriptor `json:"layers"`
|
|
|
|
Annotations map[string]string `json:"annotations,omitempty"`
|
|
|
|
Subject *Descriptor `json:"subject,omitempty"`
|
|
|
|
}
|
|
|
|
|
2024-04-10 22:10:09 +08:00
|
|
|
type Descriptor struct {
|
|
|
|
MediaType types.MediaType `json:"mediaType"`
|
|
|
|
Size int64 `json:"size"`
|
|
|
|
Digest Hash `json:"digest"`
|
|
|
|
Data []byte `json:"data,omitempty"`
|
|
|
|
URLs []string `json:"urls,omitempty"`
|
|
|
|
Annotations map[string]string `json:"annotations,omitempty"`
|
|
|
|
Platform *Platform `json:"platform,omitempty"`
|
|
|
|
ArtifactType string `json:"artifactType,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type IndexManifest struct {
|
|
|
|
SchemaVersion int64 `json:"schemaVersion"`
|
|
|
|
MediaType types.MediaType `json:"mediaType,omitempty"`
|
|
|
|
Manifests []Descriptor `json:"manifests"`
|
|
|
|
Annotations map[string]string `json:"annotations,omitempty"`
|
|
|
|
Subject *Descriptor `json:"subject,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func ParseIndexManifest(r io.Reader) (*IndexManifest, error) {
|
|
|
|
im := IndexManifest{}
|
|
|
|
if err := json.NewDecoder(r).Decode(&im); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &im, nil
|
|
|
|
}
|
2024-04-15 18:02:54 +08:00
|
|
|
|
|
|
|
type PackageManifest struct {
|
|
|
|
Id uint64 `json:"id" gorm:"primaryKey;column:id"`
|
|
|
|
CreatedAt int64 `json:"created_at" gorm:"column:created_at;autoCreateTime:milli"`
|
|
|
|
UpdatedAt int64 `json:"updated_at" gorm:"column:updated_at;autoUpdateTime:milli"`
|
|
|
|
|
|
|
|
DigestId uint64 `json:"digest_id" gorm:"column:digest_id"`
|
|
|
|
Digest string `json:"digest" gorm:"-"`
|
|
|
|
Size int `json:"size" gorm:"-"`
|
|
|
|
Repo string `json:"repo" gorm:"uniqueIndex:repo_tag_idx;column:repo"`
|
|
|
|
Tag string `json:"tag" gorm:"uniqueIndex:repo_tag_idx;column:tag"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type PackageDigest struct {
|
|
|
|
Id uint64 `json:"id" gorm:"primaryKey;column:id"`
|
|
|
|
CreatedAt int64 `json:"created_at" gorm:"column:created_at;autoCreateTime:milli"`
|
|
|
|
UpdatedAt int64 `json:"updated_at" gorm:"column:updated_at;autoUpdateTime:milli"`
|
|
|
|
|
|
|
|
Digest string `json:"digest" gorm:"uniqueIndex;column:digest"`
|
|
|
|
Size int `json:"size" gorm:"column:size;default:0"`
|
|
|
|
ContentType string `json:"content_type" gorm:"column:content_type"`
|
|
|
|
Content []byte `json:"content" gorm:"column:content;type:bytes"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func ParseManifest(r io.Reader) (*Manifest, error) {
|
|
|
|
m := Manifest{}
|
|
|
|
if err := json.NewDecoder(r).Decode(&m); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &m, nil
|
|
|
|
}
|