优化大文件稳定并发传输与打包流程
This commit is contained in:
+29
-2
@@ -34,6 +34,7 @@ pub enum ReceiverEvent {
|
||||
|
||||
#[derive(Debug)]
|
||||
struct UploadSession {
|
||||
upload_key: Option<String>,
|
||||
device: String,
|
||||
file_name: String,
|
||||
size: u64,
|
||||
@@ -254,9 +255,34 @@ fn init_parallel_upload(
|
||||
let total_chunks_u64 = if size == 0 { 0 } else { (size + chunk_size - 1) / chunk_size };
|
||||
if total_chunks_u64 > u32::MAX as u64 { bail!("文件分块数过大") }
|
||||
let total_chunks = total_chunks_u64 as u32;
|
||||
let upload_key = headers.get("x-qft-upload-key")
|
||||
.map(|value| value.trim().to_owned())
|
||||
.filter(|value| !value.is_empty() && value.len() <= 128);
|
||||
|
||||
let mut map = sessions.lock().map_err(|_| anyhow::anyhow!("上传会话锁损坏"))?;
|
||||
// init is idempotent: if the response packet was lost, the Android client
|
||||
// may repeat init. Return the existing session so already received chunks
|
||||
// and the preallocated file are preserved instead of leaking a second part.
|
||||
for (existing_id, existing) in map.iter() {
|
||||
if let Ok(session) = existing.lock() {
|
||||
if upload_key.is_some() && session.upload_key == upload_key {
|
||||
let missing: Vec<usize> = session.received.iter().enumerate()
|
||||
.filter_map(|(index, received)| if *received { None } else { Some(index) })
|
||||
.collect();
|
||||
let body = json!({
|
||||
"ok": true,
|
||||
"upload_id": existing_id,
|
||||
"chunk_size": session.chunk_size,
|
||||
"total_chunks": session.total_chunks,
|
||||
"resumed": true,
|
||||
"missing": missing,
|
||||
}).to_string();
|
||||
return write_response(stream, 200, "application/json", body.as_bytes());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let upload_id = random_id();
|
||||
let mut map = sessions.lock().map_err(|_| anyhow::anyhow!("上传会话锁损坏"))?;
|
||||
let final_path = unique_path_reserved(save_dir, &file_name, map.values());
|
||||
let final_name = final_path.file_name().and_then(|v| v.to_str()).unwrap_or("qft-upload.bin");
|
||||
let part_path = final_path.with_file_name(format!("{final_name}.{upload_id}.qftpart"));
|
||||
@@ -266,6 +292,7 @@ fn init_parallel_upload(
|
||||
drop(part);
|
||||
|
||||
let session = UploadSession {
|
||||
upload_key,
|
||||
device: device.to_owned(),
|
||||
file_name: file_name.clone(),
|
||||
size,
|
||||
@@ -338,7 +365,7 @@ fn receive_parallel_chunk(
|
||||
let mut out = OpenOptions::new().write(true).open(&part_path)?;
|
||||
out.seek(SeekFrom::Start(offset))?;
|
||||
copy_exact(reader, &mut out, content_len)?;
|
||||
out.flush()?;
|
||||
out.sync_data()?;
|
||||
|
||||
let received = {
|
||||
let mut session = session_arc.lock().map_err(|_| anyhow::anyhow!("上传会话损坏"))?;
|
||||
|
||||
Reference in New Issue
Block a user