d_id/endpoints/resources/
images.rs

1// File: images.rs
2// Path: src/endpoints/resources/voices.rs
3
4use super::*;
5
6const IMAGES_PATH: &str = "/images";
7
8// This temporary URL should be provided when creating an animation via the /animations endpoint.
9#[derive(Serialize, Deserialize, Debug)]
10pub struct ImageResponse {
11    pub id: String,
12    pub url: String,
13}
14
15/// Upload an image to a temporary storage before creating an animation.
16/// Supported mime types: image/jpeg,image/png
17/// Storage duration: 24-48H
18// TODO: Cover all body params [https://docs.d-id.com/reference/upload-an-image]
19pub async fn upload_image_by_file(path: &str) -> Result<ImageResponse> {
20    let mime_subtype = path.split(".").last().ok_or("Invalid file path")?;
21    let mut form = MultipartFormData::new();
22    form.add_file(&format!("image/{}", mime_subtype), "image", path)?;
23    form.end_body()?;
24
25    let c = ClientBuilder::new()?
26        .method(POST)?
27        .path(IMAGES_PATH)?
28        .header(ACCEPT, APPLICATION_JSON)?
29        .header(CONTENT_TYPE, &format!("{}{}", MULTIPART_FORM_DATA_BOUNDARY, form.boundary))?
30        .build()?;
31
32    let resp = c.send_request(Full::<Bytes>::new(form.body.into())).await?;
33
34    let json = serde_json::from_slice::<ImageResponse>(&resp.as_ref())?;
35
36    Ok(json)
37}
38
39pub async fn delete_image(id: &str) -> Result<()> {
40    let c = ClientBuilder::new()?
41        .method(DELETE)?
42        .path(&format!("{}/{}", IMAGES_PATH, id))?
43        .header(CONTENT_TYPE, APPLICATION_JSON)?
44        .build()?;
45
46    let _resp = c.send_request(Empty::<Bytes>::new()).await?;
47
48    Ok(())
49}
50