Skip to main content

roblox_api/api/data/
mod.rs

1use reqwest::header::{self, HeaderValue};
2
3use crate::{AssetTypeId, Error, client::Client};
4
5pub const URL: &str = "https://data.roblox.com/data";
6
7//"https://data.roblox.com/ide/publish/UploadNewMesh" ?
8
9// perhaps we should have a "update" function too that doesn't take in all these parameters
10// apparently decals are also supported, i couldn't get it to work though
11//
12/// `id` can be set to None, or Some(0) to upload a new asset, using an existing `id` will overwrite the old asset
13/// on success RETURNS the new asset id
14pub async fn upload(
15    client: &mut Client,
16    id: Option<u64>,
17    name: &str,
18    description: &str,
19    asset_type: AssetTypeId,
20    group_id: Option<u64>,
21    genre: u8,
22    is_public: bool,
23    allow_comments: bool,
24    bytes: &[u8],
25) -> Result<u64, Error> {
26    let id = id.unwrap_or(0);
27    let genre_type_id = genre;
28
29    let mut url = format!("{URL}/upload.ashx?assetId={id}");
30    if let AssetTypeId::Model = asset_type {
31        url.push_str("&type=Model");
32    } else if let AssetTypeId::Place = asset_type {
33        url.push_str("&type=Place");
34    } else {
35        let asset_type_id = asset_type as u8;
36        url.push_str(&format!("&assetTypeId={asset_type_id}"));
37    }
38
39    if let Some(group_id) = group_id {
40        url.push_str(&format!("&groupId={group_id}"));
41    }
42
43    let mut headers = client.requestor.default_headers.clone();
44    headers.insert(
45        header::ACCEPT,
46        HeaderValue::from_str("application/json").unwrap(),
47    );
48
49    headers.insert(
50        header::CONTENT_TYPE,
51        HeaderValue::from_str("application/octect-stream").unwrap(),
52    );
53
54    headers.insert(
55        header::USER_AGENT,
56        HeaderValue::from_str("Roblox/WinInet").unwrap(),
57    );
58
59    let result = client
60        .requestor
61        .client
62        .post(url)
63        .query(&[
64            ("name", name),
65            ("description", description),
66            ("genreTypeId", &genre_type_id.to_string()),
67            ("isPublic", &is_public.to_string()),
68            ("allowComments", &allow_comments.to_string()),
69        ])
70        .headers(headers)
71        .body(bytes.to_owned())
72        .send()
73        .await;
74
75    let response = client.requestor.validate_response(result).await?;
76    let id: u64 = response.text().await.unwrap().parse().unwrap();
77    Ok(id)
78}