validation: factor out asset download

This commit is contained in:
Quaternions 2025-04-09 18:16:05 -07:00
parent e2c72c90c7
commit 1d409218a5
Signed by: Quaternions
GPG Key ID: D0DF5964F79AC131
6 changed files with 47 additions and 56 deletions

@ -1,3 +1,4 @@
use crate::download::download_asset_version;
use crate::rbx_util::{get_root_instance,get_mapinfo,read_dom,MapInfo,ReadDomError,GetRootInstanceError,ParseGameIDError};
#[allow(dead_code)]
@ -5,11 +6,9 @@ use crate::rbx_util::{get_root_instance,get_mapinfo,read_dom,MapInfo,ReadDomErro
pub enum Error{
CreatorTypeMustBeUser,
ModelInfoDownload(rbx_asset::cloud::GetError),
ModelLocationDownload(rbx_asset::cloud::GetError),
NonFreeModel,
ModelFileDownload(rbx_asset::cloud::GetError),
ParseUserID(core::num::ParseIntError),
ParseModelVersion(core::num::ParseIntError),
Download(crate::download::Error),
ModelFileDecode(ReadDomError),
GetRootInstance(GetRootInstanceError),
ParseGameID(ParseGameIDError),
@ -49,19 +48,11 @@ impl crate::message_handler::MessageHandler{
let user_id:u64=user_id_string.parse().map_err(Error::ParseUserID)?;
let asset_version=info.revisionId.parse().map_err(Error::ParseModelVersion)?;
// download the location of the map model
let location=self.cloud_context.get_asset_version_location(rbx_asset::cloud::GetAssetVersionRequest{
// download the map model
let model_data=download_asset_version(&self.cloud_context,rbx_asset::cloud::GetAssetVersionRequest{
asset_id:create_info.ModelID,
version:asset_version,
}).await.map_err(Error::ModelLocationDownload)?;
// if the location does not exist, you are not allowed to donwload it
let Some(location)=location.location else{
return Err(Error::NonFreeModel);
};
// download the map model
let model_data=self.cloud_context.get_asset(&location).await.map_err(Error::ModelFileDownload)?;
}).await.map_err(Error::Download)?;
// decode dom (slow!)
let dom=read_dom(std::io::Cursor::new(model_data)).map_err(Error::ModelFileDecode)?;

@ -0,0 +1,26 @@
#[allow(dead_code)]
#[derive(Debug)]
pub enum Error{
ModelLocationDownload(rbx_asset::cloud::GetError),
NonFreeModel,
ModelFileDownload(rbx_asset::cloud::GetError),
}
impl std::fmt::Display for Error{
fn fmt(&self,f:&mut std::fmt::Formatter<'_>)->std::fmt::Result{
write!(f,"{self:?}")
}
}
impl std::error::Error for Error{}
pub async fn download_asset_version(cloud_context:&rbx_asset::cloud::Context,request:rbx_asset::cloud::GetAssetVersionRequest)->Result<Vec<u8>,Error>{
// download the location of the map model
let location=cloud_context.get_asset_version_location(request).await.map_err(Error::ModelLocationDownload)?;
// if the location does not exist, you are not allowed to download it
let location=location.location.ok_or(Error::NonFreeModel)?;
// download the map model
let model_data=cloud_context.get_asset(&location).await.map_err(Error::ModelFileDownload)?;
Ok(model_data)
}

@ -4,6 +4,7 @@ mod rbx_util;
mod message_handler;
mod nats_types;
mod types;
mod download;
mod create;
mod create_mapfix;
mod create_submission;

@ -1,11 +1,10 @@
use crate::download::download_asset_version;
use crate::nats_types::UploadMapfixRequest;
#[allow(dead_code)]
#[derive(Debug)]
pub enum Error{
GetLocation(rbx_asset::cloud::GetError),
NonFreeModel,
Get(rbx_asset::cloud::GetError),
Download(crate::download::Error),
Json(serde_json::Error),
Upload(rbx_asset::cookie::UploadError),
ApiActionMapfixUploaded(submissions_api::Error),
@ -19,19 +18,11 @@ impl std::error::Error for Error{}
impl crate::message_handler::MessageHandler{
pub async fn upload_mapfix(&self,upload_info:UploadMapfixRequest)->Result<(),Error>{
// download the location of the map model
let location=self.cloud_context.get_asset_version_location(rbx_asset::cloud::GetAssetVersionRequest{
// download the map model
let model_data=download_asset_version(&self.cloud_context,rbx_asset::cloud::GetAssetVersionRequest{
asset_id:upload_info.ModelID,
version:upload_info.ModelVersion,
}).await.map_err(Error::GetLocation)?;
// if the location does not exist, you are not allowed to donwload it
let Some(location)=location.location else{
return Err(Error::NonFreeModel);
};
// download the map model
let model_data=self.cloud_context.get_asset(&location).await.map_err(Error::Get)?;
}).await.map_err(Error::Download)?;
// upload the map to the strafesnet group
let _upload_response=self.cookie_context.upload(rbx_asset::cookie::UploadRequest{

@ -1,11 +1,10 @@
use crate::download::download_asset_version;
use crate::nats_types::UploadSubmissionRequest;
#[allow(dead_code)]
#[derive(Debug)]
pub enum Error{
GetLocation(rbx_asset::cloud::GetError),
NonFreeModel,
Get(rbx_asset::cloud::GetError),
Download(crate::download::Error),
Json(serde_json::Error),
Create(rbx_asset::cookie::CreateError),
SystemTime(std::time::SystemTimeError),
@ -20,19 +19,11 @@ impl std::error::Error for Error{}
impl crate::message_handler::MessageHandler{
pub async fn upload_submission(&self,upload_info:UploadSubmissionRequest)->Result<(),Error>{
// download the location of the map model
let location=self.cloud_context.get_asset_version_location(rbx_asset::cloud::GetAssetVersionRequest{
// download the map model
let model_data=download_asset_version(&self.cloud_context,rbx_asset::cloud::GetAssetVersionRequest{
asset_id:upload_info.ModelID,
version:upload_info.ModelVersion,
}).await.map_err(Error::GetLocation)?;
// if the location does not exist, you are not allowed to donwload it
let Some(location)=location.location else{
return Err(Error::NonFreeModel);
};
// download the map model
let model_data=self.cloud_context.get_asset(&location).await.map_err(Error::Get)?;
}).await.map_err(Error::Download)?;
// upload the map to the strafesnet group
let upload_response=self.cookie_context.create(rbx_asset::cookie::CreateRequest{

@ -1,6 +1,7 @@
use futures::TryStreamExt;
use submissions_api::types::ResourceType;
use crate::download::download_asset_version;
use crate::rbx_util::{class_is_a,read_dom,ReadDomError};
use crate::types::ResourceID;
@ -36,9 +37,7 @@ pub enum Error{
ScriptFlaggedIllegalKeyword(String),
ScriptBlocked(Option<submissions_api::types::ScriptID>),
ScriptNotYetReviewed(Option<submissions_api::types::ScriptID>),
ModelLocationDownload(rbx_asset::cloud::GetError),
NonFreeModel,
ModelFileDownload(rbx_asset::cloud::GetError),
Download(crate::download::Error),
ModelFileDecode(ReadDomError),
ApiGetScriptPolicyFromHash(submissions_api::types::SingleItemError),
ApiGetScript(submissions_api::Error),
@ -91,19 +90,11 @@ impl From<crate::nats_types::ValidateSubmissionRequest> for ValidateRequest{
impl crate::message_handler::MessageHandler{
pub async fn validate_inner(&self,validate_info:ValidateRequest)->Result<(),Error>{
// download the location of the map model
let location=self.cloud_context.get_asset_version_location(rbx_asset::cloud::GetAssetVersionRequest{
// download the map model
let model_data=download_asset_version(&self.cloud_context,rbx_asset::cloud::GetAssetVersionRequest{
asset_id:validate_info.ModelID,
version:validate_info.ModelVersion,
}).await.map_err(Error::ModelLocationDownload)?;
// if the location does not exist, you are not allowed to donwload it
let Some(location)=location.location else{
return Err(Error::NonFreeModel);
};
// download the map model
let model_data=self.cloud_context.get_asset(&location).await.map_err(Error::ModelFileDownload)?;
}).await.map_err(Error::Download)?;
// decode dom (slow!)
let mut dom=read_dom(std::io::Cursor::new(model_data)).map_err(Error::ModelFileDecode)?;