Skip to content

Commit 2c8811c

Browse files
committed
feat(s3): return existence on uploading
1 parent c7f0622 commit 2c8811c

7 files changed

Lines changed: 58 additions & 30 deletions

File tree

netmito/src/api/groups.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ pub async fn upload_attachment(
157157
Path(group_name): Path<String>,
158158
Json(req): Json<UploadAttachmentReq>,
159159
) -> Result<Json<UploadAttachmentResp>, ApiError> {
160-
let url =
160+
let (exist, url) =
161161
service::s3::user_upload_attachment(u.id, &pool, group_name, req.key, req.content_length)
162162
.await
163163
.map_err(|e| match e {
@@ -168,7 +168,7 @@ pub async fn upload_attachment(
168168
ApiError::InternalServerError
169169
}
170170
})?;
171-
Ok(Json(UploadAttachmentResp { url }))
171+
Ok(Json(UploadAttachmentResp { exist, url }))
172172
}
173173

174174
pub async fn download_attachment(

netmito/src/api/tasks.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ pub async fn upload_artifact(
207207
Path(uuid): Path<Uuid>,
208208
Json(req): Json<UploadArtifactReq>,
209209
) -> Result<Json<UploadArtifactResp>, ApiError> {
210-
let url =
210+
let (exist, url) =
211211
service::s3::user_upload_artifact(&pool, u.id, uuid, req.content_type, req.content_length)
212212
.await
213213
.map_err(|e| match e {
@@ -218,7 +218,7 @@ pub async fn upload_artifact(
218218
ApiError::InternalServerError
219219
}
220220
})?;
221-
Ok(Json(UploadArtifactResp { url }))
221+
Ok(Json(UploadArtifactResp { exist, url }))
222222
}
223223

224224
pub async fn download_artifact(

netmito/src/client/interactive.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,22 @@ pub(crate) fn output_group_info(info: &GroupQueryInfo) {
221221
}
222222
}
223223

224+
pub(crate) fn output_upload_artifact_resp(exist: bool) {
225+
if exist {
226+
tracing::info!("Artifact already exists on server, replaced it");
227+
} else {
228+
tracing::info!("Artifact uploaded successfully");
229+
}
230+
}
231+
232+
pub(crate) fn output_upload_attachment_resp(exist: bool) {
233+
if exist {
234+
tracing::info!("Attachment already exists on server, replaced it");
235+
} else {
236+
tracing::info!("Attachment uploaded successfully");
237+
}
238+
}
239+
224240
pub(crate) fn fill_admin_create_user(
225241
username: Option<String>,
226242
password: Option<String>,

netmito/src/client/mod.rs

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -630,7 +630,10 @@ impl MitoClient {
630630
self.http_client.user_submit_task(req).await
631631
}
632632

633-
pub async fn upload_artifact(&mut self, args: UploadArtifactArgs) -> crate::error::Result<()> {
633+
pub async fn upload_artifact(
634+
&mut self,
635+
args: UploadArtifactArgs,
636+
) -> crate::error::Result<bool> {
634637
let metadata = args
635638
.local_file
636639
.metadata()
@@ -649,13 +652,14 @@ impl MitoClient {
649652
let resp = self.http_client.get_upload_artifact_resp(uuid, req).await?;
650653
self.http_client
651654
.upload_file(resp.url.as_str(), content_length, args.local_file, args.pb)
652-
.await
655+
.await?;
656+
Ok(resp.exist)
653657
}
654658

655659
pub async fn upload_attachment(
656660
&mut self,
657661
args: UploadAttachmentArgs,
658-
) -> crate::error::Result<()> {
662+
) -> crate::error::Result<bool> {
659663
fn get_fname<P: AsRef<std::path::Path>>(p: P) -> crate::error::Result<String> {
660664
let fname = p
661665
.as_ref()
@@ -718,7 +722,8 @@ impl MitoClient {
718722
.await?;
719723
self.http_client
720724
.upload_file(resp.url.as_str(), content_length, args.local_file, args.pb)
721-
.await
725+
.await?;
726+
Ok(resp.exist)
722727
}
723728

724729
pub async fn admin_workers_cancel(
@@ -860,7 +865,7 @@ impl MitoClient {
860865
pub async fn tasks_artifacts_upload(
861866
&mut self,
862867
args: TaskArtifactUploadArgs,
863-
) -> crate::error::Result<()> {
868+
) -> crate::error::Result<bool> {
864869
self.upload_artifact(args).await
865870
}
866871

@@ -874,7 +879,7 @@ impl MitoClient {
874879
pub async fn groups_attachments_upload(
875880
&mut self,
876881
args: GroupAttachmentUploadArgs,
877-
) -> crate::error::Result<()> {
882+
) -> crate::error::Result<bool> {
878883
self.upload_attachment(args).await
879884
}
880885

@@ -1535,8 +1540,8 @@ impl MitoClient {
15351540
}
15361541
}
15371542
AttachmentsCommands::Upload(args) => match self.upload_attachment(args).await {
1538-
Ok(_) => {
1539-
tracing::info!("Attachment uploaded successfully");
1543+
Ok(exist) => {
1544+
output_upload_attachment_resp(exist);
15401545
}
15411546
Err(e) => {
15421547
tracing::error!("{}", e);
@@ -1837,8 +1842,8 @@ impl MitoClient {
18371842
}
18381843
}
18391844
ArtifactsCommands::Upload(args) => match self.upload_artifact(args).await {
1840-
Ok(_) => {
1841-
tracing::info!("Artifact uploaded successfully");
1845+
Ok(exist) => {
1846+
output_upload_artifact_resp(exist);
18421847
}
18431848
Err(e) => {
18441849
tracing::error!("{}", e);

netmito/src/schema.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ pub struct UploadAttachmentReq {
169169

170170
#[derive(Debug, Serialize, Deserialize, Clone)]
171171
pub struct UploadAttachmentResp {
172+
pub exist: bool,
172173
pub url: String,
173174
}
174175

@@ -180,6 +181,7 @@ pub struct UploadArtifactReq {
180181

181182
#[derive(Debug, Serialize, Deserialize, Clone)]
182183
pub struct UploadArtifactResp {
184+
pub exist: bool,
183185
pub url: String,
184186
}
185187

netmito/src/service/s3.rs

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,7 @@ pub(crate) async fn group_upload_artifact(
406406
task: StoredTaskModel,
407407
content_type: ArtifactContentType,
408408
content_length: u64,
409-
) -> Result<String, crate::error::Error> {
409+
) -> Result<(bool, String), crate::error::Error> {
410410
let now = TimeDateTimeWithTimeZone::now_utc();
411411
let content_length = content_length as i64;
412412
let (uuid, group_id) = match task {
@@ -425,9 +425,10 @@ pub(crate) async fn group_upload_artifact(
425425
}
426426
let s3_client = pool.s3.clone();
427427
let artifacts_bucket = pool.artifacts_bucket.clone();
428-
let url = pool
428+
// This returns (exist, url)
429+
let resp = pool
429430
.db
430-
.transaction::<_, String, Error>(|txn| {
431+
.transaction::<_, (bool, String), Error>(|txn| {
431432
Box::pin(async move {
432433
// Update the task to reflect the artifact upload
433434
match task {
@@ -456,7 +457,7 @@ pub(crate) async fn group_upload_artifact(
456457
let s3_object_key = format!("{uuid}/{content_type}");
457458
let url: String;
458459
// Check group storage quota and allocate storage for the artifact
459-
match artifact {
460+
let exist = match artifact {
460461
Some(artifact) => {
461462
let recorded_content_length = content_length.max(artifact.size);
462463
let new_storage_used =
@@ -486,6 +487,7 @@ pub(crate) async fn group_upload_artifact(
486487
..Default::default()
487488
};
488489
group.update(txn).await?;
490+
true
489491
}
490492
None => {
491493
let new_storage_used = group.storage_used + content_length;
@@ -516,13 +518,14 @@ pub(crate) async fn group_upload_artifact(
516518
..Default::default()
517519
};
518520
group.update(txn).await?;
521+
false
519522
}
520-
}
521-
Ok(url)
523+
};
524+
Ok((exist, url))
522525
})
523526
})
524527
.await?;
525-
Ok(url)
528+
Ok(resp)
526529
}
527530

528531
pub async fn user_upload_artifact(
@@ -531,7 +534,7 @@ pub async fn user_upload_artifact(
531534
uuid: Uuid,
532535
content_type: ArtifactContentType,
533536
content_length: u64,
534-
) -> Result<String, crate::error::Error> {
537+
) -> Result<(bool, String), crate::error::Error> {
535538
// Find the task and group
536539
let (group_id, task) = find_task_by_uuid(pool, uuid).await?;
537540
// Check if user has permission to upload artifact to the task
@@ -861,7 +864,7 @@ pub async fn user_upload_attachment(
861864
group_name: String,
862865
key: String,
863866
content_length: u64,
864-
) -> Result<String, crate::error::Error> {
867+
) -> Result<(bool, String), crate::error::Error> {
865868
tracing::debug!(
866869
"Uploading attachment to group {} with key {} and size {}",
867870
group_name,
@@ -880,9 +883,9 @@ pub async fn user_upload_attachment(
880883
let s3_client = pool.s3.clone();
881884
let now = TimeDateTimeWithTimeZone::now_utc();
882885
let attachments_bucket = pool.attachments_bucket.clone();
883-
let uri = pool
886+
let resp = pool
884887
.db
885-
.transaction::<_, String, crate::error::Error>(|txn| {
888+
.transaction::<_, (bool, String), crate::error::Error>(|txn| {
886889
Box::pin(async move {
887890
let group = Group::Entity::find()
888891
.filter(Group::Column::GroupName.eq(group_name.clone()))
@@ -907,7 +910,7 @@ pub async fn user_upload_attachment(
907910
.one(txn)
908911
.await?;
909912
let url: String;
910-
match attachment {
913+
let exist = match attachment {
911914
Some(attachment) => {
912915
let recorded_content_length = content_length.max(attachment.size);
913916
let new_storage_used =
@@ -937,6 +940,7 @@ pub async fn user_upload_attachment(
937940
..Default::default()
938941
};
939942
group.update(txn).await?;
943+
true
940944
}
941945
None => {
942946
let new_storage_used = group.storage_used + content_length;
@@ -968,13 +972,14 @@ pub async fn user_upload_attachment(
968972
..Default::default()
969973
};
970974
group.update(txn).await?;
975+
false
971976
}
972-
}
973-
Ok(url)
977+
};
978+
Ok((exist, url))
974979
})
975980
})
976981
.await?;
977-
Ok(uri)
982+
Ok(resp)
978983
}
979984

980985
// show_pb is used to show progress bar

netmito/src/service/worker/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -733,7 +733,7 @@ pub async fn report_task(
733733
content_type,
734734
content_length
735735
);
736-
let url = group_upload_artifact(
736+
let (_, url) = group_upload_artifact(
737737
pool,
738738
StoredTaskModel::Active(task),
739739
content_type,

0 commit comments

Comments
 (0)