refactor: extract platform media crates
This commit is contained in:
111
server-rs/crates/platform-hyper3d/src/request/options.rs
Normal file
111
server-rs/crates/platform-hyper3d/src/request/options.rs
Normal file
@@ -0,0 +1,111 @@
|
||||
use crate::{
|
||||
error::Hyper3dError,
|
||||
request::normalize::{normalize_addons, normalize_bbox_condition, normalize_enum},
|
||||
types::{
|
||||
DEFAULT_GEOMETRY_FILE_FORMAT, DEFAULT_MATERIAL, DEFAULT_MESH_MODE, DEFAULT_QUALITY,
|
||||
SubmitOptions,
|
||||
},
|
||||
};
|
||||
|
||||
pub(crate) fn build_submit_options_from_text(
|
||||
payload: &shared_contracts::hyper3d::Hyper3dTextToModelRequest,
|
||||
) -> Result<SubmitOptions, Hyper3dError> {
|
||||
SubmitOptions::new(
|
||||
payload.seed,
|
||||
payload.geometry_file_format.as_deref(),
|
||||
payload.material.as_deref(),
|
||||
payload.quality.as_deref(),
|
||||
payload.mesh_mode.as_deref(),
|
||||
payload.addons.clone(),
|
||||
payload.bbox_condition.clone(),
|
||||
payload.preview_render,
|
||||
)
|
||||
}
|
||||
|
||||
pub(crate) fn build_submit_options_from_image(
|
||||
payload: &shared_contracts::hyper3d::Hyper3dImageToModelRequest,
|
||||
) -> Result<SubmitOptions, Hyper3dError> {
|
||||
SubmitOptions::new(
|
||||
payload.seed,
|
||||
payload.geometry_file_format.as_deref(),
|
||||
payload.material.as_deref(),
|
||||
payload.quality.as_deref(),
|
||||
payload.mesh_mode.as_deref(),
|
||||
payload.addons.clone(),
|
||||
payload.bbox_condition.clone(),
|
||||
payload.preview_render,
|
||||
)
|
||||
}
|
||||
|
||||
impl SubmitOptions {
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
pub(crate) fn new(
|
||||
seed: Option<u32>,
|
||||
geometry_file_format: Option<&str>,
|
||||
material: Option<&str>,
|
||||
quality: Option<&str>,
|
||||
mesh_mode: Option<&str>,
|
||||
addons: Vec<String>,
|
||||
bbox_condition: Option<Vec<f32>>,
|
||||
preview_render: Option<bool>,
|
||||
) -> Result<Self, Hyper3dError> {
|
||||
Ok(Self {
|
||||
seed,
|
||||
geometry_file_format: normalize_enum(
|
||||
geometry_file_format,
|
||||
DEFAULT_GEOMETRY_FILE_FORMAT,
|
||||
&["glb", "usdz", "fbx", "obj", "stl"],
|
||||
"geometryFileFormat",
|
||||
)?,
|
||||
material: normalize_enum(
|
||||
material,
|
||||
DEFAULT_MATERIAL,
|
||||
&["PBR", "Shaded", "All"],
|
||||
"material",
|
||||
)?,
|
||||
quality: normalize_enum(
|
||||
quality,
|
||||
DEFAULT_QUALITY,
|
||||
&["high", "medium", "low", "extra-low"],
|
||||
"quality",
|
||||
)?,
|
||||
mesh_mode: normalize_enum(mesh_mode, DEFAULT_MESH_MODE, &["Quad", "Raw"], "meshMode")?,
|
||||
addons: normalize_addons(addons)?,
|
||||
bbox_condition: normalize_bbox_condition(bbox_condition)?,
|
||||
preview_render: preview_render.unwrap_or(true),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn build_common_submit_fields(
|
||||
form: reqwest::multipart::Form,
|
||||
options: &SubmitOptions,
|
||||
) -> Result<reqwest::multipart::Form, Hyper3dError> {
|
||||
let mut form = form
|
||||
.text(
|
||||
"geometry_file_format",
|
||||
options.geometry_file_format.to_string(),
|
||||
)
|
||||
.text("material", options.material.to_string())
|
||||
.text("quality", options.quality.to_string())
|
||||
.text("mesh_mode", options.mesh_mode.to_string())
|
||||
.text("preview_render", options.preview_render.to_string());
|
||||
if let Some(seed) = options.seed {
|
||||
form = form.text("seed", seed.to_string());
|
||||
}
|
||||
for addon in &options.addons {
|
||||
form = form.text("addons", addon.to_string());
|
||||
}
|
||||
if let Some(bbox_condition) = &options.bbox_condition {
|
||||
form = form.text(
|
||||
"bbox_condition",
|
||||
serde_json::to_string(bbox_condition).map_err(|error| {
|
||||
Hyper3dError::invalid_request(
|
||||
Some("bboxCondition"),
|
||||
format!("bboxCondition 序列化失败:{error}"),
|
||||
)
|
||||
})?,
|
||||
);
|
||||
}
|
||||
Ok(form)
|
||||
}
|
||||
Reference in New Issue
Block a user