Revert to using Reqwest, reimplement upload command

This commit is contained in:
Lucien Greathouse
2019-08-29 16:31:42 -07:00
parent ee3fed97e0
commit 4e512b7023
3 changed files with 467 additions and 3 deletions

View File

@@ -1,15 +1,36 @@
use std::path::PathBuf;
use std::{collections::HashMap, path::PathBuf};
use failure::Fail;
use rbx_dom_weak::{RbxInstanceProperties, RbxTree};
use reqwest::header::{ACCEPT, CONTENT_TYPE, COOKIE, USER_AGENT};
use crate::auth_cookie::get_auth_cookie;
use crate::{
auth_cookie::get_auth_cookie,
imfs::new::{Imfs, RealFetcher, WatchMode},
snapshot::{apply_patch_set, compute_patch_set},
snapshot_middleware::snapshot_from_imfs,
};
#[derive(Debug, Fail)]
pub enum UploadError {
#[fail(display = "Rojo could not find your Roblox auth cookie. Please pass one via --cookie.")]
NeedAuthCookie,
#[fail(display = "XML model file encode error: {}", _0)]
XmlModelEncode(#[fail(cause)] rbx_xml::EncodeError),
#[fail(display = "HTTP error: {}", _0)]
Http(#[fail(cause)] reqwest::Error),
#[fail(display = "Roblox API error: {}", _0)]
RobloxApi(String),
}
impl_from!(UploadError {
rbx_xml::EncodeError => XmlModelEncode,
reqwest::Error => Http,
});
#[derive(Debug)]
pub struct UploadOptions<'a> {
pub fuzzy_project_path: PathBuf,
@@ -24,5 +45,59 @@ pub fn upload(options: UploadOptions) -> Result<(), UploadError> {
.or_else(get_auth_cookie)
.ok_or(UploadError::NeedAuthCookie)?;
unimplemented!("TODO: Reimplement upload command");
let mut tree = RbxTree::new(RbxInstanceProperties {
name: "ROOT".to_owned(),
class_name: "Folder".to_owned(),
properties: HashMap::new(),
});
let root_id = tree.get_root_id();
log::trace!("Constructing in-memory filesystem");
let mut imfs = Imfs::new(RealFetcher::new(WatchMode::Disabled));
log::trace!("Reading project root");
let entry = imfs
.get(&options.fuzzy_project_path)
.expect("could not get project path");
log::trace!("Generating snapshot of instances from IMFS");
let snapshot = snapshot_from_imfs(&mut imfs, &entry)
.expect("snapshot failed")
.expect("snapshot did not return an instance");
log::trace!("Computing patch set");
let patch_set = compute_patch_set(&snapshot, &tree, root_id);
log::trace!("Applying patch set");
apply_patch_set(&mut tree, &patch_set);
let root_id = tree.get_root_id();
let mut buffer = Vec::new();
let config = rbx_xml::EncodeOptions::new()
.property_behavior(rbx_xml::EncodePropertyBehavior::WriteUnknown);
rbx_xml::to_writer(&mut buffer, &tree, &[root_id], config)?;
let url = format!(
"https://data.roblox.com/Data/Upload.ashx?assetid={}",
options.asset_id
);
let client = reqwest::Client::new();
let mut response = client
.post(&url)
.header(COOKIE, format!(".ROBLOSECURITY={}", &cookie))
.header(USER_AGENT, "Roblox/WinInet")
.header("Requester", "Client")
.header(CONTENT_TYPE, "application/xml")
.header(ACCEPT, "application/json")
.body(buffer)
.send()?;
if !response.status().is_success() {
return Err(UploadError::RobloxApi(response.text()?));
}
Ok(())
}