diff --git a/crates/in-web/src/files.rs b/crates/in-web/src/files.rs index 9334c4a..dcf53b3 100644 --- a/crates/in-web/src/files.rs +++ b/crates/in-web/src/files.rs @@ -183,8 +183,9 @@ struct ViewQuery { } /// The file row this account may open: its owner may, and anyone holding a -/// live grant onto it may. Trashed files open for nobody here — the trash -/// has its own screen. `None` folds "no such file" and "not shared" into +/// live grant onto it may. A trashed row opens for its owner only — the +/// trash's preview — and stays download-dead on the byte route until the +/// row is restored. `None` folds "no such file" and "not shared" into /// one answer. async fn visible_file( store: &dyn Store, @@ -193,7 +194,7 @@ async fn visible_file( ) -> Option { let file = store.file(file_id).await.ok()??; if file.deleted_at.is_some() { - return None; + return (file.owner_id == user_id).then_some(file); } if file.owner_id == user_id { return Some(file); @@ -211,8 +212,9 @@ async fn visible_file( /// Serves one file's bytes, or the same not-found a stranger would see for /// a file that does not exist — never a `403`, which would confirm the id /// belongs to somebody else's drive. Inline bytes are the viewer's preview: -/// anyone who may see the file reads them. Taking the file away is a -/// different act — `?dl=1` needs the download grant, and only it counts. +/// anyone who may see the file reads them, a trashed row's owner included. +/// Taking the file away is a different act — `?dl=1` needs the download +/// grant, and a trashed row never counts: restoring is its way back. /// /// The body streams off disk through [`bytes_response`]: a play starts on /// the first 64 KiB frame, not on the whole file landing in memory, and a @@ -239,12 +241,14 @@ async fn download(cx: &Cx) -> topcoat::Result<(StatusCode, HeaderMap, topcoat::r .ok() .and_then(|query| query.dl.clone()) .is_some(); - // A view-only grant previews but never downloads. - let may_download = file.owner_id == user.id - || store - .can_download(ShareKind::File, id, &user.id) - .await - .unwrap_or(false); + // A view-only grant previews but never downloads, and neither does a + // trashed row — its owner may look, not take. + let may_download = file.deleted_at.is_none() + && (file.owner_id == user.id + || store + .can_download(ShareKind::File, id, &user.id) + .await + .unwrap_or(false)); if forced && !may_download { return Ok(not_found()); } @@ -475,17 +479,19 @@ fn is_archive_mime(mime: &str) -> bool { } /// `GET /view/{id}`: one file's viewer page — its name, the inline viewer -/// for its kind, a download link and its details. Authorization mirrors -/// trashed files open for nobody. Anyone who may see the file gets the -/// media preview — "view only" means preview, no download: the media -/// elements carry no save affordance (no download in the controls, no -/// drag, no context menu — `viewer_guard_script`), the Download link shows -/// only while the reader may download, and `?dl=1` stays gated on the -/// byte route. Documents (PDF, text) frame the bytes in the browser's own -/// chrome, whose toolbar downloads — they preview only under the grant. -/// Bytes for another owner's file answer 404, not 403. The head's back -/// link names the origin: `?from=shared`, the pair the shared page's rows -/// carry, returns there; anything else back to the drive. +/// for its kind, a download link and its details. Anyone who may see the +/// file gets the media preview — "view only" means preview, no download: +/// the media elements carry no save affordance (no download in the +/// controls, no drag, no context menu — `viewer_guard_script`), the +/// Download link shows only while the reader may download, and `?dl=1` +/// stays gated on the byte route. A trashed row previews for its owner +/// alone, download-dead until restored. Documents (PDF, text) frame the +/// bytes in the browser's own chrome, whose toolbar downloads — they +/// preview only under the grant. Bytes for another owner's file answer +/// 404, not 403. The head's back link names the origin: `?from=shared`, +/// the pair the shared page's rows carry, returns there; a trashed row, +/// wherever it was opened from, back to the trash; anything else back to +/// the drive. #[page("/view/{id}")] async fn view_file(cx: &Cx) -> Result { let id: &str = path_param::(cx); @@ -511,11 +517,20 @@ async fn view_file(cx: &Cx) -> Result { let Some(file) = visible_file(store.as_ref(), &user.id, id).await else { return Err(page_not_found().into()); }; - let may_download = file.owner_id == user.id - || store - .can_download(ShareKind::File, &file.id, &user.id) - .await - .unwrap_or(false); + let may_download = file.deleted_at.is_none() + && (file.owner_id == user.id + || store + .can_download(ShareKind::File, &file.id, &user.id) + .await + .unwrap_or(false)); + // A trashed row's home is the trash, whatever pair the query wears. + let (back_href, back_key) = if file.deleted_at.is_some() { + ("/trash", Key::BackToTrash) + } else if from_shared { + ("/shared", Key::BackToShared) + } else { + ("/drive", Key::BackToDrive) + }; let src = format!("/file/{}", file.id); let download_href = format!("/file/{}?dl=1", file.id); @@ -530,9 +545,7 @@ async fn view_file(cx: &Cx) -> Result { (topbar(cx, NavPage::Drive, &user, language).await?)
- - (t(language, if from_shared { Key::BackToShared } else { Key::BackToDrive })) - + (t(language, back_key))
if may_download { (t(language, Key::Download)) diff --git a/crates/in-web/src/i18n.rs b/crates/in-web/src/i18n.rs index dea16cd..a395b4c 100644 --- a/crates/in-web/src/i18n.rs +++ b/crates/in-web/src/i18n.rs @@ -84,6 +84,7 @@ pub enum Key { NothingAtThisAddress, BackToDrive, BackToShared, + BackToTrash, // Landing. WelcomeTitle, WelcomeBlurb, @@ -257,6 +258,7 @@ pub fn t(lang: Lang, key: Key) -> &'static str { NothingAtThisAddress => "There is nothing at this address.", BackToDrive => "Back to the drive", BackToShared => "Back to shared", + BackToTrash => "Back to trash", WelcomeTitle => "In: your files", WelcomeBlurb => "Your folders and files, shared on your terms.", SignIn => "Sign in", @@ -416,6 +418,7 @@ pub fn t(lang: Lang, key: Key) -> &'static str { NothingAtThisAddress => "Bu adreste bir şey yok.", BackToDrive => "Sürücüye dön", BackToShared => "Paylaşılanlara dön", + BackToTrash => "Çöpe dön", WelcomeTitle => "In: dosyaların", WelcomeBlurb => "Klasörlerin ve dosyaların, senin kurallarınla.", SignIn => "Oturum aç", diff --git a/crates/in-web/src/trash.rs b/crates/in-web/src/trash.rs index 862028c..b7ac340 100644 --- a/crates/in-web/src/trash.rs +++ b/crates/in-web/src/trash.rs @@ -1,7 +1,9 @@ //! Trash: the soft-deleted shelf and its three mutations. //! -//! `GET /trash` lists the reader's trashed files and folders. Trashed bytes -//! stay on disk and keep counting toward the quota until the purge. Folder +//! `GET /trash` lists the reader's trashed files and folders; a file row +//! opens the preview — `/view/{id}` — which serves a trashed row to its +//! owner alone, download-dead until restored. Trashed bytes stay on disk +//! and keep counting toward the quota until the purge. Folder //! trash and restore cascade to descendants under one timestamp, and an item //! whose ancestor is still trashed cannot be restored. //! `POST /api/trash/restore|purge|empty` bring back, destroy one row (plus @@ -141,9 +143,9 @@ async fn empty(cx: &Cx) -> Redirect { } } -/// A trashed file's chip: always the mime-class glyph, never the thumbnail -/// image — `/thumb/{id}` 404s trashed rows, so a Ready row would render a -/// broken image. The clone only clears the thumbnail flag for the render. +/// A trashed file's chip: the mime-class glyph, never the thumbnail image — +/// the shelf wears its own mark, thumbnails belong to the live listings. +/// The clone only clears the thumbnail flag for the render. async fn trash_chip(cx: &Cx, file: &File) -> Result { let mut unthumb = file.clone(); unthumb.thumb_state = ThumbState::None; @@ -371,31 +373,29 @@ async fn trash(cx: &Cx) -> Result {
for row in &rows { - // Trashed entries open nothing — the row is a fact, - // not a way in; the ⋯ holds the way back and the way - // gone. The grid is `.drive-cols`, the same columns - // the drive's anchors wear. + // File rows open the preview — the row is a way in + // for a look; the ⋯ still holds the way back and + // the way gone. Folder rows stay facts. The grid is + // `.drive-cols`' columns, the same ones the drive's + // anchors wear.
-
- match row { - TrashEntry::Folder(folder) => { - - (folder.name.clone()) - - } - TrashEntry::File(file) => { - (trash_chip(cx, file).await?) - (file.name.clone()) - (crate::drive::human_size(file.size_bytes)) - } - } - (row.deleted().date().to_string()) - if let TrashEntry::File(file) = row { + if let TrashEntry::File(file) = row { + + (trash_chip(cx, file).await?) + (file.name.clone()) + (crate::drive::human_size(file.size_bytes)) + (row.deleted().date().to_string()) (file.download_count.to_string()) - } else { + + } else if let TrashEntry::Folder(folder) = row { +
+ + (folder.name.clone()) - } -
+ (row.deleted().date().to_string()) + +
+ }
"⋯"
diff --git a/crates/in-web/tests/http.rs b/crates/in-web/tests/http.rs index 4ae68bc..1d61ce7 100644 --- a/crates/in-web/tests/http.rs +++ b/crates/in-web/tests/http.rs @@ -1298,9 +1298,58 @@ async fn file_rename_move_and_delete() { trashed.deleted_at.is_some(), "delete did not trash the file" ); - // And the bytes answer 404 once trashed. - let gone = app.get(&format!("/file/{}", file.id), Some(&cookie)).await; - assert_eq!(gone.status, StatusCode::NOT_FOUND); + // Trashed bytes still serve the owner's preview inline — only the + // taking is dead until the row is restored. + let preview = app.get(&format!("/file/{}", file.id), Some(&cookie)).await; + assert_eq!(preview.status, StatusCode::OK); + let forced = app + .get(&format!("/file/{}?dl=1", file.id), Some(&cookie)) + .await; + assert_eq!(forced.status, StatusCode::NOT_FOUND); +} + +/// A trashed file previews for its owner: the trash's row opens the +/// viewer, the back link names the trash, the bytes serve inline while +/// `?dl=1` stays dead — and a stranger, grant or no grant, sees nothing. +#[tokio::test] +async fn trashed_file_previews_for_its_owner() { + let app = TestApp::build().await; + let admin = app.sign_in("sub-admin", "ada@in.test", "Ada").await; + let bob = app.sign_in("sub-bob", "bob@in.test", "Bob").await; + let owner = owner_of(&app, "sub-admin").await; + let file = file_id(&app, &owner, "gone.png", b"\x89PNG\r\n\x1a\n gone").await; + + let answer = app + .post("/api/file/delete", Some(&admin), &[("id", &file)]) + .await; + assert_eq!(answer.body, "null", "deleting was refused: {}", answer.body); + + // The trash's file row opens the preview; the folder rows beside it + // stay facts. + let shelf = app.get("/trash", Some(&admin)).await; + assert_eq!(shelf.status, StatusCode::OK, "{}", shelf.text()); + assert!( + shelf.text().contains(&format!("/view/{file}")), + "{}", + shelf.text() + ); + + // The viewer names the trash as the way back and shows the media, + // but offers no download for a trashed row. + let page = app.get(&format!("/view/{file}"), Some(&admin)).await; + assert_eq!(page.status, StatusCode::OK, "{}", page.text()); + assert!(page.text().contains("Back to trash"), "{}", page.text()); + assert!(page.text().contains("viewer-media"), "{}", page.text()); + assert!(!page.text().contains("?dl=1"), "{}", page.text()); + + // Inline bytes serve the owner's preview, taking stays dead, and a + // stranger sees the same nothing a missing id would show. + let inline = app.get(&format!("/file/{file}"), Some(&admin)).await; + assert_eq!(inline.status, StatusCode::OK, "{}", inline.text()); + let forced = app.get(&format!("/file/{file}?dl=1"), Some(&admin)).await; + assert_eq!(forced.status, StatusCode::NOT_FOUND); + let stranger = app.get(&format!("/view/{file}"), Some(&bob)).await; + assert_eq!(stranger.status, StatusCode::NOT_FOUND); } #[tokio::test]