Skip to content

Commit

Permalink
Merge pull request #27 from Bill2015/develop-resource-type
Browse files Browse the repository at this point in the history
Add media type field in resource file type
  • Loading branch information
Bill2015 authored Mar 4, 2024
2 parents ae134cb + 10b46bd commit 84d19da
Show file tree
Hide file tree
Showing 24 changed files with 302 additions and 56 deletions.
146 changes: 142 additions & 4 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"date-fns": "^3.3.1",
"dayjs": "^1.11.10",
"embla-carousel-react": "^7.1.0",
"file-type": "^19.0.0",
"i18next": "^23.9.0",
"immer": "^10.0.3",
"mantine-contextmenu": "^7.5.0",
Expand Down
7 changes: 7 additions & 0 deletions src-tauri/Cargo.lock

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

1 change: 1 addition & 0 deletions src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ regex = "1.10.3"
paste = "1.0.14"
strum_macros = "0.26.1"
strum = "0.26.1"
file-format = { version = "0.24.0", features = [ "reader" ] }

[features]
# this feature is used for production builds or when `devPath` points to the filesystem
Expand Down
2 changes: 2 additions & 0 deletions src-tauri/src/modules/resource/application/dto/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ pub struct ResourceFileDto {
pub path: String,

pub ext: Option<String>,

pub media_type: String,
}

#[derive(Debug, Deserialize, Serialize)]
Expand Down
3 changes: 2 additions & 1 deletion src-tauri/src/modules/resource/domain/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ impl Resource {

// get new name
let new_name = new_name.unwrap_or(self.name.clone());
let ResourceFileVO { uuid, name, path, ext } = self.file.to_owned().unwrap();
let ResourceFileVO { uuid, name, path, ext, media_type } = self.file.to_owned().unwrap();

// if same as new, do nothing
if name == new_name {
Expand Down Expand Up @@ -96,6 +96,7 @@ impl Resource {
path: new_path,
uuid: uuid,
ext: ext,
media_type: media_type,
});

self.name = new_name;
Expand Down
61 changes: 42 additions & 19 deletions src-tauri/src/modules/resource/domain/valueobj/filevo.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::path::Path;

use file_format::FileFormat;
use serde::Serialize;

use crate::modules::resource::domain::ResourceGenericError;
Expand All @@ -11,6 +12,7 @@ pub struct ResourceFileVO {
pub name: String,
pub path: String,
pub ext: Option<String>,
pub media_type: String,
}

impl ResourceFileVO {
Expand All @@ -26,32 +28,53 @@ impl ResourceFileVO {
if path.exists() == false {
return Err(ResourceGenericError::FilePathNotExist());
}

if path.file_name().is_none() {
return Err(ResourceGenericError::FileNameIsEmpty());
}

let ext = match path.is_file() {
true => path.extension()
.map(|osr| Some(String::from(osr.to_str().unwrap())))
.unwrap_or(None),
false => Some("folder".to_string()),
};
let obj = match path.is_file() {
true => Self::handle_file(main_path, path),
false => Self::handle_folder(main_path, path),
}?;

Ok(obj)
}


fn handle_folder(main_path: &str, path: &Path) -> Result<Self, ResourceGenericError> {
let name = String::from(path.file_name().unwrap().to_str().unwrap());

Ok(ResourceFileVO {
uuid: String::from("id"),
name: name,
ext: None,
path: main_path.to_string(),
media_type: "application/folder".to_string(),
})
}

fn handle_file(main_path: &str, path: &Path) -> Result<Self, ResourceGenericError> {
let filefmt = FileFormat::from_file(path)
.or(Err(ResourceGenericError::FilePathNotExist()))?;

let media_type = filefmt.media_type().to_string();
let ext = path.extension()
.map(|osr| Some(String::from(osr.to_str().unwrap())))
.unwrap_or(None);

let name = String::from(path.file_name().unwrap().to_str().unwrap());
let name = match path.is_file() {
true if ext.is_none()=> name,
true => String::from(name.slice(..name.chars().count() - ext.as_ref().unwrap().chars().count() - 1)),
false => name,
let name = match ext.is_none() {
true => name,
false => String::from(name.slice(..name.chars().count() - ext.as_ref().unwrap().chars().count() - 1)),
};

Ok(
ResourceFileVO {
uuid: String::from("id"),
name: name,
ext: ext,
path: String::from(main_path),
}
)
Ok(ResourceFileVO {
uuid: String::from("id"),
name: name,
ext: ext,
path: main_path.to_string(),
media_type: media_type,
})
}
}
8 changes: 4 additions & 4 deletions src-tauri/src/modules/resource/infrastructure/domapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@ impl DomainModelMapper<ResourceTaggingVO> for ResourceTaggingDo {
// ====================================================================
impl DomainModelMapper<ResourceFileVO> for ResourceFileDo {
fn to_domain(self) -> ResourceFileVO {
let Self { uuid, name, path, ext } = self;
ResourceFileVO { uuid, name, path, ext }
let Self { uuid, name, path, ext, media_type } = self;
ResourceFileVO { uuid, name, path, ext, media_type }
}
fn from_domain(value: ResourceFileVO) -> Self {
let ResourceFileVO { uuid, name, path, ext } = value;
Self { uuid, name, path, ext }
let ResourceFileVO { uuid, name, path, ext, media_type } = value;
Self { uuid, name, path, ext, media_type }
}
}

Expand Down
Loading

0 comments on commit 84d19da

Please sign in to comment.