d_id/endpoints/resources/
mod.rs1pub mod credits;
2pub mod voices;
3pub mod images;
4pub mod audios;
5pub mod settings;
6
7use std::{fs::File, io::{self, Read, Write}};
8pub use crate::client::*;
9pub use crate::prelude::*;
10pub use serde::{Deserialize, Serialize};
11
12use rand::Rng;
13
14pub struct MultipartFormData {
15 pub boundary: String,
16 pub body: Vec<u8>,
17}
18
19impl MultipartFormData {
20 pub fn new() -> Self {
21 Self {
22 boundary: format!(
23 "-----------------------------{}", rand::thread_rng().gen::<u64>()),
24 body: Vec::new(),
25 }
26 }
27
28 pub fn add_text(&mut self, name: &str, value: &str) -> io::Result<()> {
29 write!(self.body, "--{}\r\n", self.boundary)?;
30 write!(self.body, "Content-Disposition: form-data; name=\"{}\"\r\n\r\n{}\r\n", name, value)?;
31 Ok(())
32 }
33
34 pub fn add_file(&mut self, mime_type: &str, name: &str, path: &str) -> io::Result<()> {
35 if !path.contains(".") {
36 return Err(io::Error::new(io::ErrorKind::Other, "Invalid file path"));
37 }
38 write!(self.body, "--{}\r\n", self.boundary)?;
39 write!(self.body, "Content-Disposition: form-data; name=\"{}\"; filename=\"{}\"\r\n", name, path)?;
40 write!(self.body, "Content-Type: {}\r\n\r\n", mime_type)?;
41 let mut file = File::open(path)?;
42 file.read_to_end(&mut self.body)?;
43 write!(self.body, "\r\n")?;
44
45
46 Ok(())
47 }
48
49 pub fn end_body(&mut self) -> io::Result<()> {
50 write!(self.body, "--{}--\r\n", self.boundary)?;
51 Ok(())
52 }
53
54}