V0.3.0版本优化传输速度和提示
This commit is contained in:
@@ -0,0 +1,540 @@
|
||||
use anyhow::{bail, Context, Result};
|
||||
use base64::{engine::general_purpose::URL_SAFE_NO_PAD, Engine as _};
|
||||
use rand::RngCore;
|
||||
use serde_json::json;
|
||||
use sha2::{Digest, Sha256};
|
||||
use std::{
|
||||
collections::HashMap,
|
||||
fs::{self, File, OpenOptions},
|
||||
io::{BufRead, BufReader, Read, Seek, SeekFrom, Write},
|
||||
net::{TcpListener, TcpStream, UdpSocket},
|
||||
path::{Path, PathBuf},
|
||||
sync::{
|
||||
atomic::{AtomicBool, Ordering},
|
||||
mpsc::Sender,
|
||||
Arc, Mutex,
|
||||
},
|
||||
thread::{self, JoinHandle},
|
||||
time::{Duration, Instant},
|
||||
};
|
||||
|
||||
pub const PREFERRED_PORT: u16 = 39393;
|
||||
pub const DISCOVERY_PORT: u16 = 39392;
|
||||
pub const PARALLEL_CHUNK_SIZE: u64 = 8 * 1024 * 1024;
|
||||
const IO_BUFFER: usize = 1024 * 1024;
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub enum ReceiverEvent {
|
||||
ClientConnected { device: String },
|
||||
UploadStarted { device: String, file_name: String, size: u64 },
|
||||
UploadProgress { device: String, file_name: String, received: u64, size: u64 },
|
||||
UploadCompleted { device: String, file_name: String, saved_path: PathBuf, sha256: String },
|
||||
Error(String),
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
struct UploadSession {
|
||||
device: String,
|
||||
file_name: String,
|
||||
size: u64,
|
||||
chunk_size: u64,
|
||||
total_chunks: u32,
|
||||
part_path: PathBuf,
|
||||
final_path: PathBuf,
|
||||
received: Vec<bool>,
|
||||
received_bytes: u64,
|
||||
}
|
||||
|
||||
type SessionMap = Arc<Mutex<HashMap<String, Arc<Mutex<UploadSession>>>>>;
|
||||
|
||||
pub struct ReceiverServer {
|
||||
pub port: u16,
|
||||
stop: Arc<AtomicBool>,
|
||||
tcp_thread: Option<JoinHandle<()>>,
|
||||
udp_thread: Option<JoinHandle<()>>,
|
||||
}
|
||||
|
||||
impl ReceiverServer {
|
||||
pub fn start(save_dir: PathBuf, session_token: String, events: Sender<ReceiverEvent>) -> Result<Self> {
|
||||
fs::create_dir_all(&save_dir).context("创建接收目录失败")?;
|
||||
let listener = TcpListener::bind(("0.0.0.0", PREFERRED_PORT))
|
||||
.or_else(|_| TcpListener::bind(("0.0.0.0", 0)))
|
||||
.context("启动本地接收端口失败")?;
|
||||
let port = listener.local_addr()?.port();
|
||||
listener.set_nonblocking(true)?;
|
||||
|
||||
let stop = Arc::new(AtomicBool::new(false));
|
||||
let sessions: SessionMap = Arc::new(Mutex::new(HashMap::new()));
|
||||
|
||||
let stop_tcp = stop.clone();
|
||||
let sessions_tcp = sessions.clone();
|
||||
let token_tcp = session_token.clone();
|
||||
let dir_tcp = save_dir.clone();
|
||||
let events_tcp = events.clone();
|
||||
let tcp_thread = thread::spawn(move || {
|
||||
while !stop_tcp.load(Ordering::Relaxed) {
|
||||
match listener.accept() {
|
||||
Ok((stream, _)) => {
|
||||
let dir = dir_tcp.clone();
|
||||
let token = token_tcp.clone();
|
||||
let tx = events_tcp.clone();
|
||||
let sessions = sessions_tcp.clone();
|
||||
thread::spawn(move || {
|
||||
if let Err(e) = handle_client(stream, &dir, &token, &tx, &sessions) {
|
||||
let _ = tx.send(ReceiverEvent::Error(format!("接收失败:{e:#}")));
|
||||
}
|
||||
});
|
||||
}
|
||||
Err(e) if e.kind() == std::io::ErrorKind::WouldBlock => {
|
||||
thread::sleep(Duration::from_millis(12));
|
||||
}
|
||||
Err(e) => {
|
||||
let _ = events_tcp.send(ReceiverEvent::Error(format!("监听端口失败:{e}")));
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
let stop_udp = stop.clone();
|
||||
let token_udp = session_token;
|
||||
let udp_thread = thread::spawn(move || {
|
||||
let socket = match UdpSocket::bind(("0.0.0.0", DISCOVERY_PORT)) {
|
||||
Ok(v) => v,
|
||||
Err(e) => {
|
||||
let _ = events.send(ReceiverEvent::Error(format!(
|
||||
"快速发现端口 {DISCOVERY_PORT} 不可用:{e}。仍可使用网关/IP 探测。"
|
||||
)));
|
||||
return;
|
||||
}
|
||||
};
|
||||
let _ = socket.set_read_timeout(Some(Duration::from_millis(250)));
|
||||
let expected = format!("QFTDISC2:{token_udp}");
|
||||
let response = format!("QFTHERE2:{token_udp}:{port}");
|
||||
let mut buf = [0u8; 512];
|
||||
while !stop_udp.load(Ordering::Relaxed) {
|
||||
match socket.recv_from(&mut buf) {
|
||||
Ok((n, peer)) => {
|
||||
if std::str::from_utf8(&buf[..n]).ok() == Some(expected.as_str()) {
|
||||
let _ = socket.send_to(response.as_bytes(), peer);
|
||||
}
|
||||
}
|
||||
Err(e) if matches!(e.kind(), std::io::ErrorKind::WouldBlock | std::io::ErrorKind::TimedOut) => {}
|
||||
Err(_) => break,
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
Ok(Self {
|
||||
port,
|
||||
stop,
|
||||
tcp_thread: Some(tcp_thread),
|
||||
udp_thread: Some(udp_thread),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for ReceiverServer {
|
||||
fn drop(&mut self) {
|
||||
self.stop.store(true, Ordering::Relaxed);
|
||||
if let Some(handle) = self.tcp_thread.take() {
|
||||
let _ = handle.join();
|
||||
}
|
||||
if let Some(handle) = self.udp_thread.take() {
|
||||
let _ = handle.join();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn handle_client(
|
||||
mut stream: TcpStream,
|
||||
save_dir: &Path,
|
||||
token: &str,
|
||||
events: &Sender<ReceiverEvent>,
|
||||
sessions: &SessionMap,
|
||||
) -> Result<()> {
|
||||
stream.set_nodelay(true).ok();
|
||||
stream.set_read_timeout(Some(Duration::from_secs(120)))?;
|
||||
stream.set_write_timeout(Some(Duration::from_secs(120)))?;
|
||||
let cloned = stream.try_clone()?;
|
||||
let mut reader = BufReader::with_capacity(64 * 1024, cloned);
|
||||
|
||||
let mut request_line = String::new();
|
||||
reader.read_line(&mut request_line)?;
|
||||
if request_line.len() > 8192 { bail!("请求行过长") }
|
||||
let mut headers = HashMap::<String, String>::new();
|
||||
loop {
|
||||
let mut line = String::new();
|
||||
let n = reader.read_line(&mut line)?;
|
||||
if n == 0 { bail!("连接提前关闭") }
|
||||
if line == "\r\n" || line == "\n" { break }
|
||||
if line.len() > 16 * 1024 { bail!("请求头过长") }
|
||||
if let Some((k, v)) = line.split_once(':') {
|
||||
headers.insert(k.trim().to_ascii_lowercase(), v.trim().to_owned());
|
||||
}
|
||||
}
|
||||
|
||||
let mut parts = request_line.split_whitespace();
|
||||
let method = parts.next().unwrap_or("");
|
||||
let path = parts.next().unwrap_or("");
|
||||
|
||||
let supplied = headers.get("x-qft-token").map(String::as_str).unwrap_or("");
|
||||
if supplied != token {
|
||||
return write_response(&mut stream, 403, "text/plain", b"invalid session");
|
||||
}
|
||||
|
||||
let device = decode_header(headers.get("x-qft-device")).unwrap_or_else(|| "Android".to_owned());
|
||||
let _ = events.send(ReceiverEvent::ClientConnected { device: device.clone() });
|
||||
|
||||
if method == "GET" && path == "/qft/ping" {
|
||||
let body = json!({"ok": true, "protocol": 2, "parallel": true}).to_string();
|
||||
return write_response(&mut stream, 200, "application/json", body.as_bytes());
|
||||
}
|
||||
|
||||
if method == "POST" && path == "/qft/upload/init" {
|
||||
return init_parallel_upload(&mut stream, save_dir, &headers, &device, events, sessions);
|
||||
}
|
||||
|
||||
if method == "POST" && path.starts_with("/qft/upload/chunk/") {
|
||||
return receive_parallel_chunk(&mut stream, &mut reader, path, &headers, events, sessions);
|
||||
}
|
||||
|
||||
if method == "POST" && path.starts_with("/qft/upload/finish/") {
|
||||
return finish_parallel_upload(&mut stream, path, events, sessions);
|
||||
}
|
||||
|
||||
// Compatibility path for old clients and content providers that cannot seek.
|
||||
if method == "POST" && path == "/qft/upload" {
|
||||
return receive_legacy_stream(&mut stream, &mut reader, save_dir, &headers, &device, events);
|
||||
}
|
||||
|
||||
write_response(&mut stream, 404, "text/plain", b"not found")
|
||||
}
|
||||
|
||||
fn init_parallel_upload(
|
||||
stream: &mut TcpStream,
|
||||
save_dir: &Path,
|
||||
headers: &HashMap<String, String>,
|
||||
device: &str,
|
||||
events: &Sender<ReceiverEvent>,
|
||||
sessions: &SessionMap,
|
||||
) -> Result<()> {
|
||||
let file_name = decode_header(headers.get("x-qft-filename"))
|
||||
.map(sanitize_file_name)
|
||||
.filter(|s| !s.is_empty())
|
||||
.unwrap_or_else(|| "qft-upload.bin".to_owned());
|
||||
let size: u64 = headers.get("x-qft-size")
|
||||
.context("缺少 X-QFT-Size")?
|
||||
.parse().context("X-QFT-Size 无效")?;
|
||||
let chunk_size = headers.get("x-qft-chunk-size")
|
||||
.and_then(|v| v.parse::<u64>().ok())
|
||||
.map(|v| v.clamp(1024 * 1024, 16 * 1024 * 1024))
|
||||
.unwrap_or(PARALLEL_CHUNK_SIZE);
|
||||
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_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"));
|
||||
let part = File::create(&part_path)
|
||||
.with_context(|| format!("创建临时文件 {}", part_path.display()))?;
|
||||
part.set_len(size)?;
|
||||
drop(part);
|
||||
|
||||
let session = UploadSession {
|
||||
device: device.to_owned(),
|
||||
file_name: file_name.clone(),
|
||||
size,
|
||||
chunk_size,
|
||||
total_chunks,
|
||||
part_path,
|
||||
final_path,
|
||||
received: vec![false; total_chunks as usize],
|
||||
received_bytes: 0,
|
||||
};
|
||||
map.insert(upload_id.clone(), Arc::new(Mutex::new(session)));
|
||||
drop(map);
|
||||
|
||||
let _ = events.send(ReceiverEvent::UploadStarted {
|
||||
device: device.to_owned(),
|
||||
file_name,
|
||||
size,
|
||||
});
|
||||
let body = json!({
|
||||
"ok": true,
|
||||
"upload_id": upload_id,
|
||||
"chunk_size": chunk_size,
|
||||
"total_chunks": total_chunks,
|
||||
}).to_string();
|
||||
write_response(stream, 200, "application/json", body.as_bytes())
|
||||
}
|
||||
|
||||
fn receive_parallel_chunk(
|
||||
stream: &mut TcpStream,
|
||||
reader: &mut BufReader<TcpStream>,
|
||||
path: &str,
|
||||
headers: &HashMap<String, String>,
|
||||
events: &Sender<ReceiverEvent>,
|
||||
sessions: &SessionMap,
|
||||
) -> Result<()> {
|
||||
let suffix = path.trim_start_matches("/qft/upload/chunk/");
|
||||
let (upload_id, index_text) = suffix.split_once('/').context("chunk 路径无效")?;
|
||||
let index: u32 = index_text.parse().context("chunk index 无效")?;
|
||||
let content_len: u64 = headers.get("content-length")
|
||||
.context("缺少 Content-Length")?
|
||||
.parse().context("Content-Length 无效")?;
|
||||
|
||||
let session_arc = {
|
||||
let map = sessions.lock().map_err(|_| anyhow::anyhow!("上传会话锁损坏"))?;
|
||||
map.get(upload_id).cloned().context("上传会话不存在或已完成")?
|
||||
};
|
||||
|
||||
let (part_path, offset, expected_len, device, file_name, size, already) = {
|
||||
let session = session_arc.lock().map_err(|_| anyhow::anyhow!("上传会话损坏"))?;
|
||||
if index >= session.total_chunks { bail!("chunk index 越界") }
|
||||
let offset = index as u64 * session.chunk_size;
|
||||
let expected = (session.size - offset).min(session.chunk_size);
|
||||
(
|
||||
session.part_path.clone(),
|
||||
offset,
|
||||
expected,
|
||||
session.device.clone(),
|
||||
session.file_name.clone(),
|
||||
session.size,
|
||||
session.received[index as usize],
|
||||
)
|
||||
};
|
||||
if content_len != expected_len { bail!("chunk 长度不匹配:{content_len} != {expected_len}") }
|
||||
|
||||
if already {
|
||||
drain_exact(reader, content_len)?;
|
||||
return write_response(stream, 200, "application/json", b"{\"ok\":true,\"duplicate\":true}");
|
||||
}
|
||||
|
||||
let mut out = OpenOptions::new().write(true).open(&part_path)?;
|
||||
out.seek(SeekFrom::Start(offset))?;
|
||||
copy_exact(reader, &mut out, content_len)?;
|
||||
out.flush()?;
|
||||
|
||||
let received = {
|
||||
let mut session = session_arc.lock().map_err(|_| anyhow::anyhow!("上传会话损坏"))?;
|
||||
if !session.received[index as usize] {
|
||||
session.received[index as usize] = true;
|
||||
session.received_bytes = session.received_bytes.saturating_add(content_len);
|
||||
}
|
||||
session.received_bytes
|
||||
};
|
||||
let _ = events.send(ReceiverEvent::UploadProgress { device, file_name, received, size });
|
||||
write_response(stream, 200, "application/json", b"{\"ok\":true}")
|
||||
}
|
||||
|
||||
fn finish_parallel_upload(
|
||||
stream: &mut TcpStream,
|
||||
path: &str,
|
||||
events: &Sender<ReceiverEvent>,
|
||||
sessions: &SessionMap,
|
||||
) -> Result<()> {
|
||||
let upload_id = path.trim_start_matches("/qft/upload/finish/");
|
||||
let session_arc = {
|
||||
let map = sessions.lock().map_err(|_| anyhow::anyhow!("上传会话锁损坏"))?;
|
||||
map.get(upload_id).cloned().context("上传会话不存在或已完成")?
|
||||
};
|
||||
|
||||
let (device, file_name, size, part_path, final_path) = {
|
||||
let session = session_arc.lock().map_err(|_| anyhow::anyhow!("上传会话损坏"))?;
|
||||
if session.received.iter().any(|v| !*v) {
|
||||
let missing = session.received.iter().filter(|v| !**v).count();
|
||||
let body = json!({"ok": false, "missing": missing}).to_string();
|
||||
return write_response(stream, 409, "application/json", body.as_bytes());
|
||||
}
|
||||
(
|
||||
session.device.clone(), session.file_name.clone(), session.size,
|
||||
session.part_path.clone(), session.final_path.clone(),
|
||||
)
|
||||
};
|
||||
|
||||
let sha = hash_file(&part_path)?;
|
||||
if final_path.exists() { bail!("目标文件已存在,请重新初始化上传") }
|
||||
fs::rename(&part_path, &final_path).context("完成文件重命名失败")?;
|
||||
{
|
||||
let mut map = sessions.lock().map_err(|_| anyhow::anyhow!("上传会话锁损坏"))?;
|
||||
map.remove(upload_id);
|
||||
}
|
||||
let _ = events.send(ReceiverEvent::UploadCompleted {
|
||||
device, file_name, saved_path: final_path, sha256: sha.clone(),
|
||||
});
|
||||
let body = json!({"ok": true, "sha256": sha, "size": size}).to_string();
|
||||
write_response(stream, 200, "application/json", body.as_bytes())
|
||||
}
|
||||
|
||||
fn receive_legacy_stream(
|
||||
stream: &mut TcpStream,
|
||||
reader: &mut BufReader<TcpStream>,
|
||||
save_dir: &Path,
|
||||
headers: &HashMap<String, String>,
|
||||
device: &str,
|
||||
events: &Sender<ReceiverEvent>,
|
||||
) -> Result<()> {
|
||||
let file_name = decode_header(headers.get("x-qft-filename"))
|
||||
.map(sanitize_file_name)
|
||||
.filter(|s| !s.is_empty())
|
||||
.unwrap_or_else(|| "qft-upload.bin".to_owned());
|
||||
let size: u64 = headers.get("content-length")
|
||||
.context("缺少 Content-Length")?
|
||||
.parse().context("Content-Length 无效")?;
|
||||
let declared_size = headers.get("x-qft-size")
|
||||
.and_then(|v| v.parse::<u64>().ok())
|
||||
.unwrap_or(size);
|
||||
if declared_size != size { bail!("文件大小头与 Content-Length 不一致") }
|
||||
|
||||
let final_path = unique_path(save_dir, &file_name);
|
||||
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}.qftpart"));
|
||||
let mut out = File::create(&part_path).with_context(|| format!("创建临时文件 {}", part_path.display()))?;
|
||||
let _ = events.send(ReceiverEvent::UploadStarted { device: device.to_owned(), file_name: file_name.clone(), size });
|
||||
|
||||
let mut remaining = size;
|
||||
let mut received = 0u64;
|
||||
let mut buf = vec![0u8; IO_BUFFER];
|
||||
let mut hasher = Sha256::new();
|
||||
let mut last_report = Instant::now();
|
||||
while remaining > 0 {
|
||||
let want = remaining.min(buf.len() as u64) as usize;
|
||||
let n = reader.read(&mut buf[..want])?;
|
||||
if n == 0 { bail!("上传中连接提前关闭:{received}/{size}") }
|
||||
out.write_all(&buf[..n])?;
|
||||
hasher.update(&buf[..n]);
|
||||
received += n as u64;
|
||||
remaining -= n as u64;
|
||||
if last_report.elapsed() >= Duration::from_millis(100) || remaining == 0 {
|
||||
let _ = events.send(ReceiverEvent::UploadProgress {
|
||||
device: device.to_owned(), file_name: file_name.clone(), received, size,
|
||||
});
|
||||
last_report = Instant::now();
|
||||
}
|
||||
}
|
||||
out.sync_all()?;
|
||||
drop(out);
|
||||
fs::rename(&part_path, &final_path).context("完成文件重命名失败")?;
|
||||
let sha = hex(&hasher.finalize());
|
||||
let _ = events.send(ReceiverEvent::UploadCompleted {
|
||||
device: device.to_owned(), file_name, saved_path: final_path, sha256: sha,
|
||||
});
|
||||
write_response(stream, 200, "application/json", b"{\"ok\":true,\"legacy\":true}")
|
||||
}
|
||||
|
||||
fn copy_exact(reader: &mut impl Read, writer: &mut impl Write, mut remaining: u64) -> Result<()> {
|
||||
let mut buf = vec![0u8; IO_BUFFER];
|
||||
while remaining > 0 {
|
||||
let want = remaining.min(buf.len() as u64) as usize;
|
||||
let n = reader.read(&mut buf[..want])?;
|
||||
if n == 0 { bail!("chunk 上传中连接提前关闭") }
|
||||
writer.write_all(&buf[..n])?;
|
||||
remaining -= n as u64;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn drain_exact(reader: &mut impl Read, remaining: u64) -> Result<()> {
|
||||
copy_exact(reader, &mut std::io::sink(), remaining)
|
||||
}
|
||||
|
||||
fn hash_file(path: &Path) -> Result<String> {
|
||||
let mut input = File::open(path)?;
|
||||
let mut hasher = Sha256::new();
|
||||
let mut buf = vec![0u8; 4 * 1024 * 1024];
|
||||
loop {
|
||||
let n = input.read(&mut buf)?;
|
||||
if n == 0 { break }
|
||||
hasher.update(&buf[..n]);
|
||||
}
|
||||
Ok(hex(&hasher.finalize()))
|
||||
}
|
||||
|
||||
fn write_response(stream: &mut TcpStream, code: u16, content_type: &str, body: &[u8]) -> Result<()> {
|
||||
let reason = match code {
|
||||
200 => "OK", 400 => "Bad Request", 403 => "Forbidden", 404 => "Not Found",
|
||||
409 => "Conflict", _ => "Error",
|
||||
};
|
||||
write!(stream,
|
||||
"HTTP/1.1 {code} {reason}\r\nContent-Type: {content_type}\r\nContent-Length: {}\r\nConnection: close\r\n\r\n",
|
||||
body.len())?;
|
||||
stream.write_all(body)?;
|
||||
stream.flush()?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn decode_header(value: Option<&String>) -> Option<String> {
|
||||
let raw = value?;
|
||||
let bytes = URL_SAFE_NO_PAD.decode(raw).ok()?;
|
||||
String::from_utf8(bytes).ok()
|
||||
}
|
||||
|
||||
fn sanitize_file_name(name: String) -> String {
|
||||
let cleaned: String = name.chars()
|
||||
.map(|c| if c.is_control() || matches!(c, '<' | '>' | ':' | '"' | '/' | '\\' | '|' | '?' | '*') { '_' } else { c })
|
||||
.collect();
|
||||
cleaned.trim().trim_matches('.').chars().take(180).collect()
|
||||
}
|
||||
|
||||
fn unique_path(dir: &Path, name: &str) -> PathBuf {
|
||||
let base = Path::new(name);
|
||||
let stem = base.file_stem().and_then(|s| s.to_str()).unwrap_or("qft-upload");
|
||||
let ext = base.extension().and_then(|s| s.to_str());
|
||||
let mut candidate = dir.join(name);
|
||||
let mut n = 1;
|
||||
while candidate.exists() {
|
||||
let next = match ext {
|
||||
Some(ext) => format!("{stem} ({n}).{ext}"),
|
||||
None => format!("{stem} ({n})"),
|
||||
};
|
||||
candidate = dir.join(next);
|
||||
n += 1;
|
||||
}
|
||||
candidate
|
||||
}
|
||||
|
||||
fn unique_path_reserved<'a>(
|
||||
dir: &Path,
|
||||
name: &str,
|
||||
sessions: impl Iterator<Item = &'a Arc<Mutex<UploadSession>>>,
|
||||
) -> PathBuf {
|
||||
let reserved: Vec<PathBuf> = sessions
|
||||
.filter_map(|s| s.lock().ok().map(|v| v.final_path.clone()))
|
||||
.collect();
|
||||
let base = Path::new(name);
|
||||
let stem = base.file_stem().and_then(|s| s.to_str()).unwrap_or("qft-upload");
|
||||
let ext = base.extension().and_then(|s| s.to_str());
|
||||
let mut n = 0u32;
|
||||
loop {
|
||||
let next_name = if n == 0 {
|
||||
name.to_owned()
|
||||
} else {
|
||||
match ext {
|
||||
Some(ext) => format!("{stem} ({n}).{ext}"),
|
||||
None => format!("{stem} ({n})"),
|
||||
}
|
||||
};
|
||||
let candidate = dir.join(next_name);
|
||||
if !candidate.exists() && !reserved.contains(&candidate) {
|
||||
return candidate;
|
||||
}
|
||||
n += 1;
|
||||
}
|
||||
}
|
||||
|
||||
fn random_id() -> String {
|
||||
let mut bytes = [0u8; 12];
|
||||
rand::rng().fill_bytes(&mut bytes);
|
||||
URL_SAFE_NO_PAD.encode(bytes)
|
||||
}
|
||||
|
||||
fn hex(bytes: &[u8]) -> String {
|
||||
const H: &[u8; 16] = b"0123456789abcdef";
|
||||
let mut out = String::with_capacity(bytes.len() * 2);
|
||||
for &b in bytes { out.push(H[(b >> 4) as usize] as char); out.push(H[(b & 15) as usize] as char); }
|
||||
out
|
||||
}
|
||||
Reference in New Issue
Block a user