oras.land/oras-go/v2@v2.5.1-0.20240520045656-aef90e4d04c4/internal/descriptor/descriptor.go (about)

     1  /*
     2  Copyright The ORAS Authors.
     3  Licensed under the Apache License, Version 2.0 (the "License");
     4  you may not use this file except in compliance with the License.
     5  You may obtain a copy of the License at
     6  
     7  http://www.apache.org/licenses/LICENSE-2.0
     8  
     9  Unless required by applicable law or agreed to in writing, software
    10  distributed under the License is distributed on an "AS IS" BASIS,
    11  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  See the License for the specific language governing permissions and
    13  limitations under the License.
    14  */
    15  
    16  package descriptor
    17  
    18  import (
    19  	"github.com/opencontainers/go-digest"
    20  	ocispec "github.com/opencontainers/image-spec/specs-go/v1"
    21  	"oras.land/oras-go/v2/internal/docker"
    22  	"oras.land/oras-go/v2/internal/spec"
    23  )
    24  
    25  // DefaultMediaType is the media type used when no media type is specified.
    26  const DefaultMediaType string = "application/octet-stream"
    27  
    28  // Descriptor contains the minimun information to describe the disposition of
    29  // targeted content.
    30  // Since it only has strings and integers, Descriptor is a comparable struct.
    31  type Descriptor struct {
    32  	// MediaType is the media type of the object this schema refers to.
    33  	MediaType string `json:"mediaType,omitempty"`
    34  
    35  	// Digest is the digest of the targeted content.
    36  	Digest digest.Digest `json:"digest"`
    37  
    38  	// Size specifies the size in bytes of the blob.
    39  	Size int64 `json:"size"`
    40  }
    41  
    42  // Empty is an empty descriptor
    43  var Empty Descriptor
    44  
    45  // FromOCI shrinks the OCI descriptor to the minimum.
    46  func FromOCI(desc ocispec.Descriptor) Descriptor {
    47  	return Descriptor{
    48  		MediaType: desc.MediaType,
    49  		Digest:    desc.Digest,
    50  		Size:      desc.Size,
    51  	}
    52  }
    53  
    54  // IsForeignLayer checks if a descriptor describes a foreign layer.
    55  func IsForeignLayer(desc ocispec.Descriptor) bool {
    56  	switch desc.MediaType {
    57  	case ocispec.MediaTypeImageLayerNonDistributable,
    58  		ocispec.MediaTypeImageLayerNonDistributableGzip,
    59  		ocispec.MediaTypeImageLayerNonDistributableZstd,
    60  		docker.MediaTypeForeignLayer:
    61  		return true
    62  	default:
    63  		return false
    64  	}
    65  }
    66  
    67  // IsManifest checks if a descriptor describes a manifest.
    68  func IsManifest(desc ocispec.Descriptor) bool {
    69  	switch desc.MediaType {
    70  	case docker.MediaTypeManifest,
    71  		docker.MediaTypeManifestList,
    72  		ocispec.MediaTypeImageManifest,
    73  		ocispec.MediaTypeImageIndex,
    74  		spec.MediaTypeArtifactManifest:
    75  		return true
    76  	default:
    77  		return false
    78  	}
    79  }
    80  
    81  // Plain returns a plain descriptor that contains only MediaType, Digest and
    82  // Size.
    83  func Plain(desc ocispec.Descriptor) ocispec.Descriptor {
    84  	return ocispec.Descriptor{
    85  		MediaType: desc.MediaType,
    86  		Digest:    desc.Digest,
    87  		Size:      desc.Size,
    88  	}
    89  }