rust-boss/src/services/npts.rs
2026-02-12 11:56:29 +00:00

202 lines
No EOL
6.4 KiB
Rust

use std::env;
use rocket::State;
use rocket::form::FromForm;
use rocket::http::Status;
use rocket::response::content::RawXml;
use serde::Serialize;
use crate::Pool;
use crate::database::{get_task, get_wup_task_files, get_wup_task_file};
use crate::models::task::Task;
use crate::models::file_wup::FileWUP;
use crate::models::file_wup::FileWUPAttributes;
#[derive(FromForm)]
struct QueryParams {
c: Option<String>,
l: Option<String>,
mode: Option<String>,
}
#[derive(Serialize)]
#[serde(rename_all = "PascalCase")]
struct FileWUPNotify {
new: String,
led: bool,
}
#[derive(Serialize)]
#[serde(rename_all = "PascalCase")]
struct FileWUPFormatted {
filename: String,
data_id: Option<i64>,
r#type: String,
url: Option<String>,
size: i64,
attributes: Option<FileWUPAttributes>,
notify: Option<FileWUPNotify>,
}
#[derive(Serialize)]
#[serde(rename_all = "PascalCase")]
struct TaskSheetFiles {
file: Vec<FileWUPFormatted>,
}
#[derive(Serialize)]
#[serde(rename_all = "PascalCase")]
struct TaskSheet {
title_id: String,
task_id: String,
service_status: String,
files: TaskSheetFiles,
}
fn build_file(task: Task, file: &FileWUP, attributes_mode: bool, cdn_url: &String) -> FileWUPFormatted {
let file = file.clone();
if attributes_mode {
FileWUPFormatted {
filename: file.name,
r#type: file.r#type,
size: file.size,
attributes: Some(FileWUPAttributes {
attribute1: file.attributes.attribute1.clone(),
attribute2: file.attributes.attribute2.clone(),
attribute3: file.attributes.attribute3.clone(),
description: file.attributes.description.clone(),
}),
data_id: None,
url: None,
notify: None,
}
} else {
let url = format!("{}/p01/data/1/{}/{}/{}", cdn_url, task.boss_app_id, file.data_id.unwrap(), file.hash);
FileWUPFormatted {
filename: file.name,
data_id: Some(file.data_id.unwrap()),
r#type: file.r#type,
url: Some(url),
size: file.size,
notify: Some(FileWUPNotify {
new: file.notify_on_new.join(","),
led: file.notify_led,
}),
attributes: None,
}
}
}
#[rocket::get("/p01/tasksheet/<id>/<boss_app_id>/<task_id>?<query..>")]
pub async fn tasksheet(pool: &State<Pool>, id: &str, boss_app_id: &str, task_id: &str, query: QueryParams) -> Result<RawXml<String>, Status> {
let pool = pool.inner();
let cdn_url = match env::var("BOSS_CDN_URL") {
Ok(url) => url,
Err(_) => return Err(Status::InternalServerError),
};
let country = query.c;
let language = query.l;
let mode = query.mode;
let task = match get_task(pool, boss_app_id.to_string(), task_id.to_string()).await {
Some(task) => task,
None => return Err(Status::NotFound)
};
let files = get_wup_task_files(pool, false, boss_app_id.to_string(), task_id.to_string(), country, language, None).await;
let tasksheet = TaskSheet{
title_id: task.title_id.clone(),
task_id: task.id.clone(),
service_status: task.status.clone(),
files: TaskSheetFiles{file: files.iter().map(|f| build_file(task.clone(), f, mode == Some("attr".to_string()), &cdn_url)).collect()},
};
let xml = quick_xml::se::to_string(&tasksheet);
match xml {
Ok(xml) => {
let xml = format!(r#"<?xml version="1.0" encoding="UTF-8"?>{}"#, xml);
Ok(RawXml(xml))
},
Err(_) => Err(Status::InternalServerError),
}
}
#[rocket::get("/p01/tasksheet/<id>/<task_id>?<query..>")]
pub async fn tasksheet_no_boss_app_id(pool: &State<Pool>, id: &str, task_id: &str, query: QueryParams) -> Result<RawXml<String>, Status> {
let pool = pool.inner();
let cdn_url = match env::var("BOSS_CDN_URL") {
Ok(url) => url,
Err(_) => return Err(Status::InternalServerError),
};
let country = query.c;
let language = query.l;
let mode = query.mode;
let boss_app_id = "";
let task = match get_task(pool, boss_app_id.to_string(), task_id.to_string()).await {
Some(task) => task,
None => return Err(Status::NotFound)
};
let files = get_wup_task_files(pool, false, boss_app_id.to_string(), task_id.to_string(), country, language, None).await;
let tasksheet = TaskSheet{
title_id: task.title_id.clone(),
task_id: task.id.clone(),
service_status: task.status.clone(),
files: TaskSheetFiles{file: files.iter().map(|f| build_file(task.clone(), f, mode == Some("attr".to_string()), &cdn_url)).collect()},
};
let xml = quick_xml::se::to_string(&tasksheet);
match xml {
Ok(xml) => {
let xml = format!(r#"<?xml version="1.0" encoding="UTF-8"?>{}"#, xml);
Ok(RawXml(xml))
},
Err(_) => Err(Status::InternalServerError),
}
}
#[rocket::get("/p01/tasksheet/<id>/<boss_app_id>/<task_id>/<file_name>?<query..>")]
pub async fn tasksheet_file(pool: &State<Pool>, id: &str, boss_app_id: &str, task_id: &str, file_name: &str, query: QueryParams) -> Result<RawXml<String>, Status> {
let pool = pool.inner();
let cdn_url = match env::var("BOSS_CDN_URL") {
Ok(url) => url,
Err(_) => return Err(Status::InternalServerError),
};
let country = query.c;
let language = query.l;
let mode = query.mode;
let task = match get_task(pool, boss_app_id.to_string(), task_id.to_string()).await {
Some(task) => task,
None => return Err(Status::NotFound)
};
let file = match get_wup_task_file(pool, false, boss_app_id.to_string(), task_id.to_string(), file_name.to_string(), country, language, None).await {
Some(file) => file,
None => return Err(Status::NotFound),
};
let tasksheet = TaskSheet{
title_id: task.title_id.clone(),
task_id: task.id.clone(),
service_status: task.status.clone(),
files: TaskSheetFiles{file: vec![build_file(task.clone(), &file, mode == Some("attr".to_string()), &cdn_url)]},
};
let xml = quick_xml::se::to_string(&tasksheet);
match xml {
Ok(xml) => {
let xml = format!(r#"<?xml version="1.0" encoding="UTF-8"?>{}"#, xml);
Ok(RawXml(xml))
},
Err(_) => Err(Status::InternalServerError),
}
}