AI media harness for planning, generating, and editing locally
TypeScript
Resume interrupted weight and LoRA downloads
Bitti287cd9947ff6
diff --git a/RELEASE_NOTES.md b/RELEASE_NOTES.mdindex d2ebd02..48def75 100644--- a/RELEASE_NOTES.md+++ b/RELEASE_NOTES.md@@ -16,6 +16,7 @@ This alpha update adds still image projects, new scene types, and a rebuilt Wind## Fixes and improvements+- Interrupted weight and LoRA downloads retain their progress and can resume on retry or after reopening the app.- Updated the bundled SlopFab runtime.- Smoother previews: clips are preloaded and pixels are kept across cuts.- Fixed export stalls in the video encoder.diff --git a/docs/generator-weights.md b/docs/generator-weights.mdindex 2f15cf9..243a48c 100644--- a/docs/generator-weights.md+++ b/docs/generator-weights.md@@ -140,8 +140,22 @@ The fill tracks completed files plus progress through the current file.Downloads also appear in Work Queue with progress and cancellation controls.They run separately from the generation queue and do not occupy a generation slot.Progress details and cancellation remain available in Settings if the editor is closed.-Failed transfers can be retried; completed files are retained, while an interrupted-file starts again. No full model file is buffered in memory.+Failed or cancelled transfers keep their downloaded bytes. Choose **Retry**, or+reopen the app and choose **Download** for the same generator or LoRA, to continue+from the saved position. Completed files are reused and progress includes the+bytes already downloaded. Downloads do not restart automatically when the app opens.++Partial downloads use a stable `.part` file and matching `.part.json` record in+the weight folder. Keep both files together if moving an unfinished download.+Slopus searches all configured weight folders for existing progress before+starting a new file. Partial files cannot be used for generation until complete.++Resume requests use the saved strong ETag with HTTP `Range` and `If-Range`, and+validate the returned byte range before appending. If the file changed, the server+ignores ranges, or no strong ETag is available, Slopus starts that file again+instead of combining incompatible data. This follows the+[HTTP range request rules](https://www.rfc-editor.org/rfc/rfc9110.html#section-13.1.5).+No full model file is buffered in memory.After completion, the path fields show local files, while the source URLs remainstored. Templates needing downloads cannot be selected as default and are omitteddiff --git a/src-tauri/src/weights.rs b/src-tauri/src/weights.rsindex 96db79a..c9b88ed 100644--- a/src-tauri/src/weights.rs+++ b/src-tauri/src/weights.rs@@ -1,13 +1,11 @@use crate::diagnostics;-use reqwest::{blocking::Client, Url};+use reqwest::Url;use serde::{Deserialize, Serialize};use std::{collections::HashMap,fs::{self, OpenOptions},- io::{Read, Write},path::{Path, PathBuf},sync::{atomic::{AtomicBool, Ordering}, Arc, Mutex},- time::{Duration, Instant},};use tauri::{AppHandle, Emitter, State};@@ -22,6 +20,8 @@ struct Progress { request_id: String, downloaded: u64, total: Option<u64> }struct CompletedFile { url: String, bytes: u64 }pub(crate) mod lora;+mod download;+use download::{has_partial_download, transfer};fn download_url(value: &str) -> Result<Url, String> {let mut url = Url::parse(value.trim()).map_err(|_| "Enter an HTTP or HTTPS download URL.".to_string())?;@@ -96,6 +96,12 @@ fn transfer_from_roots(url: &Url, roots: &[PathBuf], cancelled: &AtomicBool, proif cancelled.load(Ordering::Relaxed) { return Err("Download cancelled.".into()); }// Search every location before choosing a destination, including read-only caches.if let Some(path) = find_cached_weight(url, roots) { return Ok(path); }+ // A previous session may have started in a fallback or an older custom folder.+ for root in roots {+ if has_partial_download(url, root) && writable_weights(root).is_ok() {+ return transfer(url, root, cancelled, progress);+ }+ }transfer(url, &weights_directory(roots)?, cancelled, progress)}@@ -118,61 +124,6 @@ pub async fn find_downloaded_weights(app: AppHandle, urls: Vec<String>) -> Resul}).await.map_err(|error| error.to_string())?}-fn transfer(url: &Url, directory: &Path, cancelled: &AtomicBool, mut progress: impl FnMut(u64, Option<u64>)) -> Result<String, String> {- let destination = directory.join(destination_name(url));- if cached_file(&destination, url) { return Ok(destination.to_string_lossy().into_owned()); }- let mut random = [0u8; 16];- getrandom::fill(&mut random).map_err(|error| error.to_string())?;- let temporary = directory.join(format!(".download-{:x}.part", u128::from_le_bytes(random)));- let result = (|| {- let client = Client::builder().connect_timeout(Duration::from_secs(30))- .timeout(Duration::from_secs(24 * 60 * 60)).build().map_err(|error| error.to_string())?;- let mut response = client.get(url.clone()).header("Accept-Encoding", "identity").send()- .and_then(|response| response.error_for_status()).map_err(|error| format!("Download failed: {error}"))?;- if response.status() != reqwest::StatusCode::OK { return Err("The server did not return a complete file.".into()); }- if response.headers().get(reqwest::header::CONTENT_TYPE).and_then(|v| v.to_str().ok())- .is_some_and(|value| value.starts_with("text/html")) {- return Err("The URL returned a web page instead of a weight file. Use a direct download link.".into());- }- let total = response.content_length();- let mut file = OpenOptions::new().write(true).create_new(true).open(&temporary).map_err(|error| error.to_string())?;- let mut downloaded = 0u64;- let mut buffer = vec![0u8; 1024 * 1024];- let mut last_progress = Instant::now();- progress(0, total);- loop {- if cancelled.load(Ordering::Relaxed) { return Err("Download cancelled.".into()); }- let count = response.read(&mut buffer).map_err(|error| format!("Download interrupted: {error}"))?;- if count == 0 { break; }- file.write_all(&buffer[..count]).map_err(|error| format!("Could not write weights: {error}"))?;- downloaded += count as u64;- if last_progress.elapsed() >= Duration::from_millis(200) {- progress(downloaded, total);- last_progress = Instant::now();- }- }- if downloaded == 0 || total.is_some_and(|total| downloaded != total) {- return Err("The download is incomplete. Try downloading again.".into());- }- if cancelled.load(Ordering::Relaxed) { return Err("Download cancelled.".into()); }- file.sync_all().map_err(|error| error.to_string())?;- drop(file);- // Only a successful, complete transfer receives the final filename.- if destination.exists() {- if cached_file(&destination, url) { return Ok(destination.to_string_lossy().into_owned()); }- fs::remove_file(&destination).map_err(|error| error.to_string())?;- }- fs::rename(&temporary, &destination).map_err(|error| error.to_string())?;- let record = CompletedFile { url: url.to_string(), bytes: downloaded };- fs::write(destination.with_extension("complete.json"), serde_json::to_vec(&record).map_err(|error| error.to_string())?)- .map_err(|error| error.to_string())?;- progress(downloaded, Some(downloaded));- Ok(destination.to_string_lossy().into_owned())- })();- let _ = fs::remove_file(&temporary);- result-}-#[tauri::command]pub async fn download_weight(app: AppHandle, downloads: State<'_, WeightDownloads>, request_id: String, url: String) -> Result<String, String> {let url = download_url(&url)?;@@ -281,6 +232,7 @@ pub async fn remove_downloaded_weights(app: AppHandle, paths: Vec<String>) -> Re#[cfg(test)]mod tests {use super::*;+ use std::io::{Read, Write};use std::net::TcpListener;#[test]@@ -410,7 +362,10 @@ mod tests {let folder = tempfile::tempdir().unwrap();let url = server(response);assert!(transfer(&url, folder.path(), &AtomicBool::new(cancelled), |_, _| {}).is_err());- assert_eq!(fs::read_dir(folder.path()).unwrap().count(), 0);+ let destination = folder.path().join(destination_name(&url));+ assert!(!destination.exists());+ assert!(!destination.with_extension("complete.json").exists());+ assert!(find_downloaded(vec![url.to_string()], &[folder.path().to_path_buf()]).is_empty());}}diff --git a/src-tauri/src/weights/download.rs b/src-tauri/src/weights/download.rsnew file mode 100644index 0000000..9af6644--- /dev/null+++ b/src-tauri/src/weights/download.rs@@ -0,0 +1,303 @@+use super::{cached_file, destination_name, CompletedFile};+use crate::storage::atomic::{atomic_replace, write_atomically};+use reqwest::{+ blocking::{Client, Response},+ header, StatusCode, Url,+};+use serde::{Deserialize, Serialize};+use std::{+ fs::{self, OpenOptions},+ io::{Read, Seek, SeekFrom, Write},+ path::Path,+ sync::atomic::{AtomicBool, Ordering},+ time::{Duration, Instant},+};++#[derive(Serialize, Deserialize)]+struct PartialFile {+ url: String,+ etag: Option<String>,+ total: Option<u64>,+ // Written only after EOF, size validation and syncing the file. This allows+ // recovery if the app exits between the final rename and completion record.+ #[serde(default)]+ complete: bool,+}++fn partial_record(destination: &Path, url: &Url) -> Option<PartialFile> {+ fs::read(destination.with_extension("part.json"))+ .ok()+ .and_then(|bytes| serde_json::from_slice::<PartialFile>(&bytes).ok())+ .filter(|record| record.url == url.as_str())+}++pub(super) fn has_partial_download(url: &Url, directory: &Path) -> bool {+ let destination = directory.join(destination_name(url));+ partial_record(&destination, url).is_some_and(|record| {+ destination.with_extension("part").is_file() || (record.complete && destination.is_file())+ })+}++fn strong_etag(value: &str) -> bool {+ value.starts_with('"')+ && value.ends_with('"')+ && value.len() >= 2+ && !value[1..value.len() - 1]+ .bytes()+ .any(|byte| byte < 0x21 || byte == b'"' || byte == 0x7f)+}++fn response_etag(response: &Response) -> Option<String> {+ response+ .headers()+ .get(header::ETAG)+ .and_then(|value| value.to_str().ok())+ .filter(|value| strong_etag(value))+ .map(str::to_owned)+}++fn save_record(destination: &Path, record: &PartialFile) -> Result<(), String> {+ write_atomically(+ &destination.with_extension("part.json"),+ &serde_json::to_vec(record).map_err(|error| error.to_string())?,+ )+}++fn publish(destination: &Path, record: &PartialFile) -> Result<String, String> {+ let temporary = destination.with_extension("part");+ if temporary.exists() {+ atomic_replace(&temporary, destination).map_err(|error| error.to_string())?;+ }+ let completed = CompletedFile {+ url: record.url.clone(),+ bytes: record.total.ok_or("Missing completed download size.")?,+ };+ write_atomically(+ &destination.with_extension("complete.json"),+ &serde_json::to_vec(&completed).map_err(|error| error.to_string())?,+ )?;+ let _ = fs::remove_file(destination.with_extension("part.json"));+ Ok(destination.to_string_lossy().into_owned())+}++// RFC 9110: never append a different representation or an unexpected byte range.+// If-Range can omit the ETag in a 206 response; if present it must still match.+fn resumed_range(response: &Response, offset: u64, record: &PartialFile) -> Option<(u64, u64)> {+ if let Some(etag) = response.headers().get(header::ETAG) {+ if Some(etag.to_str().ok()?) != record.etag.as_deref() {+ return None;+ }+ }+ let range = response+ .headers()+ .get(header::CONTENT_RANGE)?+ .to_str()+ .ok()?+ .strip_prefix("bytes ")?;+ let (range, total) = range.split_once('/')?;+ let (start, end) = range.split_once('-')?;+ let (start, end, total) = (+ start.parse::<u64>().ok()?,+ end.parse::<u64>().ok()?,+ total.parse::<u64>().ok()?,+ );+ if start != offset+ || end < start+ || end >= total+ || record.total.is_some_and(|size| size != total)+ || response+ .content_length()+ .is_some_and(|size| size != end - start + 1)+ {+ return None;+ }+ Some((end + 1, total))+}++pub(super) fn transfer(+ url: &Url,+ directory: &Path,+ cancelled: &AtomicBool,+ mut progress: impl FnMut(u64, Option<u64>),+) -> Result<String, String> {+ if cancelled.load(Ordering::Relaxed) {+ return Err("Download cancelled.".into());+ }+ let destination = directory.join(destination_name(url));+ if cached_file(&destination, url) {+ return Ok(destination.to_string_lossy().into_owned());+ }+ let temporary = destination.with_extension("part");+ let saved = partial_record(&destination, url);+ if let Some(record) = &saved {+ if record.complete+ && !temporary.exists()+ && record.total.is_some_and(|size| {+ size > 0 && fs::metadata(&destination).is_ok_and(|file| file.len() == size)+ })+ {+ return publish(&destination, record);+ }+ }+ let mut options = OpenOptions::new();+ options.read(true).write(true).create(true).truncate(false);+ #[cfg(windows)]+ {+ use std::os::windows::fs::OpenOptionsExt;+ // Another Slopus process must not append to the same partial file.+ options.share_mode(0);+ }+ let mut file = options+ .open(&temporary)+ .map_err(|error| format!("Could not open partial download (it may be in use): {error}"))?;+ // Read metadata under the file lock; another process may have updated it+ // between the initial recovery check and opening the partial file.+ let saved = partial_record(&destination, url);+ let length = file.metadata().map_err(|error| error.to_string())?.len();+ if let Some(record) = &saved {+ if record.complete && length > 0 && record.total == Some(length) {+ drop(file);+ return publish(&destination, record);+ }+ }+ let resume = saved.as_ref().filter(|record| {+ length > 0+ && !record.complete+ && record.total.is_none_or(|size| length < size)+ && record.etag.as_deref().is_some_and(strong_etag)+ });+ let mut offset = if resume.is_some() { length } else { 0 };+ let client = Client::builder()+ .connect_timeout(Duration::from_secs(30))+ .timeout(Duration::from_secs(24 * 60 * 60))+ .build()+ .map_err(|error| error.to_string())?;+ let request = || {+ client+ .get(url.clone())+ .header(header::ACCEPT_ENCODING, "identity")+ };+ let mut pending = request();+ if let Some(record) = resume {+ pending = pending+ .header(header::RANGE, format!("bytes={offset}-"))+ .header(header::IF_RANGE, record.etag.as_deref().unwrap());+ }+ let mut response = pending+ .send()+ .map_err(|error| format!("Download failed: {error}"))?;+ let mut range = None;+ if let Some(record) = resume {+ if response.status() == StatusCode::PARTIAL_CONTENT {+ range = resumed_range(&response, offset, record);+ }+ if response.status() == StatusCode::RANGE_NOT_SATISFIABLE+ || (response.status() == StatusCode::PARTIAL_CONTENT && range.is_none())+ {+ drop(response);+ if cancelled.load(Ordering::Relaxed) {+ return Err("Download cancelled.".into());+ }+ response = request()+ .send()+ .map_err(|error| format!("Download failed: {error}"))?;+ }+ }+ response+ .error_for_status_ref()+ .map_err(|error| format!("Download failed: {error}"))?;+ if response+ .headers()+ .get(header::CONTENT_TYPE)+ .and_then(|value| value.to_str().ok())+ .is_some_and(|value| value.to_ascii_lowercase().starts_with("text/html"))+ {+ return Err(+ "The URL returned a web page instead of a weight file. Use a direct download link."+ .into(),+ );+ }+ if response+ .headers()+ .get(header::CONTENT_ENCODING)+ .is_some_and(|value| value != "identity")+ {+ return Err("The server returned encoded data instead of the requested file bytes.".into());+ }+ let (end, total, etag) = if response.status() == StatusCode::OK {+ offset = 0;+ (+ response.content_length(),+ response.content_length(),+ response_etag(&response),+ )+ } else if response.status() == StatusCode::PARTIAL_CONTENT && range.is_some() {+ let (end, total) = range.unwrap();+ (Some(end), Some(total), resume.unwrap().etag.clone())+ } else {+ return Err("The server did not return the requested file bytes.".into());+ };+ if cancelled.load(Ordering::Relaxed) {+ return Err("Download cancelled.".into());+ }+ if total == Some(0) {+ return Err("The URL returned an empty weight file.".into());+ }+ if offset == 0 {+ // Truncate before replacing metadata, so a crash cannot pair old bytes+ // with a new ETag. Keep existing data untouched until a valid response.+ file.set_len(0).map_err(|error| error.to_string())?;+ file.sync_all().map_err(|error| error.to_string())?;+ }+ file.seek(SeekFrom::Start(offset))+ .map_err(|error| error.to_string())?;+ let mut record = PartialFile {+ url: url.to_string(),+ etag,+ total,+ complete: false,+ };+ save_record(&destination, &record)?;+ let mut downloaded = offset;+ let mut buffer = vec![0u8; 1024 * 1024];+ let mut last_progress = Instant::now();+ progress(downloaded, total);+ loop {+ if cancelled.load(Ordering::Relaxed) {+ return Err("Download cancelled.".into());+ }+ let count = response+ .read(&mut buffer)+ .map_err(|error| format!("Download interrupted: {error}"))?;+ if count == 0 {+ break;+ }+ if end.is_some_and(|end| count as u64 > end.saturating_sub(downloaded)) {+ return Err("The server sent more bytes than the requested file range.".into());+ }+ file.write_all(&buffer[..count])+ .map_err(|error| format!("Could not write weights: {error}"))?;+ downloaded += count as u64;+ if last_progress.elapsed() >= Duration::from_millis(200) {+ progress(downloaded, total);+ last_progress = Instant::now();+ }+ }+ if downloaded == 0 || total.is_some_and(|total| downloaded != total) {+ return Err("The download is incomplete. Retry to continue downloading.".into());+ }+ if cancelled.load(Ordering::Relaxed) {+ return Err("Download cancelled.".into());+ }+ file.sync_all().map_err(|error| error.to_string())?;+ record.complete = true;+ record.total = Some(downloaded);+ save_record(&destination, &record)?;+ drop(file);+ let path = publish(&destination, &record)?;+ progress(downloaded, Some(downloaded));+ Ok(path)+}++#[cfg(test)]+mod tests;diff --git a/src-tauri/src/weights/download/tests.rs b/src-tauri/src/weights/download/tests.rsnew file mode 100644index 0000000..0ef66cf--- /dev/null+++ b/src-tauri/src/weights/download/tests.rs@@ -0,0 +1,306 @@+use super::*;+use std::{net::TcpListener, thread::JoinHandle};++fn server(responses: Vec<&'static [u8]>) -> (Url, JoinHandle<Vec<String>>) {+ let listener = TcpListener::bind("127.0.0.1:0").unwrap();+ let address = listener.local_addr().unwrap();+ listener.set_nonblocking(true).unwrap();+ let task = std::thread::spawn(move || {+ responses+ .into_iter()+ .map(|response| {+ let started = Instant::now();+ let mut connection = loop {+ match listener.accept() {+ Ok((connection, _)) => break connection,+ Err(error)+ if error.kind() == std::io::ErrorKind::WouldBlock+ && started.elapsed() < Duration::from_secs(5) =>+ {+ std::thread::sleep(Duration::from_millis(5))+ }+ Err(error) => panic!("Expected download request: {error}"),+ }+ };+ connection.set_nonblocking(false).unwrap();+ connection+ .set_read_timeout(Some(Duration::from_secs(5)))+ .unwrap();+ let mut request = Vec::new();+ let mut buffer = [0u8; 1024];+ while !request.windows(4).any(|bytes| bytes == b"\r\n\r\n") {+ let count = connection.read(&mut buffer).unwrap();+ assert_ne!(count, 0);+ request.extend_from_slice(&buffer[..count]);+ }+ let _ = connection.write_all(response);+ String::from_utf8(request).unwrap().to_ascii_lowercase()+ })+ .collect()+ });+ (+ Url::parse(&format!("http://{address}/model.safetensors")).unwrap(),+ task,+ )+}++fn seed_partial(+ directory: &Path,+ url: &Url,+ bytes: &[u8],+ etag: Option<&str>,+ complete: bool,+) -> std::path::PathBuf {+ fs::create_dir_all(directory).unwrap();+ let destination = directory.join(destination_name(url));+ fs::write(destination.with_extension("part"), bytes).unwrap();+ save_record(+ &destination,+ &PartialFile {+ url: url.to_string(),+ etag: etag.map(str::to_owned),+ total: Some(10),+ complete,+ },+ )+ .unwrap();+ destination+}++const FULL: &[u8] = b"HTTP/1.1 200 OK\r\nContent-Length: 10\r\nETag: \"one\"\r\nConnection: close\r\n\r\n1234567890";+const REMAINDER: &[u8] = b"HTTP/1.1 206 Partial Content\r\nContent-Length: 6\r\nContent-Range: bytes 4-9/10\r\nETag: \"one\"\r\nConnection: close\r\n\r\n567890";++#[test]+fn interrupted_download_resumes_from_disk_with_range_and_version_and_full_progress() {+ let (url, task) = server(vec![+ b"HTTP/1.1 200 OK\r\nContent-Length: 10\r\nETag: \"one\"\r\nConnection: close\r\n\r\n1234",+ REMAINDER,+ ]);+ let folder = tempfile::tempdir().unwrap();+ assert!(transfer(&url, folder.path(), &AtomicBool::new(false), |_, _| {}).is_err());+ let destination = folder.path().join(destination_name(&url));+ assert_eq!(+ fs::read(destination.with_extension("part")).unwrap(),+ b"1234"+ );+ assert!(!cached_file(&destination, &url));+ // No in-memory download state is reused, just as after restarting the app.+ let mut events = Vec::new();+ let result = transfer(+ &url,+ folder.path(),+ &AtomicBool::new(false),+ |bytes, total| events.push((bytes, total)),+ )+ .unwrap();+ assert_eq!(fs::read(result).unwrap(), b"1234567890");+ assert_eq!(events.first(), Some(&(4, Some(10))));+ assert_eq!(events.last(), Some(&(10, Some(10))));+ assert!(cached_file(&destination, &url));+ assert!(!destination.with_extension("part").exists());+ assert!(!destination.with_extension("part.json").exists());+ let requests = task.join().unwrap();+ assert!(!requests[0].contains("range:"));+ assert!(requests[1].contains("range: bytes=4-\r\n"));+ assert!(requests[1].contains("if-range: \"one\"\r\n"));+}++#[test]+fn cancellation_keeps_partial_bytes_for_the_next_download() {+ let (url, task) = server(vec![REMAINDER, REMAINDER]);+ let folder = tempfile::tempdir().unwrap();+ let destination = seed_partial(folder.path(), &url, b"1234", Some("\"one\""), false);+ let cancelled = AtomicBool::new(false);+ assert_eq!(+ transfer(&url, folder.path(), &cancelled, |_, _| cancelled+ .store(true, Ordering::Relaxed))+ .unwrap_err(),+ "Download cancelled."+ );+ assert_eq!(+ fs::read(destination.with_extension("part")).unwrap(),+ b"1234"+ );+ let result = transfer(&url, folder.path(), &AtomicBool::new(false), |_, _| {}).unwrap();+ assert_eq!(fs::read(result).unwrap(), b"1234567890");+ assert!(task+ .join()+ .unwrap()+ .iter()+ .all(|request| request.contains("range: bytes=4-\r\n")));+}++#[test]+fn another_interruption_retains_newly_appended_bytes() {+ let (url, task) = server(vec![+ b"HTTP/1.1 206 Partial Content\r\nContent-Length: 6\r\nContent-Range: bytes 4-9/10\r\nETag: \"one\"\r\nConnection: close\r\n\r\n56",+ b"HTTP/1.1 206 Partial Content\r\nContent-Length: 4\r\nContent-Range: bytes 6-9/10\r\nConnection: close\r\n\r\n7890",+ ]);+ let folder = tempfile::tempdir().unwrap();+ let destination = seed_partial(folder.path(), &url, b"1234", Some("\"one\""), false);+ assert!(transfer(&url, folder.path(), &AtomicBool::new(false), |_, _| {}).is_err());+ assert_eq!(+ fs::read(destination.with_extension("part")).unwrap(),+ b"123456"+ );+ let result = transfer(&url, folder.path(), &AtomicBool::new(false), |_, _| {}).unwrap();+ assert_eq!(fs::read(result).unwrap(), b"1234567890");+ assert!(task.join().unwrap()[1].contains("range: bytes=6-\r\n"));+}++#[test]+fn full_response_to_range_restarts_instead_of_appending_changed_or_duplicate_data() {+ let (url, task) = server(vec![FULL]);+ let folder = tempfile::tempdir().unwrap();+ seed_partial(folder.path(), &url, b"old!", Some("\"old\""), false);+ let mut events = Vec::new();+ let result = transfer(+ &url,+ folder.path(),+ &AtomicBool::new(false),+ |bytes, total| events.push((bytes, total)),+ )+ .unwrap();+ assert_eq!(fs::read(result).unwrap(), b"1234567890");+ assert_eq!(events.first(), Some(&(0, Some(10))));+ assert!(task.join().unwrap()[0].contains("if-range: \"old\""));+}++#[test]+fn invalid_or_changed_ranges_restart_with_an_unconditional_request() {+ for response in [+ &b"HTTP/1.1 416 Range Not Satisfiable\r\nContent-Range: bytes */3\r\nContent-Length: 0\r\nConnection: close\r\n\r\n"[..],+ &b"HTTP/1.1 206 Partial Content\r\nContent-Range: bytes 0-5/10\r\nContent-Length: 6\r\nConnection: close\r\n\r\nwrong!"[..],+ &b"HTTP/1.1 206 Partial Content\r\nContent-Range: bytes 4-9/11\r\nContent-Length: 6\r\nConnection: close\r\n\r\nwrong!"[..],+ &b"HTTP/1.1 206 Partial Content\r\nContent-Range: bytes 4-9/10\r\nContent-Length: 6\r\nETag: \"two\"\r\nConnection: close\r\n\r\nwrong!"[..],+ &b"HTTP/1.1 206 Partial Content\r\nContent-Range: bytes 4-9/10\r\nContent-Length: 5\r\nConnection: close\r\n\r\nwrong"[..],+ &b"HTTP/1.1 206 Partial Content\r\nContent-Range: bytes 4-3/10\r\nContent-Length: 0\r\nConnection: close\r\n\r\n"[..],+ ] {+ let (url, task) = server(vec![response, FULL]);+ let folder = tempfile::tempdir().unwrap();+ seed_partial(folder.path(), &url, b"1234", Some("\"one\""), false);+ let result = transfer(&url, folder.path(), &AtomicBool::new(false), |_, _| {}).unwrap();+ assert_eq!(fs::read(result).unwrap(), b"1234567890");+ assert!(!task.join().unwrap()[1].contains("range:"));+ }+}++#[test]+fn missing_or_weak_validators_restart_instead_of_combining_unverifiable_files() {+ for etag in [None, Some("W/\"weak\""), Some("malformed")] {+ let (url, task) = server(vec![FULL]);+ let folder = tempfile::tempdir().unwrap();+ seed_partial(folder.path(), &url, b"old!", etag, false);+ let result = transfer(&url, folder.path(), &AtomicBool::new(false), |_, _| {}).unwrap();+ assert_eq!(fs::read(result).unwrap(), b"1234567890");+ assert!(!task.join().unwrap()[0].contains("range:"));+ }+}++#[test]+fn failed_or_html_responses_do_not_destroy_saved_progress() {+ for response in [+ &b"HTTP/1.1 503 Unavailable\r\nContent-Length: 0\r\nConnection: close\r\n\r\n"[..],+ &b"HTTP/1.1 200 OK\r\nContent-Length: 0\r\nConnection: close\r\n\r\n"[..],+ &b"HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nContent-Length: 5\r\nConnection: close\r\n\r\nerror"[..],+ ] {+ let (url, task) = server(vec![response]);+ let folder = tempfile::tempdir().unwrap();+ let destination = seed_partial(folder.path(), &url, b"1234", Some("\"one\""), false);+ assert!(transfer(&url, folder.path(), &AtomicBool::new(false), |_, _| {}).is_err());+ assert_eq!(fs::read(destination.with_extension("part")).unwrap(), b"1234");+ assert_eq!(partial_record(&destination, &url).unwrap().etag.as_deref(), Some("\"one\""));+ assert!(!cached_file(&destination, &url));+ task.join().unwrap();+ }+}++#[test]+fn restart_recovers_finished_download_on_either_side_of_the_final_rename() {+ for renamed in [false, true] {+ let folder = tempfile::tempdir().unwrap();+ let url = Url::parse("http://127.0.0.1:1/finished.safetensors").unwrap();+ let destination = seed_partial(folder.path(), &url, b"1234567890", Some("\"one\""), true);+ if renamed {+ fs::rename(destination.with_extension("part"), &destination).unwrap();+ }+ let result = transfer(&url, folder.path(), &AtomicBool::new(false), |_, _| {}).unwrap();+ assert_eq!(fs::read(result).unwrap(), b"1234567890");+ assert!(cached_file(&destination, &url));+ }+}++#[test]+fn resumed_download_uses_existing_fallback_before_a_new_preferred_folder() {+ let (url, task) = server(vec![REMAINDER]);+ let folder = tempfile::tempdir().unwrap();+ let preferred = folder.path().join("new-preferred");+ let fallback = folder.path().join("fallback");+ let destination = seed_partial(&fallback, &url, b"1234", Some("\"one\""), false);+ let result = super::super::transfer_from_roots(+ &url,+ &[preferred.clone(), fallback],+ &AtomicBool::new(false),+ |_, _| {},+ )+ .unwrap();+ assert_eq!(Path::new(&result), destination);+ assert!(!preferred.exists());+ assert!(task.join().unwrap()[0].contains("range: bytes=4-\r\n"));+}++#[test]+fn damaged_or_unrelated_metadata_cannot_resume_a_partial_file() {+ for metadata in [+ b"broken".as_slice(),+ br#"{"url":"https://other.example/file","etag":"\"one\"","total":10}"#,+ ] {+ let (url, task) = server(vec![FULL]);+ let folder = tempfile::tempdir().unwrap();+ let destination = seed_partial(folder.path(), &url, b"old!", Some("\"one\""), false);+ fs::write(destination.with_extension("part.json"), metadata).unwrap();+ let result = transfer(&url, folder.path(), &AtomicBool::new(false), |_, _| {}).unwrap();+ assert_eq!(fs::read(result).unwrap(), b"1234567890");+ assert!(!task.join().unwrap()[0].contains("range:"));+ }+}++#[test]+fn short_chunked_range_is_not_published_and_encoded_data_is_rejected() {+ for response in [+ &b"HTTP/1.1 206 Partial Content\r\nContent-Range: bytes 4-9/10\r\nTransfer-Encoding: chunked\r\nConnection: close\r\n\r\n2\r\n56\r\n0\r\n\r\n"[..],+ &b"HTTP/1.1 206 Partial Content\r\nContent-Range: bytes 4-9/10\r\nContent-Encoding: gzip\r\nContent-Length: 6\r\nConnection: close\r\n\r\n567890"[..],+ ] {+ let (url, task) = server(vec![response]);+ let folder = tempfile::tempdir().unwrap();+ let destination = seed_partial(folder.path(), &url, b"1234", Some("\"one\""), false);+ assert!(transfer(&url, folder.path(), &AtomicBool::new(false), |_, _| {}).is_err());+ assert!(!destination.exists());+ assert!(!cached_file(&destination, &url));+ task.join().unwrap();+ }+}++#[cfg(windows)]+#[test]+fn another_process_cannot_write_to_an_active_partial_download() {+ use std::os::windows::fs::OpenOptionsExt;+ let folder = tempfile::tempdir().unwrap();+ let url = Url::parse("http://127.0.0.1:1/locked.safetensors").unwrap();+ let destination = seed_partial(folder.path(), &url, b"1234", Some("\"one\""), false);+ let locked = OpenOptions::new()+ .write(true)+ .share_mode(0)+ .open(destination.with_extension("part"))+ .unwrap();+ assert!(+ transfer(&url, folder.path(), &AtomicBool::new(false), |_, _| {})+ .unwrap_err()+ .contains("may be in use")+ );+ drop(locked);+ assert_eq!(+ fs::read(destination.with_extension("part")).unwrap(),+ b"1234"+ );+}