summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/files.rs2
-rw-r--r--src/models/model.rs11
-rw-r--r--src/routes/model.rs8
-rw-r--r--src/routes/user.rs4
4 files changed, 12 insertions, 13 deletions
diff --git a/src/files.rs b/src/files.rs
index b0f0ad5..380c3bf 100644
--- a/src/files.rs
+++ b/src/files.rs
@@ -55,7 +55,7 @@ pub async fn upload(
}
/// Delete a file from the filesystem
-pub fn delete_upload(filename: &String) -> Result<(), AppError> {
+pub fn delete_upload(filename: &str) -> Result<(), AppError> {
let last_slash_index = filename.rfind('/').unwrap();
let path = format!(
"{}/{}",
diff --git a/src/models/model.rs b/src/models/model.rs
index 80eb5ad..6b7c166 100644
--- a/src/models/model.rs
+++ b/src/models/model.rs
@@ -194,6 +194,7 @@ impl Model {
}
impl ModelUser {
+ /// Returns the author id from the `JsonValue`
pub fn author_id(&self) -> JsonValue {
match &self.author {
Some(json) => json.get("id").unwrap().clone(),
@@ -201,10 +202,10 @@ impl ModelUser {
}
}
- pub async fn upload_paths(&self) -> Option<Vec<String>> {
- if self.uploads.is_none() {
- return None;
- }
+ /// Returns a vec of string made by all the filepaths from the model
+ pub async fn list_upload_filepaths(&self) -> Option<Vec<String>> {
+ // Raise a `None` if `self.uploads` is `None`
+ self.uploads.as_ref()?;
let uploads = ModelUpload::find_by_model(self.id)
.await
@@ -215,7 +216,7 @@ impl ModelUser {
.map(|x| x.filepath.clone())
.collect::<Vec<String>>();
- return Some(paths);
+ Some(paths)
}
}
diff --git a/src/routes/model.rs b/src/routes/model.rs
index 4f935a9..0572977 100644
--- a/src/routes/model.rs
+++ b/src/routes/model.rs
@@ -109,12 +109,10 @@ async fn delete_model(claims: Claims, Path(model_id): Path<i32>) -> Result<Statu
let user = User::find_by_id(claims.user_id).await?;
- let uploads: Vec<String> = model.upload_paths().await.unwrap();
+ let uploads: Vec<String> = model.list_upload_filepaths().await.unwrap();
- if model.author_id() != user.id {
- if !user.is_staff.unwrap() {
- return Err(AppError::Unauthorized);
- }
+ if !(model.author_id() == user.id || user.is_staff.unwrap()) {
+ return Err(AppError::Unauthorized);
}
// If the model has been deleted, remove all old uploads from the file system
diff --git a/src/routes/user.rs b/src/routes/user.rs
index d20f1f6..791e441 100644
--- a/src/routes/user.rs
+++ b/src/routes/user.rs
@@ -63,7 +63,7 @@ async fn edit_my_avatar(
if user.avatar.is_some() {
let avatar_url = user.avatar.as_ref().unwrap();
- delete_upload(&avatar_url)?;
+ delete_upload(avatar_url)?;
}
match upload(
@@ -93,7 +93,7 @@ async fn delete_my_avatar(claims: Claims) -> Result<Json<UserList>, AppError> {
if user.avatar.is_some() {
let avatar_url = user.avatar.as_ref().unwrap();
- delete_upload(&avatar_url)?;
+ delete_upload(avatar_url)?;
}
user.edit_avatar(None).await?;