Native iCloud apps for Windows and Linux, built in Rust. Notes, Reminders, Calendar, Contacts, Drive and Photos, offline and in sync with your iPhone.
Rust apple-notesapple-reminderscloudkitcrdtdesktop-appicloudicloud-calendaricloud-driveicloud-notesicloud-photosiphonelinuxlinux-desktoplocal-firstoffline-firstprivacyrusttauritauri-appwindows
Drive uploads: per-file progress, whole folders, and every file proven intact
Yashwanth Kamireddi1e22bde43300
diff --git a/.uiverify/drive-upload.mjs b/.uiverify/drive-upload.mjsnew file mode 100644index 0000000..9677417--- /dev/null+++ b/.uiverify/drive-upload.mjs@@ -0,0 +1,100 @@+// Uploading from Drive: each file named, with its own progress, and a verdict per file.+//+// "Uploading…" on the button said nothing about which of forty photos had gone. The tray must+// show each one moving, call it uploaded only when the backend has proven it intact, and name+// the reason for any that did not make it — while the window stays usable.+import { chromium } from "playwright";++const b = await chromium.launch();+const ctx = await b.newContext({ viewport: { width: 1280, height: 820 } });+await ctx.addInitScript(() => {+ const handlers = {};+ const wait = (ms) => new Promise((r) => setTimeout(r, ms));+ window.__uploadCalls = [];+ window.__TAURI_INTERNALS__ = {+ transformCallback: (cb) => cb,+ invoke: async (cmd, args) => {+ if (cmd === "auth_status") return { hasStoredSession: true, signedIn: true };+ if (cmd === "auth_resume") return true;+ if (cmd === "account_services") return ["drivews", "docws"];+ if (cmd === "plugin:event|listen") {+ (handlers[args.event] ??= []).push(args.handler);+ return handlers[args.event].length;+ }+ if (cmd === "plugin:dialog|open")+ return args.options?.directory ? ["/home/me/Holiday"] : ["/home/me/a.jpg", "/home/me/b.jpg"];+ if (cmd === "drive_children") return [];+ if (cmd === "drive_item") return null;+ if (cmd === "drive_sync_folder") return 0;+ if (cmd === "drive_upload") {+ window.__uploadCalls.push(args);+ const say = (e) => (handlers["quince://upload"] ?? []).forEach((h) =>+ h({ event: "quince://upload", id: 1, payload: { batch: args.batch, within: "", error: null, ...e } }));+ const a = { id: `${args.batch}:0`, name: "a.jpg", total: 1000 };+ const c = { id: `${args.batch}:1`, name: "b.jpg", total: 500 };+ say({ ...a, sent: 0, state: "waiting" });+ say({ ...c, sent: 0, state: "waiting" });+ await wait(150);+ say({ ...a, sent: 500, state: "sending" });+ await wait(400);+ window.__midway = document.querySelector(".upload-tray")?.innerText ?? "";+ say({ ...a, sent: 1000, state: "verifying" });+ await wait(100);+ say({ ...a, sent: 1000, state: "done" });+ say({ ...c, sent: 500, state: "failed", error: "the bytes Apple received for b.jpg are not the file's, so it was not filed" });+ return ["b.jpg: not filed"];+ }+ if (cmd === "sync_all" || cmd === "sync_status") return { services: [], asOf: 1, tookMs: 0 };+ return [];+ },+ };+});++const page = await ctx.newPage();+await page.goto("http://localhost:4173", { waitUntil: "networkidle" });+await page.locator(".nav-item", { hasText: "iCloud Drive" }).click();+await page.waitForSelector(".drive-rows", { state: "attached" });+await page.waitForTimeout(300);++let failures = 0;+const ok = (pass, yes, no) => {+ console.log(pass ? `PASS ${yes}` : `FAIL ${no}`);+ if (!pass) failures++;+};++await page.getByRole("button", { name: "Upload", exact: true }).click();+const items = await page.getByRole("menuitem").allInnerTexts();+ok(+ JSON.stringify(items) === JSON.stringify(["Files…", "Folder…"]),+ "Upload offers files or a whole folder",+ `Upload offered ${JSON.stringify(items)}`+);+await page.getByRole("menuitem", { name: "Files…" }).click();+await page.waitForTimeout(1200);++const midway = await page.evaluate(() => window.__midway);+ok(/a\.jpg/.test(midway) && /50%/.test(midway) && /b\.jpg/.test(midway) && /Waiting/.test(midway),+ "while going up, each file shows by name with its own progress",+ `midway the tray said ${JSON.stringify(midway)}`);++const rows = await page.locator(".upload-row").evaluateAll((els) =>+ els.map((e) => ({ cls: e.className, text: e.innerText.replace(/\s+/g, " ") })));+ok(rows.length === 2 && /is-done/.test(rows[0].cls) && /is-failed/.test(rows[1].cls) &&+ /not filed/.test(rows[1].text),+ "each file ends with its own verdict, and a failure says why",+ `rows ended as ${JSON.stringify(rows)}`);+const head = await page.locator(".upload-tray-title").innerText();+ok(head === "1 of 2 didn’t upload", "the tray sums it up honestly", `the tray said "${head}"`);++const usable = await page.getByRole("button", { name: "Upload", exact: true }).isEnabled();+ok(usable, "the window stays usable, and another upload can start", "Upload was left disabled");++await page.getByRole("button", { name: "Upload", exact: true }).click();+await page.getByRole("menuitem", { name: "Folder…" }).click();+await page.waitForTimeout(1000);+const calls = await page.evaluate(() => window.__uploadCalls.map((c) => c.paths));+ok(JSON.stringify(calls[1]) === JSON.stringify(["/home/me/Holiday"]),+ "a chosen folder is handed over whole", `the folder upload sent ${JSON.stringify(calls)}`);++await b.close();+process.exitCode = failures === 0 ? 0 : 1;diff --git a/Cargo.lock b/Cargo.lockindex 20d2007..4d70807 100644--- a/Cargo.lock+++ b/Cargo.lock@@ -3182,6 +3182,7 @@ name = "quince-app"version = "0.1.0"dependencies = ["anyhow",+ "futures-util","jiff","quince-auth","quince-core",@@ -3246,6 +3247,7 @@ dependencies = ["reqwest 0.12.28","serde","serde_json",+ "sha1","thiserror 2.0.20","tokio","uuid",@@ -3274,6 +3276,8 @@ dependencies = ["reqwest 0.12.28","rpassword","serde_json",+ "sha1",+ "sha2","tokio","uuid",]diff --git a/check.sh b/check.shindex 3599c6d..2c6af47 100755--- a/check.sh+++ b/check.sh@@ -33,6 +33,7 @@ step "accessibility" node .uiverify/a11y.mjsstep "keyboard only" node .uiverify/keyboard.mjsstep "somewhere else" node .uiverify/elsewhere.mjsstep "writing a note" node .uiverify/notes-compose.mjs+step "uploading" node .uiverify/drive-upload.mjsprintf '\n'if [ "$fail" -eq 0 ]; then printf '\033[32mAll checks passed.\033[0m\n'; else printf '\033[31mSome checks failed.\033[0m\n'; fidiff --git a/crates/quince-icloud/Cargo.toml b/crates/quince-icloud/Cargo.tomlindex f6131b8..b4c7966 100644--- a/crates/quince-icloud/Cargo.toml+++ b/crates/quince-icloud/Cargo.toml@@ -17,6 +17,7 @@ flate2 = "1"uuid = { workspace = true }tokio = { workspace = true, features = ["fs", "io-util"] }futures-util = { version = "0.3", default-features = false, features = ["std"] }+sha1 = "0.10"[dev-dependencies]tokio = { workspace = true }diff --git a/crates/quince-icloud/src/drive.rs b/crates/quince-icloud/src/drive.rsindex b639da1..53c3d34 100644--- a/crates/quince-icloud/src/drive.rs+++ b/crates/quince-icloud/src/drive.rs@@ -437,6 +437,61 @@ pub async fn move_to(Ok(())}+#[cfg(test)]+mod checksum_tests {+ use super::*;+ use serde_json::json;++ fn pattern(size: usize) -> Vec<u8> {+ (0..size)+ .map(|i| u8::try_from(i * 31 % 251).unwrap())+ .collect()+ }++ /// Apple's own answers for these exact bytes, measured on real uploads.+ #[test]+ fn the_checksum_is_apples() {+ for (size, apple) in [+ (37, "AcbV5Q1Z72Y4XYc0UObScQwypTml"),+ (3 * 1024 * 1024, "AbnSbKOQ813hyTYHFLl6Dh/oYsl2"),+ ] {+ let bytes = pattern(size);+ let mut whole = AppleChecksum::default();+ whole.update(&bytes);+ assert_eq!(whole.finish(), apple, "{size} bytes");+ // Fed in pieces as a stream feeds it, the answer is the same.+ let mut pieces = AppleChecksum::default();+ for chunk in bytes.chunks(256 * 1024 + 7) {+ pieces.update(chunk);+ }+ assert_eq!(pieces.finish(), apple, "{size} bytes in pieces");+ }+ }++ fn filed(signature: &str, size: u64, code: i64) -> Value {+ json!({ "results": [{ "document": {+ "document_id": "06502863-6910-440B-A1B9-29536C125463",+ "name": "photo 2.jpg", "size": size,+ "data": { "signature": signature },+ "status": { "status_code": code, "error_message": "OK" }+ }}]})+ }++ /// Found by the id Apple gave it, whatever it was renamed to.+ #[test]+ fn a_filed_document_is_accepted_only_when_it_is_the_file_sent() {+ let sig = "AcbV5Q1Z72Y4XYc0UObScQwypTml";+ assert_eq!(+ filed_document(&filed(sig, 37, 0), sig, 37, "photo.jpg").unwrap(),+ "FILE::com.apple.CloudDocs::06502863-6910-440B-A1B9-29536C125463"+ );+ assert!(filed_document(&filed("Aother", 37, 0), sig, 37, "photo.jpg").is_err());+ assert!(filed_document(&filed(sig, 36, 0), sig, 37, "photo.jpg").is_err());+ assert!(filed_document(&filed(sig, 37, 1), sig, 37, "photo.jpg").is_err());+ assert!(filed_document(&json!({}), sig, 37, "photo.jpg").is_err());+ }+}+#[cfg(test)]mod created_folder_tests {use super::*;@@ -1053,19 +1108,60 @@ fn zone_of(drivewsid: &str) -> Option<&str> {/// The one zone a file uploaded from this machine belongs in.const DOCUMENTS_ZONE: &str = "com.apple.CloudDocs";-/// Put a file from this machine into a Drive folder.+/// Apple's checksum of a file's bytes, as its storage service reports it back.///-/// Three separate calls, because that is how Apple does it and skipping any of them leaves a-/// half-made file: ask `upload/web` for somewhere to put the bytes, post the bytes to the URL-/// it hands back, then tell `update/documents` to file the result under a name. Only the third-/// call makes the file exist; the first two leave nothing visible behind if they fail.-///-/// The bytes are streamed off disk rather than read into memory, so putting a film in Drive-/// costs the same as putting a note in it.+/// Measured against real uploads of 37 bytes and 3 MiB: the receipt's `fileChecksum` is a+/// `0x01` byte and then SHA-1 of the bytes behind a fixed salt, base64. Knowing it means an+/// upload can be proven intact — the bytes Apple holds are the ones on disk, not merely the+/// same number of them.+pub struct AppleChecksum(sha1::Sha1);++const CHECKSUM_SALT: &[u8] = b"com.apple.XattrObjectSalt\0com.apple.DataObjectSalt\0";++impl Default for AppleChecksum {+ fn default() -> Self {+ use sha1::Digest;+ let mut hash = sha1::Sha1::new();+ hash.update(CHECKSUM_SALT);+ Self(hash)+ }+}++impl AppleChecksum {+ pub fn update(&mut self, bytes: &[u8]) {+ use sha1::Digest;+ self.0.update(bytes);+ }++ #[must_use]+ pub fn finish(self) -> String {+ use base64::Engine;+ use sha1::Digest;+ let mut out = vec![0x01u8];+ out.extend_from_slice(&self.0.finalize());+ base64::engine::general_purpose::STANDARD.encode(out)+ }+}++/// How far one upload has got, for a progress bar.+pub type Progress = std::sync::Arc<dyn Fn(u64, u64) + Send + Sync>;++/// Milliseconds since the epoch for a file time, when the filesystem keeps one.+fn millis(time: std::io::Result<std::time::SystemTime>) -> Option<i64> {+ time.ok()+ .and_then(|t| t.duration_since(std::time::UNIX_EPOCH).ok())+ .and_then(|d| i64::try_from(d.as_millis()).ok())+}++/// Upload one file into a folder, and prove it arrived whole.///-/// # Errors-/// When the file cannot be read, when Apple refuses any of the three steps, or when the file-/// does not afterwards appear in the folder it was meant for.+/// Three steps, as iCloud.com does it: ask for somewhere to put the bytes, send them, then+/// file them under a name. Every one is checked rather than trusted — the receipt must carry+/// the checksum of exactly the bytes that were sent, and the filed document must carry it+/// again at the same size — and the folder is read back for the document by its id. A file+/// renamed on the way in because the name was taken ("photo 2.jpg") is found by id too, where+/// matching by name reported it missing, and an older file of the same name could stand in+/// for a failed one.pub async fn upload(http: &reqwest::Client,docws_root: &str,@@ -1073,7 +1169,10 @@ pub async fn upload(params: Vec<(String, String)>,folder_id: &str,path: &std::path::Path,+ progress: Progress,) -> Result<DriveItem> {+ use tokio::io::AsyncReadExt;+if folder_id.trim().is_empty() {return Err(Error::Protocol("no folder was named to put this in".into()));}@@ -1091,10 +1190,10 @@ pub async fn upload(.and_then(|n| n.to_str()).ok_or_else(|| Error::Protocol("that file has no name to give it".into()))?.to_string();- let size = tokio::fs::metadata(path)+ let meta = tokio::fs::metadata(path).await- .map_err(|e| Error::Protocol(format!("could not read {name}: {e}")))?- .len();+ .map_err(|e| Error::Protocol(format!("could not read {name}: {e}")))?;+ let size = meta.len();let docws = docws_root.trim_end_matches('/');// 1. Somewhere to put the bytes.@@ -1123,52 +1222,88 @@ pub async fn upload(.and_then(Value::as_str).ok_or_else(|| Error::Protocol("Apple's upload slot carried no document id".into()))?;- // 2. The bytes themselves, straight off disk.+ // 2. The bytes, straight off disk and never all in memory, checksummed and counted on the+ // way past so what was sent is known exactly.let file = tokio::fs::File::open(path).await.map_err(|e| Error::Protocol(format!("could not open {name}: {e}")))?;- let part = reqwest::multipart::Part::stream_with_length(reqwest::Body::from(file), size)- .file_name(name.clone())- .mime_str(content_type(&name))- .map_err(|e| Error::Protocol(format!("{name} has a content type Apple refused: {e}")))?;- let sent = http+ let checksum = std::sync::Arc::new(std::sync::Mutex::new(Some(AppleChecksum::default())));+ let sent = std::sync::Arc::new(std::sync::atomic::AtomicU64::new(0));+ let stream = futures_util::stream::try_unfold(+ (file, checksum.clone(), sent.clone(), progress.clone()),+ move |(mut file, checksum, sent, progress)| async move {+ let mut buf = vec![0u8; 256 * 1024];+ let n = file.read(&mut buf).await?;+ if n == 0 {+ return Ok::<_, std::io::Error>(None);+ }+ buf.truncate(n);+ if let Some(hash) = checksum.lock().ok().as_mut().and_then(|h| h.as_mut()) {+ hash.update(&buf);+ }+ let so_far = sent.fetch_add(n as u64, std::sync::atomic::Ordering::Relaxed) + n as u64;+ progress(so_far, size);+ Ok(Some((buf, (file, checksum, sent, progress))))+ },+ );+ let part =+ reqwest::multipart::Part::stream_with_length(reqwest::Body::wrap_stream(stream), size)+ .file_name(name.clone())+ .mime_str(content_type(&name))+ .map_err(|e| {+ Error::Protocol(format!("{name} has a content type Apple refused: {e}"))+ })?;+ let response = http.post(url).multipart(reqwest::multipart::Form::new().part("files", part)).send().await?;- let status = sent.status();- let text = sent.text().await.unwrap_or_default();+ let status = response.status();+ let text = response.text().await.unwrap_or_default();if !status.is_success() {return Err(Error::Protocol(format!("Apple would not take the file ({status}): {}",text.chars().take(160).collect::<String>())));}+ let local = checksum+ .lock()+ .ok()+ .and_then(|mut h| h.take())+ .map(AppleChecksum::finish)+ .ok_or_else(|| Error::Protocol(format!("{name} could not be checksummed")))?;+ if sent.load(std::sync::atomic::Ordering::Relaxed) != size {+ return Err(Error::Protocol(format!(+ "{name} changed on disk while it was being sent, so nothing was filed"+ )));+ }let receipt: Value = serde_json::from_str(&text).map_err(|e| Error::Protocol(format!("Apple's upload reply was not JSON: {e}")))?;let single = receipt.get("singleFile").ok_or_else(|| Error::Protocol("Apple took the bytes but described nothing".into()))?;+ if single.get("fileChecksum").and_then(Value::as_str) != Some(local.as_str()) {+ return Err(Error::Protocol(format!(+ "the bytes Apple received for {name} are not the file's, so it was not filed"+ )));+ }let of = |key: &str| single.get(key).cloned().unwrap_or(Value::Null);- // 3. File it under a name. Until this, the bytes belong to nothing.- let now = i64::try_from(- std::time::SystemTime::now()- .duration_since(std::time::UNIX_EPOCH)- .map(|d| d.as_millis())- .unwrap_or(0),- )- .unwrap_or(0);- post_json(+ // 3. File it under a name, with the file's own dates — as Finder keeps them — rather than+ // the moment it happened to be uploaded.+ let now = millis(Ok(std::time::SystemTime::now())).unwrap_or(0);+ let modified = millis(meta.modified()).unwrap_or(now);+ let created = millis(meta.created()).unwrap_or(modified);+ let filed = post_json(http,&format!("{docws}/ws/com.apple.CloudDocs/update/documents"),¶ms,&json!({// Apple renames rather than overwrites when the name is taken, which is what a- // file manager does and what a person expects.+ // file manager does and what a person expects: nothing already there is lost."allow_conflict": true,- "btime": now,- "mtime": now,+ "btime": created,+ "mtime": modified,"command": "add_file","create_short_guid": true,"document_id": document_id,@@ -1184,13 +1319,13 @@ pub async fn upload(}),).await?;+ let stored = filed_document(&filed, &local, size, &name)?;// Apple answers 200 to writes it did not make. The folder is the only witness that counts.let after = folder_listing(http, drivews_root, ¶ms, folder_id).await?;- let stem = name.rsplit_once('.').map_or(name.as_str(), |(s, _)| s);after.into_iter()- .find(|i| i.name == name || i.name == stem)+ .find(|i| i.drivewsid == stored).ok_or_else(|| {Error::Protocol(format!("Apple accepted {name} but the folder does not hold it"@@ -1198,6 +1333,29 @@ pub async fn upload(})}+/// The id of the document an `add_file` reply describes, once it is shown to be the file sent.+fn filed_document(reply: &Value, checksum: &str, size: u64, name: &str) -> Result<String> {+ let document = reply+ .pointer("/results/0/document")+ .ok_or_else(|| Error::Protocol(format!("Apple filed {name} without describing it")))?;+ let ok = document+ .pointer("/status/status_code")+ .and_then(Value::as_i64)+ == Some(0);+ let same_bytes = document.pointer("/data/signature").and_then(Value::as_str) == Some(checksum);+ let same_size = document.get("size").and_then(Value::as_u64) == Some(size);+ if !(ok && same_bytes && same_size) {+ return Err(Error::Protocol(format!(+ "what Apple filed as {name} is not the file that was sent"+ )));+ }+ let id = document+ .get("document_id")+ .and_then(Value::as_str)+ .ok_or_else(|| Error::Protocol(format!("Apple filed {name} without an id")))?;+ Ok(format!("FILE::{DOCUMENTS_ZONE}::{id}"))+}+/// A JSON POST to Drive that fails loudly rather than returning something unusable.async fn post_json(http: &reqwest::Client,@@ -1297,6 +1455,7 @@ mod app_library_tests {vec![],folder,&file,+ std::sync::Arc::new(|_, _| {}),).await.expect_err("an app's own library is not somewhere to put a file");@@ -1320,6 +1479,7 @@ mod app_library_tests {vec![],quince_core::drive::ROOT,&file,+ std::sync::Arc::new(|_, _| {}),).await.expect_err("nothing is listening, so this fails on the network");diff --git a/probe/Cargo.toml b/probe/Cargo.tomlindex b021d26..0afbaff 100644--- a/probe/Cargo.toml+++ b/probe/Cargo.toml@@ -6,6 +6,8 @@ license.workspace = truepublish = false[dependencies]+sha1 = "0.10"+sha2 = "0.10"anyhow.workspace = truereqwest.workspace = trueserde_json.workspace = truediff --git a/probe/examples/drive_trash.rs b/probe/examples/drive_trash.rsnew file mode 100644index 0000000..23ff983--- /dev/null+++ b/probe/examples/drive_trash.rs@@ -0,0 +1,33 @@+//! Throwaway: move named probe leftovers in the Drive root to the trash.+use anyhow::{anyhow, Result};++#[tokio::main]+async fn main() -> Result<()> {+ let dir = std::path::PathBuf::from(std::env::var("HOME")?).join(".local/share/quince-probe");+ let mut client = quince_auth::Client::new(&dir)?;+ if !client.resume().await? {+ return Err(anyhow!("no session"));+ }+ let drivews = client+ .session()+ .service_url("drivews")+ .ok_or_else(|| anyhow!("no drivews"))?;+ let http = client.http().clone();+ let params = client.service_params();+ let store = std::env::temp_dir().join(format!("quince-{}", uuid::Uuid::new_v4()));+ std::fs::create_dir_all(&store)?;+ let mut lib = quince_core::Library::open(&store)?;+ let folder = std::env::var("FOLDER").unwrap_or(quince_core::drive::ROOT.into());+ quince_icloud::drive::sync_folder(&http, &drivews, params.clone(), &mut lib, &folder).await?;+ let names: Vec<String> = std::env::args().skip(1).collect();+ for item in lib.drive_children(&folder)? {+ if names+ .iter()+ .any(|n| *n == item.display_name() || *n == item.name)+ {+ quince_icloud::drive::delete(&http, &drivews, params.clone(), &item).await?;+ println!("trashed {}", item.display_name());+ }+ }+ Ok(())+}diff --git a/probe/examples/drive_upload.rs b/probe/examples/drive_upload.rsindex 720301d..111224a 100644--- a/probe/examples/drive_upload.rs+++ b/probe/examples/drive_upload.rs@@ -28,8 +28,16 @@ async fn main() -> Result<()> {println!("local file: {} bytes", body.len());// Into the root.- let landed =- quince_icloud::drive::upload(&http, &docws, &drivews, params.clone(), ROOT, &tmp).await?;+ let landed = quince_icloud::drive::upload(+ &http,+ &docws,+ &drivews,+ params.clone(),+ ROOT,+ &tmp,+ std::sync::Arc::new(|_, _| {}),+ )+ .await?;println!("root: {} ({:?}) {} bytes parent={:?}",landed.name,@@ -67,6 +75,7 @@ async fn main() -> Result<()> {params.clone(),&folder.drivewsid,&tmp,+ std::sync::Arc::new(|_, _| {}),).await?;println!("folder: {} in {:?}", inner.name, inner.parent_id);diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.tomlindex aa9e4e8..c3276aa 100644--- a/src-tauri/Cargo.toml+++ b/src-tauri/Cargo.toml@@ -9,6 +9,7 @@ license.workspace = truetauri-build = { version = "2", features = [] }[dependencies]+futures-util = { version = "0.3", default-features = false, features = ["std"] }tauri = { version = "2", features = [] }quince-auth = { path = "../crates/quince-auth" }quince-core = { path = "../crates/quince-core" }diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rsindex f96352c..8abd39d 100644--- a/src-tauri/src/main.rs+++ b/src-tauri/src/main.rs@@ -1056,7 +1056,152 @@ async fn drive_download(Ok(destination.to_string_lossy().into_owned())}-/// Create a folder, and reflect it locally without waiting for the next full sync.+/// Where an upload's progress is announced, one event per file as it moves.+const UPLOAD: &str = "quince://upload";++/// One file's upload, as the window shows it.+#[derive(serde::Serialize, Clone)]+#[serde(rename_all = "camelCase")]+struct UploadEvent {+ /// Unique within the batch the window started.+ id: String,+ batch: String,+ name: String,+ /// The folders it sits in below the one it was dropped on, for a folder upload.+ within: String,+ sent: u64,+ total: u64,+ /// `waiting`, `sending`, `verifying`, `done` or `failed`.+ state: &'static str,+ error: Option<String>,+}++/// One file to send, and the Drive folder it goes into.+struct UploadJob {+ event: UploadEvent,+ path: std::path::PathBuf,+ folder: String,+}++/// The Drive folder called `name` inside `parent`: the one already there, or a new one.+///+/// Dropping a folder that is already in iCloud adds to it, as Finder offers to, rather than+/// making "Photos 2" beside it. Files inside that already exist are kept too: Apple renames+/// the newcomer rather than overwrite.+async fn folder_for(+ http: &reqwest::Client,+ root: &str,+ params: &[(String, String)],+ data: &State<'_, Data>,+ parent: &str,+ name: &str,+) -> Result<String, String> {+ resync_drive_folder(http, root, params.to_vec(), data, parent).await?;+ let existing = {+ let library = data.0.lock().await;+ library+ .drive_children(parent)+ .map_err(|e| e.to_string())?+ .into_iter()+ .find(|i| i.is_container() && i.name.eq_ignore_ascii_case(name))+ };+ if let Some(folder) = existing {+ return Ok(folder.drivewsid);+ }+ quince_icloud::drive::create_folder(http, root, params.to_vec(), parent, name)+ .await+ .map(|f| f.drivewsid)+ .map_err(|e| e.to_string())+}++/// Every file under the paths dropped or chosen, with folders made to match on the way.+///+/// Links are not followed: one that points back up the tree would never finish, and one that+/// points outside it would upload something nobody chose.+#[allow(clippy::too_many_arguments)]+async fn plan_upload(+ http: &reqwest::Client,+ root: &str,+ params: &[(String, String)],+ data: &State<'_, Data>,+ batch: &str,+ folder_id: &str,+ paths: &[String],+ failed: &mut Vec<String>,+) -> Vec<UploadJob> {+ let mut jobs = Vec::new();+ // (path on disk, Drive folder it goes into, folders below the drop target)+ let mut pending: Vec<(std::path::PathBuf, String, String)> = paths+ .iter()+ .map(|p| {+ (+ std::path::PathBuf::from(p),+ folder_id.to_string(),+ String::new(),+ )+ })+ .collect();+ while let Some((path, into, within)) = pending.pop() {+ let name = path.file_name().map_or_else(+ || path.display().to_string(),+ |n| n.to_string_lossy().into(),+ );+ let meta = match tokio::fs::symlink_metadata(&path).await {+ Ok(meta) => meta,+ Err(e) => {+ failed.push(format!("{name}: {e}"));+ continue;+ }+ };+ if meta.is_dir() {+ let folder = match folder_for(http, root, params, data, &into, &name).await {+ Ok(folder) => folder,+ Err(e) => {+ failed.push(format!("{name}: {e}"));+ continue;+ }+ };+ let below = if within.is_empty() {+ name.clone()+ } else {+ format!("{within}/{name}")+ };+ let mut entries = match tokio::fs::read_dir(&path).await {+ Ok(entries) => entries,+ Err(e) => {+ failed.push(format!("{name}: {e}"));+ continue;+ }+ };+ let mut children = Vec::new();+ while let Ok(Some(entry)) = entries.next_entry().await {+ children.push(entry.path());+ }+ // Popped from the end, so reversed to go in name order.+ children.sort();+ for child in children.into_iter().rev() {+ pending.push((child, folder.clone(), below.clone()));+ }+ } else if meta.is_file() {+ jobs.push(UploadJob {+ event: UploadEvent {+ id: format!("{batch}:{}", jobs.len()),+ batch: batch.to_string(),+ name,+ within,+ sent: 0,+ total: meta.len(),+ state: "waiting",+ error: None,+ },+ path,+ folder: into,+ });+ }+ }+ jobs+}+/// Put files from this machine into a Drive folder.////// Paths, never bytes. The interface hands over what the person chose — from the file dialog@@ -1065,13 +1210,23 @@ async fn drive_download(////// Each file is reported on separately: one that Apple refuses must not take the rest of a/// dragged selection down with it.+///+/// Folders are uploaded whole, and each file's progress is announced as it moves.+///+/// Three at a time: enough to keep the connection busy through each file's round trips+/// without asking Apple for dozens of upload slots at once. Every file is proven intact+/// before it counts as done — see `quince_icloud::drive::upload`. Returns what failed.#[tauri::command]async fn drive_upload(+ app: tauri::AppHandle,+ batch: String,folder_id: String,paths: Vec<String>,auth: State<'_, Auth>,data: State<'_, Data>,) -> UiResult<Vec<String>> {+ use futures_util::StreamExt;+let (http, root, params) = drive_context(&auth).await?;let docws = {let client = auth.client.lock().await;@@ -1082,24 +1237,92 @@ async fn drive_upload(};let mut failed = Vec::new();- for path in &paths {- let path = std::path::Path::new(path);- let name = path.file_name().map_or_else(- || path.display().to_string(),- |n| n.to_string_lossy().into(),- );- if let Err(e) =- quince_icloud::drive::upload(&http, &docws, &root, params.clone(), &folder_id, path)- .await- {- failed.push(format!("{name}: {e}"));- }+ let jobs = plan_upload(+ &http,+ &root,+ ¶ms,+ &data,+ &batch,+ &folder_id,+ &paths,+ &mut failed,+ )+ .await;+ for job in &jobs {+ app.emit(UPLOAD, &job.event).ok();}+ let results: Vec<Option<String>> = futures_util::stream::iter(jobs)+ .map(|job| {+ let (app, http, docws, root, params) = (+ app.clone(),+ http.clone(),+ docws.clone(),+ root.clone(),+ params.clone(),+ );+ async move {+ let mut event = job.event.clone();+ event.state = "sending";+ app.emit(UPLOAD, &event).ok();++ // Told about every quarter-megabyte, passed on at every whole percent: a+ // smooth bar without a flood of messages for a large file.+ let last = std::sync::Arc::new(std::sync::atomic::AtomicU64::new(0));+ let progress: quince_icloud::drive::Progress = {+ let (app, event, last) = (app.clone(), event.clone(), last.clone());+ std::sync::Arc::new(move |sent: u64, total: u64| {+ let step = (total / 100).max(1);+ let before = last.load(std::sync::atomic::Ordering::Relaxed);+ if sent / step != before / step || sent == total {+ last.store(sent, std::sync::atomic::Ordering::Relaxed);+ let mut e = event.clone();+ e.sent = sent;+ e.state = if sent >= total {+ "verifying"+ } else {+ "sending"+ };+ app.emit(UPLOAD, &e).ok();+ }+ })+ };+ let outcome = quince_icloud::drive::upload(+ &http,+ &docws,+ &root,+ params,+ &job.folder,+ &job.path,+ progress,+ )+ .await;+ event.sent = event.total;+ match outcome {+ Ok(_) => {+ event.state = "done";+ app.emit(UPLOAD, &event).ok();+ None+ }+ Err(e) => {+ event.state = "failed";+ event.error = Some(e.to_string());+ app.emit(UPLOAD, &event).ok();+ Some(format!("{}: {e}", event.name))+ }+ }+ }+ })+ .buffer_unordered(3)+ .collect()+ .await;+ failed.extend(results.into_iter().flatten());+resync_drive_folder(&http, &root, params, &data, &folder_id).await?;Ok(failed)}+/// Create a folder, and reflect it locally without waiting for the next full sync.#[tauri::command]async fn drive_create_folder(parent_id: String,diff --git a/src/components/UploadTray.tsx b/src/components/UploadTray.tsxnew file mode 100644index 0000000..6a640de--- /dev/null+++ b/src/components/UploadTray.tsx@@ -0,0 +1,97 @@+import type { UploadEvent } from "../lib/ipc";+import { FileGlyph } from "./FileGlyph";++/** What a person reads about one file, in the fewest words that are still true. */+function status(u: UploadEvent): string {+ switch (u.state) {+ case "waiting":+ return "Waiting";+ case "sending":+ return `${Math.floor((u.sent / Math.max(u.total, 1)) * 100)}%`;+ case "verifying":+ return "Checking";+ case "done":+ return "Uploaded";+ case "failed":+ return "Didn’t upload";+ }+}++const extensionOf = (name: string) => {+ const dot = name.lastIndexOf(".");+ return dot > 0 ? name.slice(dot + 1) : null;+};++/**+ * Every file going up, each with its own progress, in the corner like a download list.+ *+ * "Uploading…" on a button said nothing about which of forty photos had gone and which had+ * not. This names each one, shows how far it has got, and only calls it uploaded once iCloud+ * has been shown to hold exactly its bytes — so "Uploaded" is a statement, not a hope.+ */+export function UploadTray({+ uploads,+ onDismiss,+}: {+ uploads: UploadEvent[];+ onDismiss: () => void;+}) {+ if (uploads.length === 0) return null;+ const finished = uploads.filter((u) => u.state === "done" || u.state === "failed");+ const failed = uploads.filter((u) => u.state === "failed").length;+ const active = finished.length < uploads.length;+ const total = uploads.reduce((n, u) => n + u.total, 0);+ const sent = uploads.reduce((n, u) => n + (u.state === "done" ? u.total : u.sent), 0);+ const percent = total > 0 ? Math.floor((sent / total) * 100) : 100;++ const heading = active+ ? `Uploading ${finished.length + 1 > uploads.length ? uploads.length : finished.length + 1} of ${uploads.length}`+ : failed > 0+ ? `${failed} of ${uploads.length} didn’t upload`+ : uploads.length === 1+ ? "Uploaded"+ : `${uploads.length} uploaded`;++ return (+ <section className="upload-tray" aria-label="Uploads" aria-live="polite">+ <header className="upload-tray-head">+ <span className="upload-tray-title">{heading}</span>+ {active ? (+ <span className="upload-tray-percent">{percent}%</span>+ ) : (+ <button className="upload-tray-close" onClick={onDismiss} aria-label="Close uploads">+ Done+ </button>+ )}+ </header>+ {active && (+ <div className="upload-bar" aria-hidden="true">+ <span style={{ width: `${percent}%` }} />+ </div>+ )}+ <ul className="upload-rows">+ {uploads.map((u) => (+ <li key={u.id} className={`upload-row is-${u.state}`}>+ <span className="upload-glyph" aria-hidden="true">+ <FileGlyph kind="FILE" extension={extensionOf(u.name)} />+ </span>+ <span className="upload-text">+ <span className="upload-name">{u.name}</span>+ <span className="upload-where">+ {u.state === "failed" && u.error ? u.error : u.within || " "}+ </span>+ {(u.state === "sending" || u.state === "verifying") && (+ <span className="upload-bar small" aria-hidden="true">+ <span style={{ width: `${Math.floor((u.sent / Math.max(u.total, 1)) * 100)}%` }} />+ </span>+ )}+ </span>+ <span className="upload-state">+ {u.state === "done" ? <span aria-label="Uploaded">✓</span> : status(u)}+ </span>+ </li>+ ))}+ </ul>+ </section>+ );+}diff --git a/src/lib/ipc.ts b/src/lib/ipc.tsindex 3dbea43..8ade3dd 100644--- a/src/lib/ipc.ts+++ b/src/lib/ipc.ts@@ -31,6 +31,22 @@ export type Refresh = {};/** Emitted by the backend whenever a refresh finishes, background ones included. */+/** Where each uploading file's progress is announced. */+export const UPLOAD = "quince://upload";++/** One file's upload, as it moves. */+export type UploadEvent = {+ id: string;+ batch: string;+ name: string;+ /** The folders it sits in below the one it was dropped on. */+ within: string;+ sent: number;+ total: number;+ state: "waiting" | "sending" | "verifying" | "done" | "failed";+ error: string | null;+};+export const SYNCED = "quince://synced";export const api = {@@ -126,8 +142,9 @@ export const api = {* Resolves with the files that did not go up, one line each, so a single refusal does not* read as the whole upload failing.*/- driveUpload: (folderId: string, paths: string[]) =>- invoke<string[]>("drive_upload", { folderId, paths }),+ /** Files and folders into a Drive folder. Progress arrives as UPLOAD events. */+ driveUpload: (batch: string, folderId: string, paths: string[]) =>+ invoke<string[]>("drive_upload", { batch, folderId, paths }),driveCreateFolder: (parentId: string, name: string) =>invoke<void>("drive_create_folder", { parentId, name }),driveRename: (drivewsid: string, name: string) =>diff --git a/src/screens/DriveView.tsx b/src/screens/DriveView.tsxindex 6627c6c..5d4090c 100644--- a/src/screens/DriveView.tsx+++ b/src/screens/DriveView.tsx@@ -8,9 +8,13 @@ import {isContainer,messageOf,DRIVE_ROOT,+ UPLOAD,type DriveItem,+ type UploadEvent,} from "../lib/ipc";import { ActionGlyph, FileGlyph } from "../components/FileGlyph";+import { UploadTray } from "../components/UploadTray";+import { listen } from "@tauri-apps/api/event";const KB = 1024;@@ -52,7 +56,6 @@ export function DriveView() {const [newFolderName, setNewFolderName] = useState("");/** The item a Move dialog is open for, if any. */const [moving, setMoving] = useState<DriveItem | null>(null);- const [uploading, setUploading] = useState(false);/** True while files are held over the window, so the drop target can say so. */const [dropping, setDropping] = useState(false);@@ -194,45 +197,80 @@ export function DriveView() {* One file Apple refuses must not look like all of them failing, so each is reported by* name and the rest still land.*/+ /** Every file sent from this window, until the tray is closed. */+ const [uploads, setUploads] = useState<Map<string, UploadEvent>>(new Map());+ const [uploadMenu, setUploadMenu] = useState(false);++ useEffect(() => {+ let stop: (() => void) | undefined;+ let cancelled = false;+ listen<UploadEvent>(UPLOAD, (event) => {+ const u = event.payload;+ setUploads((was) => {+ const next = new Map(was);+ const before = next.get(u.id);+ // Events for one file can pass each other; a finished file never goes back.+ if (before && (before.state === "done" || before.state === "failed")) return was;+ next.set(u.id, u);+ return next;+ });+ })+ .then((unlisten) => {+ // Stopping may throw where there is no event system behind the page — a plain+ // browser running the interface — and a screen closing must not throw for that.+ const quietly = () => {+ try {+ // It answers with a promise, whose failure would otherwise escape this try.+ void Promise.resolve(unlisten() as unknown).catch(() => {});+ } catch {+ // Nothing was listening.+ }+ };+ if (cancelled) quietly();+ else stop = quietly;+ })+ .catch(() => {});+ return () => {+ cancelled = true;+ stop?.();+ };+ }, []);+const upload = useCallback(async (paths: string[]) => {if (paths.length === 0) return;- setUploading(true);setError(null);+ // Never blocks the window: browse, open another folder, start another upload.+ const batch = `${Date.now().toString(36)}-${Math.random().toString(36).slice(2, 7)}`;+ const into = here.id;try {- const failed = await api.driveUpload(here.id, paths);- await refresh();- if (failed.length > 0) {- setError(- failed.length === paths.length- ? failed.join("\n")- : `${failed.length} of ${paths.length} did not go up — ${failed.join("; ")}`- );- } else {- setSavedTo(- paths.length === 1- ? `Uploaded ${paths[0].split("/").pop()}`- : `Uploaded ${paths.length} files`- );- }+ await api.driveUpload(batch, into, paths);+ // What failed is named in the tray, file by file; the folder is re-read either way.+ if (into === here.id) await refresh();} catch (e) {setError(messageOf(e));- } finally {- setUploading(false);}},[here.id, refresh]);- const chooseAndUpload = useCallback(async () => {- try {- const chosen = await openFileDialog({ multiple: true, title: "Upload to iCloud Drive" });- if (!chosen) return;- await upload(Array.isArray(chosen) ? chosen : [chosen]);- } catch (e) {- setError(messageOf(e));- }- }, [upload]);+ const chooseAndUpload = useCallback(+ async (folders: boolean) => {+ setUploadMenu(false);+ try {+ const chosen = await openFileDialog({+ multiple: true,+ directory: folders,+ title: folders ? "Upload folders to iCloud Drive" : "Upload to iCloud Drive",+ });+ if (!chosen) return;+ await upload(Array.isArray(chosen) ? chosen : [chosen]);+ } catch (e) {+ setError(messageOf(e));+ }+ },+ [upload]+ );// Dropping files on the window uploads them to the folder that is open. Tauri hands over// paths rather than contents, so a drop costs nothing until the file is actually read.@@ -326,19 +364,37 @@ export function DriveView() {New Folder</button>{/* An app's own library takes files from the app, not from here. */}- <button- className="primary with-glyph"- onClick={() => void chooseAndUpload()}- disabled={loading || uploading || !acceptsUploads(here.id)}- title={- acceptsUploads(here.id)- ? undefined- : "This folder belongs to an app, which is the only thing that can add to it"- }- >- <ActionGlyph name="upload" />- {uploading ? "Uploading…" : "Upload"}- </button>+ <span className="upload-menu-wrap">+ <button+ className="primary with-glyph"+ aria-haspopup="menu"+ aria-expanded={uploadMenu}+ onClick={() => setUploadMenu((open) => !open)}+ disabled={loading || !acceptsUploads(here.id)}+ title={+ acceptsUploads(here.id)+ ? undefined+ : "This folder belongs to an app, which is the only thing that can add to it"+ }+ >+ <ActionGlyph name="upload" />+ Upload+ </button>+ {uploadMenu && (+ <span+ className="upload-menu"+ role="menu"+ onKeyDown={(e) => e.key === "Escape" && setUploadMenu(false)}+ >+ <button role="menuitem" autoFocus onClick={() => void chooseAndUpload(false)}>+ Files…+ </button>+ <button role="menuitem" onClick={() => void chooseAndUpload(true)}>+ Folder…+ </button>+ </span>+ )}+ </span></div></header>@@ -496,6 +552,8 @@ export function DriveView() {onMove={(destinationId) => void move(moving, destinationId)}/>)}++ <UploadTray uploads={[...uploads.values()]} onDismiss={() => setUploads(new Map())} /></div>);}diff --git a/src/styles/app.css b/src/styles/app.cssindex 82bb3a8..6cd1c5b 100644--- a/src/styles/app.css+++ b/src/styles/app.css@@ -1984,3 +1984,112 @@ button.cal-day-item:hover { background: color-mix(in srgb, var(--sheet) 92%, var}.notes-search::placeholder { color: var(--text-3); }.notes-search:focus-visible { outline: none; box-shadow: 0 0 0 3px var(--gold-ring); }++/* ---- uploads ---------------------------------------------------------------- */++.upload-menu-wrap { position: relative; display: inline-flex; }++/* A menu, not a sheet: two choices under the button that opened them. */+.upload-menu {+ position: absolute;+ top: calc(100% + 6px);+ right: 0;+ z-index: 20;+ display: flex;+ flex-direction: column;+ min-width: 150px;+ padding: 5px;+ border: 1px solid var(--glass-edge);+ border-radius: 10px;+ background: var(--sheet);+ box-shadow: var(--shadow-sheet);+}+.upload-menu button {+ padding: 6px 10px;+ border: 0;+ border-radius: 6px;+ background: none;+ color: var(--text);+ font: inherit;+ font-size: var(--t-13);+ text-align: left;+ cursor: default;+}+.upload-menu button:hover,+.upload-menu button:focus-visible { background: var(--gold); color: #fff; outline: none; }++/* The corner list of files going up, like a browser's downloads. */+.upload-tray {+ position: fixed;+ right: 20px;+ bottom: 20px;+ z-index: 30;+ width: min(360px, calc(100vw - 32px));+ max-height: min(420px, 60vh);+ display: flex;+ flex-direction: column;+ border: 1px solid var(--glass-edge);+ border-radius: 12px;+ background: var(--glass);+ -webkit-backdrop-filter: var(--glass-blur);+ backdrop-filter: var(--glass-blur);+ box-shadow: var(--shadow-sheet), var(--glass-specular);+ overflow: hidden;+}+.upload-tray-head {+ display: flex;+ align-items: baseline;+ justify-content: space-between;+ gap: 12px;+ padding: 12px 14px 8px;+}+.upload-tray-title { font-size: var(--t-13); font-weight: 600; }+.upload-tray-percent { color: var(--text-2); font-size: var(--t-13); font-variant-numeric: tabular-nums; }+.upload-tray-close {+ padding: 2px 10px;+ border: 0;+ border-radius: var(--r-pill);+ background: color-mix(in srgb, var(--text) 10%, transparent);+ color: var(--text);+ font: inherit;+ font-size: var(--t-11);+ font-weight: 600;+ cursor: default;+}+.upload-tray-close:focus-visible { outline: 3px solid var(--gold-ring); }++.upload-bar {+ height: 4px;+ margin: 0 14px 8px;+ border-radius: var(--r-pill);+ background: color-mix(in srgb, var(--text) 10%, transparent);+ overflow: hidden;+}+.upload-bar > span {+ display: block;+ height: 100%;+ border-radius: inherit;+ background: var(--gold);+ transition: width 200ms var(--ease);+}+.upload-bar.small { height: 3px; margin: 4px 0 0; }++.upload-rows { margin: 0; padding: 0 6px 8px; list-style: none; overflow-y: auto; }+.upload-row {+ display: grid;+ grid-template-columns: 22px minmax(0, 1fr) auto;+ gap: 8px;+ align-items: center;+ padding: 6px 8px;+ border-radius: 8px;+}+.upload-glyph { display: grid; place-items: center; }+.upload-text { display: flex; flex-direction: column; min-width: 0; }+.upload-name,+.upload-where { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }+.upload-name { font-size: var(--t-13); }+.upload-where { color: var(--text-3); font-size: var(--t-11); }+.upload-state { color: var(--text-2); font-size: var(--t-11); font-variant-numeric: tabular-nums; }+.upload-row.is-done .upload-state { color: var(--gold-strong); font-size: var(--t-13); font-weight: 700; }+.upload-row.is-failed .upload-state,+.upload-row.is-failed .upload-where { color: var(--danger); }