diff --git a/src/dao/dao_attachment.rs b/src/dao/dao_attachment.rs index 807c149..7d20379 100644 --- a/src/dao/dao_attachment.rs +++ b/src/dao/dao_attachment.rs @@ -47,9 +47,7 @@ pub async fn get_by_id(id: i32) -> Result Result, DevBoardGenericError> { +pub async fn get_by_item_id(parent_id: i32) -> Result, DevBoardGenericError> { let db = DB_POOL.get().await; let result = db_attachment::Entity::find() @@ -69,7 +67,32 @@ pub async fn get_by_item_id( let models = result.unwrap(); - Ok(models) + let mut attachments: Vec = vec![]; + + for model in models { + let name_cloned = model.name.clone(); + let file_extension = Path::new(&name_cloned).extension().and_then(OsStr::to_str); + + let file_name = model.hashcode.clone(); + + let body_result = std::fs::read(format!( + "{}/{}.{}", + SETTINGS.issue_storage_path, + file_name, + file_extension.unwrap_or("") + )); + + let body_result = body_result.unwrap(); + + let encoded = base64::encode(body_result); + let encoded = format!("data:{};base64,{}", model.file_type, encoded); + attachments.push(FullAttachment { + meta_information: model, + content: encoded, + }); + } + + Ok(attachments) } pub async fn create( @@ -378,7 +401,7 @@ pub async fn download_file( let body_result = body_result.unwrap(); let encoded = base64::encode(body_result); - + let encoded = format!("data:{};base64,{}", file_model.file_type, encoded); Ok(FullAttachment { meta_information: file_model, content: encoded, diff --git a/src/dao/dao_item.rs b/src/dao/dao_item.rs index c147824..b7f4189 100644 --- a/src/dao/dao_item.rs +++ b/src/dao/dao_item.rs @@ -1,5 +1,3 @@ -use std::fs; - use crate::structure::structure::DevBoardErrorType; use crate::structure::structure::DevBoardGenericError; use crate::structure::structure::ItemAttachments; diff --git a/src/structure/structure.rs b/src/structure/structure.rs index 5e3e037..8cc0463 100644 --- a/src/structure/structure.rs +++ b/src/structure/structure.rs @@ -1,5 +1,7 @@ use entity::{db_attachment, db_board, db_column, db_item}; use serde::{Deserialize, Serialize}; + +use crate::dao::dao_attachment::FullAttachment; #[derive(Serialize, Debug)] pub enum DevBoardErrorType { Error, @@ -83,5 +85,5 @@ pub struct UserReponse { #[derive(Serialize)] pub struct ItemAttachments { pub item: db_item::Model, - pub attachments: Vec, + pub attachments: Vec, }