|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the SwiftContainerPlugin open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2025 Apple Inc. and the SwiftContainerPlugin project authors |
| 6 | +// Licensed under Apache License v2.0 |
| 7 | +// |
| 8 | +// See LICENSE.txt for license information |
| 9 | +// See CONTRIBUTORS.txt for the list of SwiftContainerPlugin project authors |
| 10 | +// |
| 11 | +// SPDX-License-Identifier: Apache-2.0 |
| 12 | +// |
| 13 | +//===----------------------------------------------------------------------===// |
| 14 | + |
| 15 | +import struct Foundation.Data |
| 16 | + |
| 17 | +/// A destination, such as a registry, to which container images can be uploaded. |
| 18 | +public protocol ImageDestination { |
| 19 | + /// Uploads a blob of unstructured data. |
| 20 | + /// |
| 21 | + /// - Parameters: |
| 22 | + /// - repository: Name of the destination repository. |
| 23 | + /// - mediaType: mediaType field for returned ContentDescriptor. |
| 24 | + /// On the wire, all blob uploads are `application/octet-stream'. |
| 25 | + /// - data: Object to be uploaded. |
| 26 | + /// - Returns: An ContentDescriptor object representing the |
| 27 | + /// uploaded blob. |
| 28 | + /// - Throws: If the upload fails. |
| 29 | + func putBlob( |
| 30 | + repository: ImageReference.Repository, |
| 31 | + mediaType: String, |
| 32 | + data: Data |
| 33 | + ) async throws -> ContentDescriptor |
| 34 | + |
| 35 | + /// Encodes and uploads a JSON object. |
| 36 | + /// |
| 37 | + /// - Parameters: |
| 38 | + /// - repository: Name of the destination repository. |
| 39 | + /// - mediaType: mediaType field for returned ContentDescriptor. |
| 40 | + /// On the wire, all blob uploads are `application/octet-stream'. |
| 41 | + /// - data: Object to be uploaded. |
| 42 | + /// - Returns: An ContentDescriptor object representing the |
| 43 | + /// uploaded blob. |
| 44 | + /// - Throws: If the blob cannot be encoded or the upload fails. |
| 45 | + /// |
| 46 | + /// Some JSON objects, such as ImageConfiguration, are stored |
| 47 | + /// in the registry as plain blobs with MIME type "application/octet-stream". |
| 48 | + /// This function encodes the data parameter and uploads it as a generic blob. |
| 49 | + func putBlob<Body: Encodable>( |
| 50 | + repository: ImageReference.Repository, |
| 51 | + mediaType: String, |
| 52 | + data: Body |
| 53 | + ) async throws -> ContentDescriptor |
| 54 | + |
| 55 | + /// Checks whether a blob exists. |
| 56 | + /// |
| 57 | + /// - Parameters: |
| 58 | + /// - repository: Name of the destination repository. |
| 59 | + /// - digest: Digest of the requested blob. |
| 60 | + /// - Returns: True if the blob exists, otherwise false. |
| 61 | + /// - Throws: If the destination encounters an error. |
| 62 | + func blobExists( |
| 63 | + repository: ImageReference.Repository, |
| 64 | + digest: ImageReference.Digest |
| 65 | + ) async throws -> Bool |
| 66 | + |
| 67 | + /// Encodes and uploads an image manifest. |
| 68 | + /// |
| 69 | + /// - Parameters: |
| 70 | + /// - repository: Name of the destination repository. |
| 71 | + /// - reference: Optional tag to apply to this manifest. |
| 72 | + /// - manifest: Manifest to be uploaded. |
| 73 | + /// - Returns: An ContentDescriptor object representing the |
| 74 | + /// uploaded blob. |
| 75 | + /// - Throws: If the blob cannot be encoded or the upload fails. |
| 76 | + /// |
| 77 | + /// Manifests are not treated as blobs by the distribution specification. |
| 78 | + /// They have their own MIME types and are uploaded to different |
| 79 | + /// registry endpoints than blobs. |
| 80 | + func putManifest( |
| 81 | + repository: ImageReference.Repository, |
| 82 | + reference: (any ImageReference.Reference)?, |
| 83 | + manifest: ImageManifest |
| 84 | + ) async throws -> ContentDescriptor |
| 85 | +} |
| 86 | + |
| 87 | +extension ImageDestination { |
| 88 | + /// Uploads a blob of unstructured data. |
| 89 | + /// |
| 90 | + /// - Parameters: |
| 91 | + /// - repository: Name of the destination repository. |
| 92 | + /// - mediaType: mediaType field for returned ContentDescriptor. |
| 93 | + /// On the wire, all blob uploads are `application/octet-stream'. |
| 94 | + /// - data: Object to be uploaded. |
| 95 | + /// - Returns: An ContentDescriptor object representing the |
| 96 | + /// uploaded blob. |
| 97 | + /// - Throws: If the upload fails. |
| 98 | + public func putBlob( |
| 99 | + repository: ImageReference.Repository, |
| 100 | + mediaType: String = "application/octet-stream", |
| 101 | + data: Data |
| 102 | + ) async throws -> ContentDescriptor { |
| 103 | + try await putBlob(repository: repository, mediaType: mediaType, data: data) |
| 104 | + } |
| 105 | +} |
0 commit comments