diff --git a/Cargo.lock b/Cargo.lock index c455c55..278ecb6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -250,7 +250,7 @@ dependencies = [ [[package]] name = "buckets-core" -version = "1.0.3" +version = "1.0.4" dependencies = [ "oiseau", "pathbufd", diff --git a/crates/buckets-core/Cargo.toml b/crates/buckets-core/Cargo.toml index bbe715d..f7fc34a 100644 --- a/crates/buckets-core/Cargo.toml +++ b/crates/buckets-core/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "buckets-core" description = "Buckets media upload types" -version = "1.0.3" +version = "1.0.4" edition = "2024" readme = "../../README.md" authors.workspace = true diff --git a/crates/buckets-core/src/database/uploads.rs b/crates/buckets-core/src/database/uploads.rs index 5b51a33..213ffd7 100644 --- a/crates/buckets-core/src/database/uploads.rs +++ b/crates/buckets-core/src/database/uploads.rs @@ -210,7 +210,7 @@ impl DataManager { // if there's an issue in the database // // the actual file takes up much more space than the database entry. - let upload = self.get_upload_by_id(id).await?; + let upload = self.get_upload_by_id_bucket(id, bucket).await?; upload.remove(&self.0.0.directory)?; // delete from database diff --git a/crates/buckets-core/src/model.rs b/crates/buckets-core/src/model.rs index 0ab4617..23d5cd4 100644 --- a/crates/buckets-core/src/model.rs +++ b/crates/buckets-core/src/model.rs @@ -90,14 +90,44 @@ impl MediaUpload { } } - /// Get the path to the fs file for this upload. - pub fn path(&self, directory: &str) -> PathBufD { + /// Get the path to the fs file for this upload (without bucket). + pub(crate) fn legacy_path(&self, directory: &str) -> PathBufD { PathBufD::current().extend(&[ directory, &format!("{}.{}", self.id, self.metadata.what.extension()), ]) } + /// Get the path to the fs file for this upload (with bucket). + pub fn full_path(&self, directory: &str) -> PathBufD { + PathBufD::current().extend(&[ + directory, + &format!( + "{}{}.{}", + if self.bucket != "" { + format!("{}.", self.bucket) + } else { + String::new() + }, + self.id, + self.metadata.what.extension() + ), + ]) + } + + /// Get the path to the fs file for this upload. + /// + /// Uses path with bucket unless legacy path exists. + pub fn path(&self, directory: &str) -> PathBufD { + let legacy = self.legacy_path(directory); + + if std::fs::exists(&legacy).unwrap() { + return legacy; + } + + self.full_path(directory) + } + /// Write to this upload in the file system. pub fn write(&self, directory: &str, bytes: &[u8]) -> Result<()> { match write(self.path(directory), bytes) { diff --git a/crates/buckets/src/routes.rs b/crates/buckets/src/routes.rs index 1e0ab52..daf5fb2 100644 --- a/crates/buckets/src/routes.rs +++ b/crates/buckets/src/routes.rs @@ -33,7 +33,7 @@ pub async fn get_request( ) -> impl IntoResponse { let data = &(data.read().await); - let upload = match data.get_upload_by_id(id).await { + let upload = match data.get_upload_by_id_bucket(id, &bucket).await { Ok(u) => u, Err(e) => { if let Some(default) = data.0.0.bucket_defaults.get(&bucket) { @@ -42,19 +42,20 @@ pub async fn get_request( Body::from(std::fs::read(&default.0).expect("failed to read default file")), )); } else { - return Err(Json(e.into())); + match data.get_upload_by_id(id).await { + Ok(x) => x, + Err(_) => { + return Err(Json(ApiReturn { + ok: false, + message: e.to_string(), + payload: (), + })); + } + } } } }; - if !upload.bucket.is_empty() && upload.bucket != bucket { - return Err(Json(ApiReturn { - ok: false, - message: Error::MiscError("Bucket mismatch".to_string()).to_string(), - payload: (), - })); - } - // ... let path = upload.path(&data.0.0.directory); @@ -84,15 +85,14 @@ pub async fn get_json_request( ) -> impl IntoResponse { let data = &(data.read().await); - let upload = match data.get_upload_by_id(id).await { + let upload = match data.get_upload_by_id_bucket(id, &bucket).await { Ok(u) => u, - Err(e) => return Json(e.into()), + Err(_) => match data.get_upload_by_id(id).await { + Ok(x) => x, + Err(e) => return Json(e.into()), + }, }; - if !upload.bucket.is_empty() && upload.bucket != bucket { - return Json(Error::MiscError("Bucket mismatch".to_string()).into()); - } - Json(ApiReturn { ok: true, message: "Success".to_string(),