1use std::sync::Arc;
5
6use tokio::sync::mpsc;
7
8use crate::core::auth::RegistryAuth;
9use crate::core::blobs::{LayerUploadOptions, LayerUploadPool, LocalLayer, UploadSummary};
10use crate::core::client::Client;
11use crate::core::errors::OciError;
12use crate::core::manifest::OciImageManifest;
13use crate::core::reference::Reference;
14
15#[derive(Debug, Clone)]
17pub struct PushMetadata {
18 pub target_ref: Reference,
20 pub layers: Vec<LocalLayer>,
22 pub config: ConfigBlob,
24 pub manifest: OciImageManifest,
26 pub index: Option<IndexBlob>,
28}
29
30#[derive(Debug, Clone)]
32pub struct ConfigBlob {
33 pub digest: String,
34 pub data: Vec<u8>,
35}
36
37#[derive(Debug, Clone)]
39pub struct IndexBlob {
40 pub media_type: String,
41 pub data: Vec<u8>,
42}
43
44#[derive(Debug, Clone)]
46pub struct PushOptions {
47 pub layer_upload_opts: Option<LayerUploadOptions>,
49 pub skip_blob_check: bool,
51}
52
53impl Default for PushOptions {
54 fn default() -> Self {
55 Self {
56 layer_upload_opts: None,
57 skip_blob_check: false,
58 }
59 }
60}
61
62#[derive(Debug)]
64pub struct PushResult {
65 pub layers_uploaded: usize,
67 pub layers_skipped: usize,
69 pub config_uploaded: bool,
71 pub index_pushed: bool,
73}
74
75#[allow(async_fn_in_trait)]
79pub trait OciImagePusher {
80 async fn push(&self, metadata: PushMetadata, opts: PushOptions) -> Result<PushResult, OciError>;
82}
83
84pub fn validate_push_metadata(metadata: &PushMetadata) -> Result<(), OciError> {
86 if metadata.layers.is_empty() {
87 return Err(OciError::PushError("Image must have at least one layer".to_string()));
88 }
89
90 if metadata.config.digest.is_empty() {
91 return Err(OciError::PushError("Config digest is required".to_string()));
92 }
93
94 if metadata.target_ref.repository.is_empty() {
95 return Err(OciError::PushError("Target reference repository is required".to_string()));
96 }
97
98 Ok(())
99}
100
101#[derive(Debug)]
103pub struct BlobCheckResult {
104 pub layers_to_upload: Vec<LocalLayer>,
105 pub skipped_count: usize,
106}
107
108pub async fn check_and_filter_layers(
111 client: &Client,
112 target_ref: &Reference,
113 auth: &RegistryAuth,
114 layers: Vec<LocalLayer>,
115) -> Result<BlobCheckResult, OciError> {
116 let mut layers_to_upload = Vec::new();
117 let mut skipped_count = 0;
118
119 for layer in layers {
120 match client.blob_exists(target_ref, &layer.digest, auth).await {
121 Ok(true) => {
122 skipped_count += 1;
123 continue;
124 }
125 Ok(false) => {
126 }
128 Err(_err) => {
129 }
131 }
132 layers_to_upload.push(layer);
133 }
134
135 Ok(BlobCheckResult {
136 layers_to_upload,
137 skipped_count,
138 })
139}
140
141pub async fn check_config_exists(
143 client: &Client,
144 target_ref: &Reference,
145 auth: &RegistryAuth,
146 config_digest: &str,
147) -> Result<bool, OciError> {
148 match client.blob_exists(target_ref, config_digest, auth).await {
149 Ok(exists) => Ok(exists),
150 Err(_) => {
151 Ok(false)
153 }
154 }
155}
156
157pub async fn upload_config_if_needed(
159 client: &Client,
160 target_ref: &Reference,
161 auth: &RegistryAuth,
162 config_digest: &str,
163 config_bytes: &[u8],
164 config_exists: bool,
165) -> Result<(), OciError> {
166 if !config_exists {
167 client
168 .push_blob(target_ref, auth, config_bytes, config_digest)
169 .await?;
170 }
171 Ok(())
172}
173
174pub async fn upload_layers_concurrent(
176 client: &Client,
177 target_ref: &Reference,
178 auth: Arc<RegistryAuth>,
179 layers: Vec<LocalLayer>,
180 options: LayerUploadOptions,
181) -> Result<UploadSummary, OciError> {
182 if layers.is_empty() {
183 return Ok(UploadSummary {
184 uploaded: vec![],
185 skipped: 0,
186 });
187 }
188
189 let (tx, rx) = mpsc::channel::<LocalLayer>(options.concurrency * 2);
190
191 let client_clone = client.clone();
193 let target_ref_clone = target_ref.clone();
194 let auth_clone = Arc::clone(&auth);
195 let options_clone = options.clone();
196
197 let upload_task = tokio::task::spawn(async move {
198 let uploader = LayerUploadPool::new(&client_clone, &target_ref_clone, auth_clone, options_clone);
199 uploader.upload_stream(rx).await
200 });
201
202 for layer in layers {
204 tx.send(layer).await.map_err(|e| {
205 OciError::Network(format!("Failed to enqueue layer: {}", e))
206 })?;
207 }
208 drop(tx);
209
210 upload_task
212 .await
213 .map_err(|err| OciError::Network(format!("Upload task failed: {}", err)))?
214}
215
216pub async fn push_manifest_bytes(
218 client: &Client,
219 target_ref: &Reference,
220 auth: &RegistryAuth,
221 manifest_media_type: &str,
222 manifest_bytes: &[u8],
223) -> Result<String, OciError> {
224 client
225 .push_manifest_bytes(target_ref, manifest_media_type, manifest_bytes, auth)
226 .await
227}
228
229pub async fn push_index_bytes_if_present(
231 client: &Client,
232 target_ref: &Reference,
233 auth: &RegistryAuth,
234 index_media_type: Option<&str>,
235 index_bytes: Option<&[u8]>,
236) -> Result<(), OciError> {
237 if let (Some(mt), Some(bytes)) = (index_media_type, index_bytes) {
238 client
239 .push_manifest_bytes(target_ref, mt, bytes, auth)
240 .await?;
241 }
242 Ok(())
243}