Skip to content

Commit

Permalink
Merge pull request #268 from avored/asset-improvements
Browse files Browse the repository at this point in the history
remove path from asset model
  • Loading branch information
indpurvesh authored Dec 15, 2024
2 parents 1d3bc01 + 3810a4d commit 1c848e6
Show file tree
Hide file tree
Showing 11 changed files with 54 additions and 36 deletions.
6 changes: 3 additions & 3 deletions react-admin/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion react-admin/src/pages/asset/DisplayAsset.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ export const DisplayAsset = ({ asset, openFolder }: DisplayAssetProp) => {
) : (
<>
<img
src={`${backend_url}${asset.path}`}
src={`${backend_url}${asset.new_path}`}
className="h-40"
alt={asset.name}
/>
Expand Down
2 changes: 1 addition & 1 deletion react-admin/src/pages/page/PageCreate.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<HTMLButtonElement>) => {
Expand Down
2 changes: 1 addition & 1 deletion react-admin/src/types/asset/IAssetModel.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
type IAssetModel = {
id: string;
name: string;
path: string;
new_path: string;
asset_type: string;
metadata: string;
created_at: string;
Expand Down
2 changes: 1 addition & 1 deletion src/api/handlers/asset/delete_asset_api_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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? {
Expand Down
2 changes: 1 addition & 1 deletion src/api/handlers/asset/delete_folder_api_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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? {
Expand Down
4 changes: 2 additions & 2 deletions src/api/handlers/asset/rename_asset_api_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,15 @@ 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? {
fs::rename(&old_asset_path, &format!(".{}", new_asset_path)).await?;
}

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 {
Expand Down
3 changes: 1 addition & 2 deletions src/api/handlers/asset/store_asset_api_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down
11 changes: 5 additions & 6 deletions src/models/asset_model.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand All @@ -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,
Expand Down Expand Up @@ -80,7 +80,6 @@ impl TryFrom<Object> 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()?;
Expand Down Expand Up @@ -119,11 +118,13 @@ impl TryFrom<Object> 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,
Expand All @@ -144,7 +145,6 @@ impl TryFrom<Object> for FileTypeMetaDataStruct {
}
}


impl TryFrom<Object> for FolderTypeMetaDataStruct {
type Error = Error;
fn try_from(val: Object) -> Result<FolderTypeMetaDataStruct> {
Expand All @@ -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,
}
Expand Down
50 changes: 36 additions & 14 deletions src/repositories/asset_repository.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,6 @@ impl AssetRepository {
start: i64,
parent_id: String
) -> Result<Vec<NewAssetModel>> {



// @todo fix this one

let sql = "SELECT * FROM type::table($table) WHERE parent_id=$parent_id LIMIT $limit START $start;";
let vars = BTreeMap::from([
Expand All @@ -39,17 +35,29 @@ impl AssetRepository {
("parent_id".into(), parent_id.into()),
]);



let responses = datastore.execute(sql, database_session, Some(vars)).await?;

let mut asset_list: Vec<NewAssetModel> = Vec::new();

for object in into_iter_objects(responses)? {
let asset_object = object?;

let asset_model: Result<NewAssetModel> = 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)
}
Expand Down Expand Up @@ -91,7 +99,6 @@ impl AssetRepository {

let data: BTreeMap<String, Value> = [
("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()),
Expand Down Expand Up @@ -129,7 +136,6 @@ impl AssetRepository {

let data: BTreeMap<String, Value> = [
("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()),
Expand Down Expand Up @@ -174,9 +180,27 @@ impl AssetRepository {
None => Err(Error::Generic("no record found".to_string())),
};

let asset_model: Result<NewAssetModel> = 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(
Expand Down Expand Up @@ -212,7 +236,6 @@ impl AssetRepository {
datastore: &Datastore,
database_session: &Session,
name: &str,
new_path: &str,
asset_id: &str,
logged_in_username: &str
) -> Result<NewAssetModel> {
Expand All @@ -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()),
Expand Down
6 changes: 2 additions & 4 deletions src/services/asset_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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?;

Expand All @@ -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},
};
Expand All @@ -140,12 +139,11 @@ impl AssetService {
&self,
(datastore, database_session): &DB,
name: &str,
new_path: &str,
asset_id: &str,
logged_in_username: &str
) -> Result<NewAssetModel> {
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
}
}

0 comments on commit 1c848e6

Please sign in to comment.