From 3810a4dcad735d37046a19d0069f9f21e47323d5 Mon Sep 17 00:00:00 2001 From: Purvesh Date: Sun, 15 Dec 2024 10:26:22 +1300 Subject: [PATCH] remove path from asset model --- react-admin/package-lock.json | 6 +-- react-admin/src/pages/asset/DisplayAsset.tsx | 2 +- react-admin/src/pages/page/PageCreate.tsx | 2 +- react-admin/src/types/asset/IAssetModel.ts | 2 +- .../asset/delete_asset_api_handler.rs | 2 +- .../asset/delete_folder_api_handler.rs | 2 +- .../asset/rename_asset_api_handler.rs | 4 +- .../handlers/asset/store_asset_api_handler.rs | 3 +- src/models/asset_model.rs | 11 ++-- src/repositories/asset_repository.rs | 50 +++++++++++++------ src/services/asset_service.rs | 6 +-- 11 files changed, 54 insertions(+), 36 deletions(-) diff --git a/react-admin/package-lock.json b/react-admin/package-lock.json index ec1ec22..be1e774 100644 --- a/react-admin/package-lock.json +++ b/react-admin/package-lock.json @@ -7807,9 +7807,9 @@ } }, "node_modules/caniuse-lite": { - "version": "1.0.30001621", - "resolved": "https://registry.npmjs.org/caniuse-lite/-/caniuse-lite-1.0.30001621.tgz", - "integrity": "sha512-+NLXZiviFFKX0fk8Piwv3PfLPGtRqJeq2TiNoUff/qB5KJgwecJTvCXDpmlyP/eCI/GUEmp/h/y5j0yckiiZrA==", + "version": "1.0.30001688", + "resolved": "https://registry.npmjs.org/caniuse-lite/-/caniuse-lite-1.0.30001688.tgz", + "integrity": "sha512-Nmqpru91cuABu/DTCXbM2NSRHzM2uVHfPnhJ/1zEAJx/ILBRVmz3pzH4N7DZqbdG0gWClsCC05Oj0mJ/1AWMbA==", "funding": [ { "type": "opencollective", diff --git a/react-admin/src/pages/asset/DisplayAsset.tsx b/react-admin/src/pages/asset/DisplayAsset.tsx index 37870ad..5c6c71e 100644 --- a/react-admin/src/pages/asset/DisplayAsset.tsx +++ b/react-admin/src/pages/asset/DisplayAsset.tsx @@ -88,7 +88,7 @@ export const DisplayAsset = ({ asset, openFolder }: DisplayAssetProp) => { ) : ( <> {asset.name} diff --git a/react-admin/src/pages/page/PageCreate.tsx b/react-admin/src/pages/page/PageCreate.tsx index 7b0a2d9..23d3a18 100644 --- a/react-admin/src/pages/page/PageCreate.tsx +++ b/react-admin/src/pages/page/PageCreate.tsx @@ -278,7 +278,7 @@ function PageCreate() { }; const selectedAsset = (index: number, selectedAsset: IAssetModel) => { - setValue(`page_fields.${index}.field_content.text_value.text_value`, selectedAsset.path); + setValue(`page_fields.${index}.field_content.text_value.text_value`, selectedAsset.new_path); closeSingleAssetModal(); } const singleImageButtonOnClick = (e: React.MouseEvent) => { diff --git a/react-admin/src/types/asset/IAssetModel.ts b/react-admin/src/types/asset/IAssetModel.ts index a5da412..0e665e8 100644 --- a/react-admin/src/types/asset/IAssetModel.ts +++ b/react-admin/src/types/asset/IAssetModel.ts @@ -1,7 +1,7 @@ type IAssetModel = { id: string; name: string; - path: string; + new_path: string; asset_type: string; metadata: string; created_at: string; diff --git a/src/api/handlers/asset/delete_asset_api_handler.rs b/src/api/handlers/asset/delete_asset_api_handler.rs index 7bcc04f..6779b24 100644 --- a/src/api/handlers/asset/delete_asset_api_handler.rs +++ b/src/api/handlers/asset/delete_asset_api_handler.rs @@ -29,7 +29,7 @@ pub async fn delete_asset_api_handler( .find_by_id(&state.db, &asset_id) .await?; - let asset_path = format!("./{path}", path = asset_model.path); + let asset_path = format!("./{path}", path = asset_model.new_path); // @todo return early if fs::try_exists(&asset_path).await? { diff --git a/src/api/handlers/asset/delete_folder_api_handler.rs b/src/api/handlers/asset/delete_folder_api_handler.rs index de0121f..fec0f34 100644 --- a/src/api/handlers/asset/delete_folder_api_handler.rs +++ b/src/api/handlers/asset/delete_folder_api_handler.rs @@ -30,7 +30,7 @@ pub async fn delete_folder_api_handler( .find_by_id(&state.db, &asset_id) .await?; - let folder_path = format!("./{path}", path = asset_model.path); + let folder_path = format!("./{path}", path = asset_model.new_path); // @todo return early if fs::try_exists(&folder_path).await? { diff --git a/src/api/handlers/asset/rename_asset_api_handler.rs b/src/api/handlers/asset/rename_asset_api_handler.rs index e627051..5a68ef5 100644 --- a/src/api/handlers/asset/rename_asset_api_handler.rs +++ b/src/api/handlers/asset/rename_asset_api_handler.rs @@ -40,7 +40,7 @@ pub async fn rename_asset_api_handler( let asset_model = state.asset_service .find_by_id(&state.db, &asset_id) .await?; - let old_asset_path = format!(".{}",asset_model.path); + let old_asset_path = format!(".{}",asset_model.new_path); let new_asset_path = format!("/public/upload/{}", &payload.name); if fs::try_exists(&old_asset_path).await? { @@ -48,7 +48,7 @@ pub async fn rename_asset_api_handler( } let updated_asset_model = state.asset_service - .update_asset_path(&state.db, &payload.name, &new_asset_path, &asset_id, &logged_in_user.email) + .update_asset_path(&state.db, &payload.name, &asset_id, &logged_in_user.email) .await?; let response = ApiResponse { diff --git a/src/api/handlers/asset/store_asset_api_handler.rs b/src/api/handlers/asset/store_asset_api_handler.rs index e409589..4a99f36 100644 --- a/src/api/handlers/asset/store_asset_api_handler.rs +++ b/src/api/handlers/asset/store_asset_api_handler.rs @@ -78,13 +78,12 @@ pub async fn store_asset_api_handler( .await?; creatable_asset_model.parent_id = query_parent_id; - asset_file = format!("/{}/{}", parent_asset.path, new_file_name.clone()); + asset_file = format!("/{}/{}", parent_asset.new_path, new_file_name.clone()); } else { asset_file = format!("/public/upload/{}", new_file_name.clone()); } creatable_asset_model.name = new_file_name.clone(); - creatable_asset_model.path = asset_file.clone(); creatable_asset_model.asset_type = String::from("FILE"); creatable_asset_model.metadata = MetaDataType::FileTypeMetaData {file_type}; diff --git a/src/models/asset_model.rs b/src/models/asset_model.rs index 780f045..17f14ec 100644 --- a/src/models/asset_model.rs +++ b/src/models/asset_model.rs @@ -1,4 +1,4 @@ -use crate::error::{Error, Result}; + use crate::error::{Error, Result}; use serde::{Deserialize, Serialize}; use surrealdb::sql::{Datetime, Object, Value}; use super::{BaseModel, Pagination}; @@ -8,7 +8,7 @@ pub struct NewAssetModel { pub id: String, pub parent_id: String, pub name: String, - pub path: String, + pub new_path: String, pub asset_type: String, pub metadata: MetaDataType, pub created_at: Datetime, @@ -80,7 +80,6 @@ impl TryFrom for NewAssetModel { let id = val.get("id").get_id()?; let parent_id = val.get("parent_id").get_string()?; let name = val.get("name").get_string()?; - let path = val.get("path").get_string()?; let asset_type = val.get("asset_type").get_string()?; let created_at = val.get("created_at").get_datetime()?; let created_by = val.get("created_by").get_string()?; @@ -119,11 +118,13 @@ impl TryFrom for NewAssetModel { _ => MetaDataType::FolderTypeMetaData {color: String::from("text-gray-400")} }; + let new_path = String::from(""); + Ok(NewAssetModel { id, parent_id, name, - path, + new_path, asset_type, metadata, created_at, @@ -144,7 +145,6 @@ impl TryFrom for FileTypeMetaDataStruct { } } - impl TryFrom for FolderTypeMetaDataStruct { type Error = Error; fn try_from(val: Object) -> Result { @@ -167,7 +167,6 @@ pub struct CreatableAssetModelNew { pub logged_in_username: String, pub parent_id: String, pub name: String, - pub path: String, pub asset_type: String, pub metadata: MetaDataType, } diff --git a/src/repositories/asset_repository.rs b/src/repositories/asset_repository.rs index aa23e50..f205122 100644 --- a/src/repositories/asset_repository.rs +++ b/src/repositories/asset_repository.rs @@ -26,10 +26,6 @@ impl AssetRepository { start: i64, parent_id: String ) -> Result> { - - - - // @todo fix this one let sql = "SELECT * FROM type::table($table) WHERE parent_id=$parent_id LIMIT $limit START $start;"; let vars = BTreeMap::from([ @@ -39,8 +35,6 @@ impl AssetRepository { ("parent_id".into(), parent_id.into()), ]); - - let responses = datastore.execute(sql, database_session, Some(vars)).await?; let mut asset_list: Vec = Vec::new(); @@ -48,8 +42,22 @@ impl AssetRepository { for object in into_iter_objects(responses)? { let asset_object = object?; - let asset_model: Result = asset_object.try_into(); - asset_list.push(asset_model?); + let mut asset_model: NewAssetModel = asset_object.try_into()?; + + let mut new_path = asset_model.name.clone(); + let mut t = asset_model.clone(); + + // Not sure do we need to add some kind a loop limit to prevent infinite loop?? + while !t.parent_id.is_empty() { + let parent_model = self.find_by_id(datastore, database_session, &t.parent_id).await?; + t = parent_model.clone(); + new_path = format!("{}/{}", parent_model.name, new_path); + } + + new_path = format!("/{}/{}", "public/upload", new_path); + asset_model.new_path = new_path; + + asset_list.push(asset_model); } Ok(asset_list) } @@ -91,7 +99,6 @@ impl AssetRepository { let data: BTreeMap = [ ("name".into(), creatable_asset_model.name.into()), - ("path".into(), creatable_asset_model.path.into(),), ("parent_id".into(), creatable_asset_model.parent_id.into()), ("asset_type".into(), creatable_asset_model.asset_type.into()), ("metadata".into(), metadata.into()), @@ -129,7 +136,6 @@ impl AssetRepository { let data: BTreeMap = [ ("name".into(), creatable_asset_model.name.into()), - ("path".into(), creatable_asset_model.path.into(),), ("asset_type".into(), creatable_asset_model.asset_type.into(),), ("parent_id".into(), creatable_asset_model.parent_id.into()), ("metadata".into(), metadata.into()), @@ -174,9 +180,27 @@ impl AssetRepository { None => Err(Error::Generic("no record found".to_string())), }; - let asset_model: Result = result_object?.try_into(); + let asset_object = result_object?; - asset_model + let mut asset_model: NewAssetModel = asset_object.try_into()?; + + let mut new_path = asset_model.name.clone(); + let mut t = asset_model.clone(); + + // Not sure do we need to add some kind a loop limit to prevent infinite loop?? + while !t.parent_id.is_empty() { + let parent_model = Box::pin(self + .find_by_id(datastore, database_session, &t.parent_id) + ).await?; + + t = parent_model.clone(); + new_path = format!("{}/{}", parent_model.name, new_path); + } + + new_path = format!("/{}/{}", "public/upload", new_path); + asset_model.new_path = new_path; + + Ok(asset_model) } pub async fn delete_by_id( @@ -212,7 +236,6 @@ impl AssetRepository { datastore: &Datastore, database_session: &Session, name: &str, - new_path: &str, asset_id: &str, logged_in_username: &str ) -> Result { @@ -225,7 +248,6 @@ impl AssetRepository { };"; let vars = BTreeMap::from([ - ("path".into(), new_path.into()), ("name".into(), name.into()), ("logged_in_user_name".into(), logged_in_username.into()), ("id".into(), asset_id.into()), diff --git a/src/services/asset_service.rs b/src/services/asset_service.rs index 003a9b8..7507822 100644 --- a/src/services/asset_service.rs +++ b/src/services/asset_service.rs @@ -115,7 +115,7 @@ impl AssetService { .find_by_id(db, &parent_id) .await?; - full_path = format!("{}/{}",asset.path, name.clone()); + full_path = format!("{}/{}",asset.new_path, name.clone()); } tokio::fs::create_dir_all(&format!("./{}", full_path.clone())).await?; @@ -126,7 +126,6 @@ impl AssetService { logged_in_username: logged_in_user.email, parent_id, name: name.clone(), - path: full_path, asset_type: "FOLDER".to_string(), metadata: MetaDataType::FolderTypeMetaData {color}, }; @@ -140,12 +139,11 @@ impl AssetService { &self, (datastore, database_session): &DB, name: &str, - new_path: &str, asset_id: &str, logged_in_username: &str ) -> Result { self.asset_repository - .update_asset_path(datastore, database_session, name, new_path, asset_id, logged_in_username) + .update_asset_path(datastore, database_session, name, asset_id, logged_in_username) .await } }