Skip to content

Commit

Permalink
oci: Resolve manifests by digest like done for blobs.
Browse files Browse the repository at this point in the history
Signed-off-by: Francis Laniel <flaniel@linux.microsoft.com>
  • Loading branch information
eiffel-fl committed Jan 8, 2024
1 parent 732d87d commit 1c01677
Showing 1 changed file with 34 additions and 1 deletion.
35 changes: 34 additions & 1 deletion content/oci/readonlyoci.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"oras.land/oras-go/v2/internal/fs/tarfs"
"oras.land/oras-go/v2/internal/graph"
"oras.land/oras-go/v2/internal/resolver"
"oras.land/oras-go/v2/internal/spec"
)

// ReadOnlyStore implements `oras.ReadonlyTarget`, and represents a read-only
Expand Down Expand Up @@ -186,6 +187,38 @@ func loadIndex(ctx context.Context, index *ocispec.Index, fetcher content.Fetche
return nil
}

// getMediaType returns the media type for the given path.
// By default, descriptor.DefaultMediaType is returned.
func getMediaType(fsys fs.FS, path string) string {
file, err := fsys.Open(path)
if err != nil {
return descriptor.DefaultMediaType
}

content, err := io.ReadAll(file)
if err != nil {
return descriptor.DefaultMediaType
}

manifest := &ocispec.Manifest{}
err = json.Unmarshal(content, manifest)
if err == nil {
return ocispec.MediaTypeImageManifest
}

// WARNING This code may never be called as an artifact can be unmarshalled
// to a manifest.
// This leads to test failures like:
// readonlyoci_test.go:350: ReadOnlyStore.Resolve() = {application/vnd.oci.image.manifest.v1+json sha256:063351176d525cd595e4741dbf2cbed7eb449534c7afc6ad9ae926da4ef24660 354 [] map[] [] <nil> }, want {application/vnd.oci.artifact.manifest.v1+json sha256:063351176d525cd595e4741dbf2cbed7eb449534c7afc6ad9ae926da4ef24660 354 [] map[] [] <nil> }
artifact := &spec.Artifact{}
err = json.Unmarshal(content, artifact)
if err == nil {
return spec.MediaTypeArtifactManifest
}

return descriptor.DefaultMediaType
}

// resolveBlob returns a descriptor describing the blob identified by dgst.
func resolveBlob(fsys fs.FS, dgst string) (ocispec.Descriptor, error) {
path, err := blobPath(digest.Digest(dgst))
Expand All @@ -204,7 +237,7 @@ func resolveBlob(fsys fs.FS, dgst string) (ocispec.Descriptor, error) {
}

return ocispec.Descriptor{
MediaType: descriptor.DefaultMediaType,
MediaType: getMediaType(fsys, path),
Size: fi.Size(),
Digest: digest.Digest(dgst),
}, nil
Expand Down

0 comments on commit 1c01677

Please sign in to comment.