-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Manage existing but incomplete uploads by deleting them and restartin…
…g when uploading the same file, split out the delete service from the endpoint to reuse elsewhere
- Loading branch information
Showing
5 changed files
with
106 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
use super::associations; | ||
use super::db; | ||
use crate::config::Config; | ||
use anyhow::Error; | ||
use aws_sdk_s3::Client as S3Client; | ||
use sea_orm::entity::prelude::*; | ||
use sea_orm::{DatabaseConnection, EntityTrait}; | ||
use std::sync::Arc; | ||
use uuid::Uuid; | ||
|
||
pub async fn delete_object( | ||
db: &DatabaseConnection, | ||
s3: &Arc<S3Client>, | ||
id: Uuid, | ||
) -> Result<(), Error> { | ||
// Delete all associations for the object | ||
associations::db::Entity::delete_many() | ||
.filter(associations::db::Column::InputObjectId.eq(id.clone())) | ||
.exec(db) | ||
.await | ||
.map_err(|e| Error::new(e))?; | ||
|
||
// Find the object to delete | ||
match db::Entity::find_by_id(id).one(db).await? { | ||
Some(obj) => { | ||
let obj: db::ActiveModel = obj.into(); | ||
|
||
// Delete from the database | ||
let res = db::Entity::delete(obj).exec(db).await?; | ||
if res.rows_affected == 0 { | ||
return Err(anyhow::anyhow!("Object not found")); | ||
} | ||
|
||
// Delete from S3 | ||
let config = Config::from_env(); | ||
s3.delete_object() | ||
.bucket(config.s3_bucket) | ||
.key(format!("{}/{}", config.s3_prefix, id)) | ||
.send() | ||
.await | ||
.map_err(|e| Error::new(e))?; | ||
|
||
Ok(()) | ||
} | ||
_ => Err(anyhow::anyhow!("Object not found")), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters