Skip to content

Commit

Permalink
feat(server): add i18n in upload api
Browse files Browse the repository at this point in the history
  • Loading branch information
yoyo930021 committed Apr 9, 2024
1 parent a16495c commit 73413e2
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 31 deletions.
24 changes: 16 additions & 8 deletions src/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,22 @@
"not_manager_of_this_schema": "You cannot manage this ticket."
},
"rules": {
"required": "%{field} field is required.",
"text_too_long": "%{field} field has too many characters.",
"text_too_many_lines": "%{field} field has too many lines.",
"not_valid_choice": "%{field} field has an incorrect choice.",
"not_upload_file": "%{field} field must upload a file.",
"not_upload_image": "%{field} field must upload an image.",
"unknown": "Unknown value for %{field} field.",
"too_many_choice": "%{field} field has too many choices."
"required": "%{field} is required.",
"text_too_long": "%{field} has too many characters.",
"text_too_many_lines": "%{field} has too many lines.",
"not_valid_choice": "%{field} has an incorrect choice.",
"not_upload_file": "%{field} must upload a file.",
"file_too_large": "%{field} file is too large.",
"invalid_file_type": "%{field} has an incorrect file type.",
"not_upload_image": "%{field} must upload an image.",
"image_too_large": "%{field} image is too large.",
"image_width_too_large": "%{field} image width is too large.",
"image_height_too_large": "%{field} image height is too large.",
"image_width_too_small": "%{field} image width is too small.",
"image_height_too_small": "%{field} image height is too small.",
"invalid_image_type": "%{field} has an incorrect image type.",
"unknown": "%{field} has an unknown value.",
"too_many_choice": "%{field} has too many choices."
}
}
}
10 changes: 9 additions & 1 deletion src/locales/zh.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"error": {
"not_join_to_this_ticket": "你沒有參與這個工單。",
"not_assign_to_this_flow": "你沒有被指定參與這個流程。",
"not_assign_to_this_schema": "你沒有不能參與這個流程",
"not_assign_to_this_schema": "你沒有能參與這個流程",
"not_probably_user_of_this_schema": "你不能建立這張工單。",
"not_manager_of_this_schema": "你不能管理這張工單。"
},
Expand All @@ -29,7 +29,15 @@
"text_too_many_lines": "%{field} 欄位行數太多。",
"not_valid_choice": "%{field} 欄位選項不正確。",
"not_upload_file": "%{field} 欄位必須上傳檔案。",
"file_too_large": "%{field} 欄位檔案太大。",
"invalid_file_type": "%{field} 欄位檔案類型不正確。",
"not_upload_image": "%{field} 欄位必須上傳圖片。",
"image_too_large": "%{field} 欄位圖片太大。",
"image_width_too_large": "%{field} 欄位圖片寬度太大。",
"image_height_too_large": "%{field} 欄位圖片高度太大。",
"image_width_too_small": "%{field} 欄位圖片寬度太小。",
"image_height_too_small": "%{field} 欄位圖片高度太小。",
"invalid_image_type": "%{field} 欄位圖片類型不正確。",
"unknown": "%{field} 欄位未知的值。",
"too_many_choice": "%{field} 欄位選項太多。"
}
Expand Down
4 changes: 2 additions & 2 deletions src/modules/ticket/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -491,7 +491,7 @@ async fn upload_file_in_form_field<'a>(

if form.field.is_file_define() {
return form
.upload_file(&mut conn, data_folder, file)
.upload_file(&mut conn, data_folder, &i18n, file)
.await
.map(|mut f: TicketFormFile| {
f.path = String::new();
Expand All @@ -502,7 +502,7 @@ async fn upload_file_in_form_field<'a>(

if form.field.is_image_define() {
return form
.upload_image(&mut conn, data_folder, file)
.upload_image(&mut conn, data_folder, &i18n, file)
.await
.map(|mut f| {
f.path = String::new();
Expand Down
69 changes: 49 additions & 20 deletions src/modules/ticket/forms/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,47 @@ impl FormSchema {
&self,
conn: &mut crate::DbConn,
i18n: &I18n<'a>,
data: &serde_json::Map<String, Value>,
data: &serde_json::Map<String, Value>
) -> Result<serde_json::Map<String, Value>, serde_json::Map<String, Value>> {
let FormSchema { fields, .. } = self;

let mut is_error = false;
let mut result = serde_json::Map::new();
let mut errors = serde_json::Map::new();

let mut last_falsy_if: Option<String> = None;
for field in fields.iter() {
if let Some(ref falsy_if_key) = last_falsy_if {
if let TicketSchemaFormField {
define: FormFieldDefine::IfEnd { key },
..
} = field
{
if key == falsy_if_key {
last_falsy_if = None;
}
}
continue;
}
if let TicketSchemaFormField {
define: FormFieldDefine::IfEqual { key, from, value },
..
} = field.clone()
{
let condition_result = match from {
FormFieldDefault::Static(from_value) => value.contains(&from_value),
FormFieldDefault::Dynamic {
value: from_value, ..
} => match from_value {
Some(from_value) => value.contains(&from_value),
None => false,
},
};
if !condition_result {
last_falsy_if = Some(key);
}
continue;
}
let user_value = match data.get::<String>(&field.key) {
Some(value) => value,
None => {
Expand Down Expand Up @@ -174,10 +207,11 @@ pub struct PartFormSchema {
}

impl PartFormSchema {
pub async fn upload_image(
pub async fn upload_image<'a>(
&self,
conn: &mut crate::DbConn,
data_folder: &State<DataFolder>,
i18n: &I18n<'a>,
mut temp_file: TempFile<'_>,
) -> Result<TicketFormImage, String> {
let PartFormSchema { field, .. } = self;
Expand Down Expand Up @@ -208,7 +242,7 @@ impl PartFormSchema {
} = field.define
{
if file_size > max_size {
return Err(format!("Image {} is too large", field.key));
return Err(i18n.tf("ticket.rules.image_too_large", &[("field", field.key.clone())]));
}

let file_content = get_file_content().await?;
Expand All @@ -219,39 +253,36 @@ impl PartFormSchema {

if let Some(min_width) = min_width {
if width < min_width {
return Err(format!("Image {} width is too small", field.key));
return Err(i18n.tf("ticket.rules.image_width_too_small", &[("field", field.key.clone())]));
}
}

if let Some(max_width) = max_width {
if width > max_width {
return Err(format!("Image {} width is too large", field.key));
return Err(i18n.tf("ticket.rules.image_width_too_large", &[("field", field.key.clone())]));
}
}

if let Some(min_height) = min_height {
if height < min_height {
return Err(format!("Image {} height is too small", field.key));
return Err(i18n.tf("ticket.rules.image_height_too_small", &[("field", field.key.clone())]));
}
}

if let Some(max_height) = max_height {
if height > max_height {
return Err(format!("Image {} height is too large", field.key));
return Err(i18n.tf("ticket.rules.image_height_too_large", &[("field", field.key.clone())]));
}
}

match mime {
Some(mime) => {
if !mimes.contains(&mime) {
return Err(format!(
"Image {} is not in the correct format",
field.key
));
return Err(i18n.tf("ticket.rules.invalid_image_type", &[("field", field.key.clone())]));
}
Ok((mime, (width, height), file_size))
}
_ => return Err(format!("Image {} is not in the correct format", field.key)),
_ => return Err(i18n.tf("ticket.rules.invalid_image_type", &[("field", field.key.clone())])),
}
};

Expand Down Expand Up @@ -311,10 +342,11 @@ impl PartFormSchema {
Err(format!("Field {} is not an image", field.key))
}

pub async fn upload_file(
pub async fn upload_file<'a>(
&self,
conn: &mut crate::DbConn,
data_folder: &State<DataFolder>,
i18n: &I18n<'a>,
mut temp_file: TempFile<'_>,
) -> Result<TicketFormFile, String> {
let PartFormSchema { field, .. } = self;
Expand All @@ -341,7 +373,7 @@ impl PartFormSchema {
} = field.define
{
if file_size > max_size {
return Err(format!("File {} is too large", field.key));
return Err(i18n.tf("ticket.rules.file_too_large", &[("field", field.key.clone())]));
}

let file_content = get_file_content().await?;
Expand All @@ -352,19 +384,16 @@ impl PartFormSchema {
match mime {
Some(mime) => {
if !mimes.contains(&mime) {
return Err(format!(
"File {} is not in the correct format",
field.key
));
return Err(i18n.tf("ticket.rules.invalid_file_type", &[("field", field.key.clone())]));
}
mime
}
_ => {
return Err(format!("File {} is not in the correct format", field.key))
return Err(i18n.tf("ticket.rules.invalid_file_type", &[("field", field.key.clone())]))
}
}
}
_ => return Err(format!("File {} is not in the correct format", field.key)),
_ => return Err(i18n.tf("ticket.rules.invalid_file_type", &[("field", field.key.clone())])),
};

let file_extension = &temp_file
Expand Down

0 comments on commit 73413e2

Please sign in to comment.