1
0
mirror of https://github.com/Bluemangoo/sekai-unpacker.git synced 2026-05-06 20:44:47 +08:00
2026-04-17 18:56:56 +08:00

93 lines
2.7 KiB
Rust

use crate::{CONFIG, SESSION_STORE};
use assets_updater::core::export_pipeline::{find_files, find_files_by_extensions};
use bytes::Bytes;
use common::http::DownloadRequest;
use common::stream::server_send_files;
use communicator::http::{json_from_request, send, send_error};
use h2::RecvStream;
use h2::server::SendResponse;
use http::{Request, Response};
use log::debug;
pub async fn download(
mut request: Request<RecvStream>,
mut send_response: SendResponse<Bytes>,
) -> Result<(), h2::Error> {
let body = json_from_request(&mut request).await;
if let Err(error) = body {
send_error(send_response, error);
return Ok(());
}
let req_body: DownloadRequest = body.unwrap();
let id = req_body.id;
let context = SESSION_STORE.get(&id).await;
if context.is_none() {
send(
send_response,
200,
"application/json",
serde_json::json!({
"msg": "invalid session id"
})
.to_string(),
);
return Ok(());
}
let context = context.unwrap();
let dir = context
.download(&req_body.task, &CONFIG.updater_config)
.await;
if let Err(error) = dir {
send_error(send_response, error.into());
return Ok(());
}
let (dir, single_file) = dir.unwrap();
let files = if context.sync_context.filters.file_ext.is_empty() {
find_files(&dir)
} else {
find_files_by_extensions(&dir, &context.sync_context.filters.file_ext)
};
if let Err(error) = files {
send_error(send_response, error.into());
return Ok(());
}
let files = files
.unwrap()
.iter()
.map(|p| {
let raw = p.clone();
let name = p.strip_prefix(&dir).unwrap();
debug!("{} {}", p.to_string_lossy(), single_file);
let path = if context.sync_context.exact_single_file_bundle
&& single_file
{
format!(
"{}/{}",
name.parent().unwrap().parent().unwrap().to_string_lossy(),
p.file_name().unwrap().to_string_lossy()
)
} else {
name.to_string_lossy().to_string()
};
(raw, path)
})
.collect::<Vec<_>>();
let response = Response::builder()
.status(200)
.header("content-type", "application/x-sekai-stream")
.body(())
.unwrap();
if let Ok(send_stream) = send_response.send_response(response, false) {
let _ = server_send_files(send_stream, &files).await;
}
let _ = std::fs::remove_file(dir);
Ok(())
}