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: put files from this computer into an app's library, filed in its own zone
Yashwanth Kamireddi0b48fa8a80a1
diff --git a/.uiverify/drive-upload.mjs b/.uiverify/drive-upload.mjsindex 710b88c..7f5738c 100644--- a/.uiverify/drive-upload.mjs+++ b/.uiverify/drive-upload.mjs@@ -104,5 +104,53 @@ 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)}`);+// An app's library (Obsidian's vault, Shortcuts') takes files from this computer too, filed in+// its own zone by the backend (rclone/rclone#9729): Upload is offered there, and sends into it.+{+ const VAULT = "FOLDER::iCloud.md.obsidian::documents";+ const appCtx = await b.newContext({ viewport: { width: 1280, height: 820 } });+ await appCtx.addInitScript((vault) => {+ 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 "library";+ if (cmd === "account_services") return ["drivews", "docws"];+ if (cmd === "plugin:event|listen") return 0;+ if (cmd === "pick_open" || cmd === "plugin:dialog|open") return ["/home/me/Note.md"];+ if (cmd === "drive_children")+ return (args?.folderId ?? args?.parentId ?? args?.folder ?? null) === vault+ ? []+ : [{ drivewsid: vault, parentId: null, name: "Obsidian", extension: null,+ kind: "APP_LIBRARY", size: null, childCount: 0, createdAt: 1780000000,+ modifiedAt: null, etag: null }];+ if (cmd === "drive_item") return null;+ if (cmd === "drive_sync_folder") return 0;+ if (cmd === "drive_upload") { window.__uploadCalls.push(args); return []; }+ if (cmd === "sync_all" || cmd === "sync_status") return { services: [], asOf: 1, tookMs: 0 };+ return [];+ },+ };+ }, VAULT);+ const app = await appCtx.newPage();+ await app.goto("http://localhost:4173", { waitUntil: "networkidle" });+ await app.locator(".nav-item", { hasText: "Drive" }).click();+ await app.locator(".drive-row-hit", { hasText: "Obsidian" }).dblclick();+ await app.waitForTimeout(400);+ const button = app.getByRole("button", { name: "Upload", exact: true });+ const offered = await button.isEnabled();+ ok(offered, "an app's library offers Upload", "Upload was disabled in an app's library");+ if (offered) {+ await button.click();+ await app.getByRole("menuitem", { name: "Files" }).click();+ await app.waitForTimeout(500);+ }+ const into = await app.evaluate(() => window.__uploadCalls.map((c) => c.folderId));+ ok(JSON.stringify(into) === JSON.stringify([VAULT]),+ "and the file goes into that library", `uploads went to ${JSON.stringify(into)}`);+ await appCtx.close();+}+await b.close();process.exitCode = failures === 0 ? 0 : 1;diff --git a/crates/quince-icloud/src/drive.rs b/crates/quince-icloud/src/drive.rsindex 0ebaa48..484d52d 100644--- a/crates/quince-icloud/src/drive.rs+++ b/crates/quince-icloud/src/drive.rs@@ -1545,13 +1545,20 @@ mod checksum_tests {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(),+ filed_document(&filed(sig, 37, 0), sig, 37, "photo.jpg", DOCUMENTS_ZONE).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());+ assert!(filed_document(+ &filed("Aother", 37, 0),+ sig,+ 37,+ "photo.jpg",+ DOCUMENTS_ZONE+ )+ .is_err());+ assert!(filed_document(&filed(sig, 36, 0), sig, 37, "photo.jpg", DOCUMENTS_ZONE).is_err());+ assert!(filed_document(&filed(sig, 37, 1), sig, 37, "photo.jpg", DOCUMENTS_ZONE).is_err());+ assert!(filed_document(&json!({}), sig, 37, "photo.jpg", DOCUMENTS_ZONE).is_err());}}@@ -2540,7 +2547,7 @@ fn zone_of(drivewsid: &str) -> Option<&str> {(!zone.is_empty()).then_some(zone)}-/// The one zone a file uploaded from this machine belongs in.+/// The ordinary iCloud Drive's zone: everything not in an app's own library.const DOCUMENTS_ZONE: &str = "com.apple.CloudDocs";/// Apple's checksum of a file's bytes, as its storage service reports it back.@@ -2647,15 +2654,12 @@ async fn upload_once(if folder_id.trim().is_empty() {return Err(Error::Protocol("no folder was named to put this in".into()));}- // An app library belongs to the app that made it, and `update/documents` names the folder- // by a bare id with no zone beside it — so a file meant for one would be filed by an id- // that means something different in the documents zone, or nothing at all. Refusing is the- // honest answer; quietly putting someone's file somewhere else is not.- if zone_of(folder_id).is_some_and(|zone| zone != DOCUMENTS_ZONE) {- return Err(Error::Protocol(- "that folder belongs to an app, and only the app can put things in it".into(),- ));- }+ // Filed in the folder's own zone. An app library (Obsidian's vault, Shortcuts', Keynote's)+ // is a zone of its own, and `update/documents` names the folder by a bare id — so asked+ // of the documents zone, Apple looked for that id there and refused with "out of order+ // children ... parents were missing" (rclone/rclone#9729). Asked of the folder's own zone,+ // the zone in the path says where the id is (rclone/rclone#9731).+ let zone = zone_of(folder_id).unwrap_or(DOCUMENTS_ZONE);let name = path.file_name().and_then(|n| n.to_str())@@ -2670,7 +2674,7 @@ async fn upload_once(// 1. Somewhere to put the bytes.let offered: Value = post_json(http,- &format!("{docws}/ws/com.apple.CloudDocs/upload/web"),+ &format!("{docws}/ws/{zone}/upload/web"),¶ms,&json!({"filename": name,@@ -2770,7 +2774,7 @@ async fn upload_once(let created = millis(meta.created()).unwrap_or(modified);let filed = post_json(http,- &format!("{docws}/ws/com.apple.CloudDocs/update/documents"),+ &format!("{docws}/ws/{zone}/update/documents"),¶ms,&json!({// Apple renames rather than overwrites when the name is taken, which is what a@@ -2794,7 +2798,7 @@ async fn upload_once().await;let stored = match filed {- Ok(filed) => filed_document(&filed, &local, size, &name)?,+ Ok(filed) => filed_document(&filed, &local, size, &name, zone)?,// No answer is not "not filed": Apple may have filed it and the reply been lost. Asked// again from the start, the file would go up a second time beside the first — "photo// 2.jpg". The slot's document id is this file's alone, so the folder says which.@@ -2803,7 +2807,7 @@ async fn upload_once(// asked for by its own id, which is current the moment it is filed, where the folder's// listing can lag behind it.Err(e @ (Error::Network(_) | Error::Refused(_))) => {- let id = format!("FILE::{DOCUMENTS_ZONE}::{document_id}");+ let id = format!("FILE::{zone}::{document_id}");match item_details(http, drivews_root, ¶ms, &id).await {Ok(Some(item)) if item.size == Some(size as i64) => id,_ => match folder_listing(http, drivews_root, ¶ms, folder_id).await {@@ -2898,8 +2902,15 @@ async fn item_details(.and_then(item_from))}-/// 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> {+/// The id of the document an `add_file` reply describes, once it is shown to be the file sent:+/// in `zone`, the folder's own, where it was filed.+fn filed_document(+ reply: &Value,+ checksum: &str,+ size: u64,+ name: &str,+ zone: &str,+) -> Result<String> {let document = reply.pointer("/results/0/document").ok_or_else(|| Error::Protocol(format!("Apple filed {name} without describing it")))?;@@ -2918,7 +2929,7 @@ fn filed_document(reply: &Value, checksum: &str, size: u64, name: &str) -> Resul.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}"))+ Ok(format!("FILE::{zone}::{id}"))}/// A JSON POST to Drive that fails loudly rather than returning something unusable.@@ -3096,35 +3107,81 @@ mod upload_tests {mod app_library_tests {use super::*;- /// Everyone has app libraries — Keynote, Shortcuts, whatever third-party apps they use —- /// and a file meant for one would be filed by a bare id that means something else in the- /// documents zone. Refusing beats putting someone's file somewhere they will not find it.+ /// A file put in an app's library — Obsidian's vault, Shortcuts' — is asked a slot of and+ /// filed in that library's own zone, under the folder's id there, and comes back with an id+ /// in that zone (rclone/rclone#9729). Asked of the documents zone, Apple refused it: the+ /// folder's id is not there.#[tokio::test]- async fn a_file_is_not_uploaded_into_an_app_library() {- let file = std::env::temp_dir().join("quince-zone-test.txt");- std::fs::write(&file, b"x").unwrap();- for folder in [- "FOLDER::com.apple.Keynote::documents",- "FOLDER::iCloud.is.workflow.my.workflows::documents",- "FOLDER::iCloud.dk.simonbs.Scriptable::documents",- ] {- let err = upload(- &reqwest::Client::new(),- "http://127.0.0.1:1",- "http://127.0.0.1:1",- vec![],- folder,- &file,- std::sync::Arc::new(|_, _| {}),+ async fn a_file_is_filed_in_its_app_librarys_own_zone() {+ use wiremock::matchers::{method, path};+ use wiremock::{Mock, MockServer, ResponseTemplate};+ const ZONE: &str = "iCloud.md.obsidian";+ let server = MockServer::start().await;+ let mut sum = AppleChecksum::default();+ sum.update(b"# vault");+ let sum = sum.finish();+ Mock::given(method("POST"))+ .and(path(format!("/ws/{ZONE}/upload/web")))+ .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!([+ { "url": format!("{}/content", server.uri()), "document_id": "D1" }+ ])))+ .mount(&server)+ .await;+ Mock::given(method("POST"))+ .and(path("/content"))+ .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({+ "singleFile": { "fileChecksum": sum, "size": 7, "receipt": "r" }+ })))+ .mount(&server)+ .await;+ Mock::given(method("POST"))+ .and(path(format!("/ws/{ZONE}/update/documents")))+ .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({+ "results": [{ "document": {+ "document_id": "D1", "size": 7,+ "status": { "status_code": 0 }, "data": { "signature": sum }+ } }]+ })))+ .mount(&server)+ .await;+ let folder = format!("FOLDER::{ZONE}::documents");+ Mock::given(method("POST"))+ .and(path("/retrieveItemDetails"))+ .respond_with(+ ResponseTemplate::new(200).set_body_json(serde_json::json!({ "items": [{+ "drivewsid": format!("FILE::{ZONE}::D1"), "parentId": folder,+ "name": "Note", "extension": "md", "type": "FILE", "size": 7, "status": "OK"+ }]})),)- .await- .expect_err("an app's own library is not somewhere to put a file");- assert!(- err.to_string().contains("only the app"),- "the reason has to be the zone, not a network failure: {err}"- );- }+ .mount(&server)+ .await;+ let file = std::env::temp_dir().join("quince-app-zone-Note.md");+ std::fs::write(&file, b"# vault").unwrap();+ let item = upload(+ &reqwest::Client::new(),+ &server.uri(),+ &server.uri(),+ vec![],+ &folder,+ &file,+ std::sync::Arc::new(|_, _| {}),+ )+ .await+ .expect("an app's library takes a file in its own zone");std::fs::remove_file(&file).ok();+ assert_eq!(item.drivewsid, format!("FILE::{ZONE}::D1"));+ let reqs = server.received_requests().await.unwrap();+ assert!(+ reqs.iter()+ .all(|r| !r.url.path().contains("com.apple.CloudDocs")),+ "nothing asked of the documents zone"+ );+ let filed = reqs+ .iter()+ .find(|r| r.url.path().ends_with("/update/documents"))+ .expect("filed");+ let body: Value = serde_json::from_slice(&filed.body).unwrap();+ assert_eq!(body["path"]["starting_document_id"], "documents");}/// A busy Drive while the bytes go up: the file is tried again from a fresh slot, up todiff --git a/src/lib/ipc.ts b/src/lib/ipc.tsindex fae7f21..54964bf 100644--- a/src/lib/ipc.ts+++ b/src/lib/ipc.ts@@ -964,13 +964,13 @@ export const zoneOf = (drivewsid: string): string | null =>drivewsid.split("::")[1] || null;/**- * Whether files from this machine can be put in a folder.+ * Whether Drive's own items can be copied into, or out of, a folder.*- * An app library — Keynote's, Shortcuts', a third party's — belongs to the app that made it,- * and Apple files a document by a bare id with no zone beside it. A file meant for one would- * land somewhere else entirely, so it is not offered.+ * An app library (Keynote's, Shortcuts', Obsidian's vault) is a zone of its own. Files from+ * this machine go up into one, filed in its zone; Drive copies its items only beside the+ * original, so a copy across zones is not offered.*/-export const acceptsUploads = (folderId: string): boolean =>+export const takesDriveCopies = (folderId: string): boolean =>(zoneOf(folderId) ?? "com.apple.CloudDocs") === "com.apple.CloudDocs";export const driveName = (item: DriveItem) =>diff --git a/src/screens/DriveView.tsx b/src/screens/DriveView.tsxindex 4434843..b141799 100644--- a/src/screens/DriveView.tsx+++ b/src/screens/DriveView.tsx@@ -11,7 +11,7 @@ import { Portal } from "../components/Portal";import { pickOpen } from "../lib/pick";import { getCurrentWebview } from "@tauri-apps/api/webview";import {- acceptsUploads,+ takesDriveCopies,api,driveName,isContainer,@@ -446,10 +446,10 @@ function sameItems(a: DriveItem[], b: DriveItem[]): boolean {}/**- * Files and folders of iCloud Drive's own: an app's library (Keynote, Shortcuts), and what is in- * one, belongs to that app (see acceptsUploads), so it is not copied out of it.+ * Files and folders of iCloud Drive's own: what is in an app's library (Keynote, Shortcuts) is in+ * that library's zone (see takesDriveCopies), so it is not copied out of it.*/-const copyable = (item: DriveItem) => (item.kind === "FILE" || item.kind === "FOLDER") && acceptsUploads(item.drivewsid);+const copyable = (item: DriveItem) => (item.kind === "FILE" || item.kind === "FOLDER") && takesDriveCopies(item.drivewsid);/** What a paste onto a name already there does: both kept ("Report 2.pdf"), or the one there replaced. */type Clash = "keep" | "replace" | null;@@ -1113,15 +1113,15 @@ export function DriveView() {}};/**- * The folder whose row is under a point the window system gave (physical pixels), if it is- * one that takes files.+ * The folder whose row is under a point the window system gave (physical pixels): files+ * dropped there from this machine go into it, in an app's library as anywhere else.*/const folderAt = (x: number, y: number): string | null => {const ratio = window.devicePixelRatio || 1;const under = document.elementFromPoint(x / ratio, y / ratio) as HTMLElement | null;const id = under?.closest?.<HTMLElement>(".drive-rows [data-id]")?.dataset.id;const item = id ? itemsRef.current.find((i) => i.drivewsid === id) : undefined;- return item && item.kind === "FOLDER" && acceptsUploads(item.drivewsid) ? item.drivewsid : null;+ return item && item.kind === "FOLDER" ? item.drivewsid : null;};const itemsRef = useRef(items);itemsRef.current = items;@@ -1239,10 +1239,6 @@ export function DriveView() {*/// Each render's own, as the keys are: it uploads into the folder open now.const paste = async (move: boolean) => {- if (!acceptsUploads(hereRef.current)) {- setError("This folder belongs to an app, which is the only thing that can add to it.");- return;- }const board = await api.clipboardRead().catch(() => null);if (board?.files.length) {await upload(board.files);@@ -1250,6 +1246,12 @@ export function DriveView() {}const c = clipRef.current;if (!c) return;+ // Drive copies its items only beside the original: not into an app's library, a zone of+ // its own. Files from this machine go up into one (above).+ if (!takesDriveCopies(hereRef.current)) {+ setError("Items from elsewhere in iCloud Drive can't be pasted into an app's folder. Upload files from this computer instead.");+ return;+ }// Something else copied since (text in another app): that is what is on the clipboard.if (c.text !== null && board?.text != null && board.text !== c.text) return;const into = hereRef.current;@@ -1754,19 +1756,12 @@ export function DriveView() {New Folder</button></span>- {/* An app's own library takes files from the app, not from here. */}<span className="upload-menu-wrap"><buttonclassName="primary with-glyph"aria-haspopup="menu"aria-expanded={uploadMenu}onClick={() => setUploadMenu((open) => !open)}- disabled={!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