),
+ or the id as given."""
+ v = str(v or "").strip()
+ if "/" not in v and "?" not in v:
+ return v
+ if "/file/d/" in v or "/document/d/" in v or "/spreadsheets/d/" in v:
+ raise ValueError(f"{k}: that link is a file's; open the folder it's in and paste that link")
+ m = DRIVE_FOLDER.search(v)
+ if not m:
+ raise ValueError(f"{k}: paste the folder's link from Drive, or its id")
+ return m[1] or m[2]
+
+
+def tidy(typ, params):
+ """Options as people paste them, as rclone wants them: a Drive folder's or shared drive's link becomes its id."""
+ if typ != "drive" or not params:
+ return params
+ return {k: _drive_folder(k, v) if k in ("root_folder_id", "team_drive") and v else v for k, v in params.items()}
+
+
def _check_params(typ, params, secret_values):
spec = BACKENDS.get(typ)
if not spec:
@@ -352,6 +376,7 @@ def _secret(typ, k, v):
def create(db, cfg, name, typ, params=None, secret_values=None, user=None):
spec = _check_params(typ, params, secret_values)
+ params = tidy(typ, params)
plain = {k: _secret(typ, k, v) for k, v in (secret_values or {}).items() if isinstance(v, str) and v}
sid = db.next_id("storage_source")
sealed = {k: settings.seal(cfg, v, f"source:{sid}:{k}") for k, v in plain.items()}
@@ -375,6 +400,7 @@ def create(db, cfg, name, typ, params=None, secret_values=None, user=None):
def update(db, cfg, sid, name=None, params=None, secret_values=None):
src = get(db, sid)
_check_params(src["type"], params, secret_values)
+ params = tidy(src["type"], params)
sealed = dict(src.get("sealed") or {})
for k, v in (secret_values or {}).items():
if v is None or v == "":
diff --git a/fastapi_backend/tests/api/test_sources.py b/fastapi_backend/tests/api/test_sources.py
index 5c8115e73..7746e8c25 100644
--- a/fastapi_backend/tests/api/test_sources.py
+++ b/fastapi_backend/tests/api/test_sources.py
@@ -320,3 +320,30 @@ def test_google_drive_as_a_service_account(client, db, cfg, monkeypatch):
assert r.status_code == 400 and "service account" in r.text
r = client.patch(f"/api/v1/sources/{sid}", json={"secrets": {"service_account_credentials": "{}"}}, headers=h)
assert r.status_code == 400
+
+
+def test_a_drive_folder_link_is_kept_as_its_id(client, db, monkeypatch):
+ """The root folder and shared drive take the link Drive shows in the address bar, as well as the bare id."""
+ monkeypatch.setattr(sources, "test", lambda *a, **k: {"ok": True})
+ make_user(db, "root@x.io", "root password 1", admin=True)
+ h = login(client, "root@x.io", "root password 1")
+ links = {
+ "https://drive.google.com/drive/folders/1AbC-d_E?usp=sharing": "1AbC-d_E",
+ "https://drive.google.com/drive/u/1/folders/1XyZ": "1XyZ",
+ "https://drive.google.com/open?id=1Open&authuser=0": "1Open",
+ " 1Bare ": "1Bare",
+ }
+ for link, fid in links.items():
+ body = {
+ "name": "Drive",
+ "type": "drive",
+ "params": {"root_folder_id": link, "team_drive": "https://drive.google.com/drive/folders/0AShared"},
+ }
+ sid = client.post("/api/v1/sources", json=body, headers=h).json()["id"]
+ params = sources.get(db, sid)["params"]
+ assert params["root_folder_id"] == fid and params["team_drive"] == "0AShared", link
+ client.patch(f"/api/v1/sources/{sid}", json={"params": {"root_folder_id": "https://drive.google.com/drive/folders/1Moved"}}, headers=h)
+ assert sources.get(db, sid)["params"]["root_folder_id"] == "1Moved"
+ for bad, why in (("https://drive.google.com/file/d/1File/view", "a file's"), ("https://example.com/somewhere", "folder's link")):
+ r = client.post("/api/v1/sources", json={"type": "drive", "params": {"root_folder_id": bad}}, headers=h)
+ assert r.status_code == 400 and why in r.json()["detail"], r.text
diff --git a/nextjs-frontend/components/sources/connection-fields.tsx b/nextjs-frontend/components/sources/connection-fields.tsx
index 9dccb0e76..268962837 100644
--- a/nextjs-frontend/components/sources/connection-fields.tsx
+++ b/nextjs-frontend/components/sources/connection-fields.tsx
@@ -231,15 +231,17 @@ export function ConnectionFields({
{select("scope", DRIVE_SCOPES)}
{text("root_folder_id", {
mono: true,
- label: "Root folder ID",
+ label: "Root folder",
optional: true,
- hint: "From the folder’s URL; limits the connection to that folder",
+ placeholder: "Folder link or ID",
+ hint: "Paste the folder’s link from Drive; the connection only sees that folder",
})}
{form.driveAuth === "service_account" && (
{text("team_drive", {
mono: true,
optional: true,
+ placeholder: "Shared drive link or ID",
hint: "Read a shared drive the account is a member of",
})}
{text("impersonate", {
diff --git a/nextjs-frontend/components/sources/source-model.ts b/nextjs-frontend/components/sources/source-model.ts
index 686806b06..5bd58d2a0 100644
--- a/nextjs-frontend/components/sources/source-model.ts
+++ b/nextjs-frontend/components/sources/source-model.ts
@@ -60,8 +60,8 @@ export const FIELD_LABEL: Record = {
secret_access_key: "Secret access key",
token: "Token",
scope: "Scope",
- root_folder_id: "Root folder ID",
- team_drive: "Shared drive ID",
+ root_folder_id: "Root folder",
+ team_drive: "Shared drive",
impersonate: "Act as user",
service_account_credentials: "Service account key",
drive_id: "Drive ID",