Skip to content

Commit

Permalink
wip: attachments
Browse files Browse the repository at this point in the history
  • Loading branch information
goto-eof committed Dec 27, 2022
1 parent 850b4e4 commit cf186d5
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 8 deletions.
33 changes: 28 additions & 5 deletions src/dao/dao_attachment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,7 @@ pub async fn get_by_id(id: i32) -> Result<db_attachment::Model, DevBoardGenericE
Ok(opt.unwrap())
}

pub async fn get_by_item_id(
parent_id: i32,
) -> Result<Vec<db_attachment::Model>, DevBoardGenericError> {
pub async fn get_by_item_id(parent_id: i32) -> Result<Vec<FullAttachment>, DevBoardGenericError> {
let db = DB_POOL.get().await;

let result = db_attachment::Entity::find()
Expand All @@ -69,7 +67,32 @@ pub async fn get_by_item_id(

let models = result.unwrap();

Ok(models)
let mut attachments: Vec<FullAttachment> = 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(
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 0 additions & 2 deletions src/dao/dao_item.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::fs;

use crate::structure::structure::DevBoardErrorType;
use crate::structure::structure::DevBoardGenericError;
use crate::structure::structure::ItemAttachments;
Expand Down
4 changes: 3 additions & 1 deletion src/structure/structure.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -83,5 +85,5 @@ pub struct UserReponse {
#[derive(Serialize)]
pub struct ItemAttachments {
pub item: db_item::Model,
pub attachments: Vec<db_attachment::Model>,
pub attachments: Vec<FullAttachment>,
}

0 comments on commit cf186d5

Please sign in to comment.