forge
Explore
Log in

Don't you dare go Hollow

A trashed file previews for its owner — look, never take

Browse files at this commit

25da666d10550d90dde4031823f3b3074c85e3f8Ahmet Kaan GümüşParent: ef74392
  • feat
    • a trashed file previews for its owner — look, never take
Show full message
feat(trash): a trashed file previews for its owner — look, never take
crates/in-web/src/files.rsmodified+43 -30
@@ -183,8 +183,9 @@ struct ViewQuery {
183 }183 }
184 184
185 /// The file row this account may open: its owner may, and anyone holding a185 /// The file row this account may open: its owner may, and anyone holding a
186-/// live grant onto it may. Trashed files open for nobody here — the trash186+/// live grant onto it may. A trashed row opens for its owner only — the
187-/// has its own screen. `None` folds "no such file" and "not shared" into187+/// trash's preview — and stays download-dead on the byte route until the
188+/// row is restored. `None` folds "no such file" and "not shared" into
188 /// one answer.189 /// one answer.
189 async fn visible_file(190 async fn visible_file(
190 store: &dyn Store,191 store: &dyn Store,
@@ -193,7 +194,7 @@ async fn visible_file(
193 ) -> Option<in_core::store::File> {194 ) -> Option<in_core::store::File> {
194 let file = store.file(file_id).await.ok()??;195 let file = store.file(file_id).await.ok()??;
195 if file.deleted_at.is_some() {196 if file.deleted_at.is_some() {
196- return None;197+ return (file.owner_id == user_id).then_some(file);
197 }198 }
198 if file.owner_id == user_id {199 if file.owner_id == user_id {
199 return Some(file);200 return Some(file);
@@ -211,8 +212,9 @@ async fn visible_file(
211 /// Serves one file's bytes, or the same not-found a stranger would see for212 /// Serves one file's bytes, or the same not-found a stranger would see for
212 /// a file that does not exist — never a `403`, which would confirm the id213 /// a file that does not exist — never a `403`, which would confirm the id
213 /// belongs to somebody else's drive. Inline bytes are the viewer's preview:214 /// belongs to somebody else's drive. Inline bytes are the viewer's preview:
214-/// anyone who may see the file reads them. Taking the file away is a215+/// anyone who may see the file reads them, a trashed row's owner included.
215-/// different act — `?dl=1` needs the download grant, and only it counts.216+/// Taking the file away is a different act — `?dl=1` needs the download
217+/// grant, and a trashed row never counts: restoring is its way back.
216 ///218 ///
217 /// The body streams off disk through [`bytes_response`]: a play starts on219 /// The body streams off disk through [`bytes_response`]: a play starts on
218 /// the first 64 KiB frame, not on the whole file landing in memory, and a220 /// 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
239 .ok()241 .ok()
240 .and_then(|query| query.dl.clone())242 .and_then(|query| query.dl.clone())
241 .is_some();243 .is_some();
242- // A view-only grant previews but never downloads.244+ // A view-only grant previews but never downloads, and neither does a
243- let may_download = file.owner_id == user.id245+ // trashed row — its owner may look, not take.
244- || store246+ let may_download = file.deleted_at.is_none()
245- .can_download(ShareKind::File, id, &user.id)247+ && (file.owner_id == user.id
246- .await248+ || store
247- .unwrap_or(false);249+ .can_download(ShareKind::File, id, &user.id)
250+ .await
251+ .unwrap_or(false));
248 if forced && !may_download {252 if forced && !may_download {
249 return Ok(not_found());253 return Ok(not_found());
250 }254 }
@@ -475,17 +479,19 @@ fn is_archive_mime(mime: &str) -> bool {
475 }479 }
476 480
477 /// `GET /view/{id}`: one file's viewer page — its name, the inline viewer481 /// `GET /view/{id}`: one file's viewer page — its name, the inline viewer
478-/// for its kind, a download link and its details. Authorization mirrors482+/// for its kind, a download link and its details. Anyone who may see the
479-/// trashed files open for nobody. Anyone who may see the file gets the483+/// file gets the media preview — "view only" means preview, no download:
480-/// media preview — "view only" means preview, no download: the media484+/// the media elements carry no save affordance (no download in the
481-/// elements carry no save affordance (no download in the controls, no485+/// controls, no drag, no context menu — `viewer_guard_script`), the
482-/// drag, no context menu — `viewer_guard_script`), the Download link shows486+/// Download link shows only while the reader may download, and `?dl=1`
483-/// only while the reader may download, and `?dl=1` stays gated on the487+/// stays gated on the byte route. A trashed row previews for its owner
484-/// byte route. Documents (PDF, text) frame the bytes in the browser's own488+/// alone, download-dead until restored. Documents (PDF, text) frame the
485-/// chrome, whose toolbar downloads — they preview only under the grant.489+/// bytes in the browser's own chrome, whose toolbar downloads — they
486-/// Bytes for another owner's file answer 404, not 403. The head's back490+/// preview only under the grant. Bytes for another owner's file answer
487-/// link names the origin: `?from=shared`, the pair the shared page's rows491+/// 404, not 403. The head's back link names the origin: `?from=shared`,
488-/// carry, returns there; anything else back to the drive.492+/// the pair the shared page's rows carry, returns there; a trashed row,
493+/// wherever it was opened from, back to the trash; anything else back to
494+/// the drive.
489 #[page("/view/{id}")]495 #[page("/view/{id}")]
490 async fn view_file(cx: &Cx) -> Result {496 async fn view_file(cx: &Cx) -> Result {
491 let id: &str = path_param::<Id>(cx);497 let id: &str = path_param::<Id>(cx);
@@ -511,11 +517,20 @@ async fn view_file(cx: &Cx) -> Result {
511 let Some(file) = visible_file(store.as_ref(), &user.id, id).await else {517 let Some(file) = visible_file(store.as_ref(), &user.id, id).await else {
512 return Err(page_not_found().into());518 return Err(page_not_found().into());
513 };519 };
514- let may_download = file.owner_id == user.id520+ let may_download = file.deleted_at.is_none()
515- || store521+ && (file.owner_id == user.id
516- .can_download(ShareKind::File, &file.id, &user.id)522+ || store
517- .await523+ .can_download(ShareKind::File, &file.id, &user.id)
518- .unwrap_or(false);524+ .await
525+ .unwrap_or(false));
526+ // A trashed row's home is the trash, whatever pair the query wears.
527+ let (back_href, back_key) = if file.deleted_at.is_some() {
528+ ("/trash", Key::BackToTrash)
529+ } else if from_shared {
530+ ("/shared", Key::BackToShared)
531+ } else {
532+ ("/drive", Key::BackToDrive)
533+ };
519 534
520 let src = format!("/file/{}", file.id);535 let src = format!("/file/{}", file.id);
521 let download_href = format!("/file/{}?dl=1", file.id);536 let download_href = format!("/file/{}?dl=1", file.id);
@@ -530,9 +545,7 @@ async fn view_file(cx: &Cx) -> Result {
530 (topbar(cx, NavPage::Drive, &user, language).await?)545 (topbar(cx, NavPage::Drive, &user, language).await?)
531 <main class="settings-stage stage-wide">546 <main class="settings-stage stage-wide">
532 <div class="viewer-head">547 <div class="viewer-head">
533- <a class="quiet" href=(if from_shared { "/shared" } else { "/drive" })>548+ <a class="quiet" href=(back_href)>(t(language, back_key))</a>
534- (t(language, if from_shared { Key::BackToShared } else { Key::BackToDrive }))
535- </a>
536 <div class="spacer"></div>549 <div class="spacer"></div>
537 if may_download {550 if may_download {
538 <a class="primary" href=(download_href) download="">(t(language, Key::Download))</a>551 <a class="primary" href=(download_href) download="">(t(language, Key::Download))</a>
crates/in-web/src/i18n.rsmodified+3 -0
@@ -84,6 +84,7 @@ pub enum Key {
84 NothingAtThisAddress,84 NothingAtThisAddress,
85 BackToDrive,85 BackToDrive,
86 BackToShared,86 BackToShared,
87+ BackToTrash,
87 // Landing.88 // Landing.
88 WelcomeTitle,89 WelcomeTitle,
89 WelcomeBlurb,90 WelcomeBlurb,
@@ -257,6 +258,7 @@ pub fn t(lang: Lang, key: Key) -> &'static str {
257 NothingAtThisAddress => "There is nothing at this address.",258 NothingAtThisAddress => "There is nothing at this address.",
258 BackToDrive => "Back to the drive",259 BackToDrive => "Back to the drive",
259 BackToShared => "Back to shared",260 BackToShared => "Back to shared",
261+ BackToTrash => "Back to trash",
260 WelcomeTitle => "In: your files",262 WelcomeTitle => "In: your files",
261 WelcomeBlurb => "Your folders and files, shared on your terms.",263 WelcomeBlurb => "Your folders and files, shared on your terms.",
262 SignIn => "Sign in",264 SignIn => "Sign in",
@@ -416,6 +418,7 @@ pub fn t(lang: Lang, key: Key) -> &'static str {
416 NothingAtThisAddress => "Bu adreste bir şey yok.",418 NothingAtThisAddress => "Bu adreste bir şey yok.",
417 BackToDrive => "Sürücüye dön",419 BackToDrive => "Sürücüye dön",
418 BackToShared => "Paylaşılanlara dön",420 BackToShared => "Paylaşılanlara dön",
421+ BackToTrash => "Çöpe dön",
419 WelcomeTitle => "In: dosyaların",422 WelcomeTitle => "In: dosyaların",
420 WelcomeBlurb => "Klasörlerin ve dosyaların, senin kurallarınla.",423 WelcomeBlurb => "Klasörlerin ve dosyaların, senin kurallarınla.",
421 SignIn => "Oturum aç",424 SignIn => "Oturum aç",
crates/in-web/src/trash.rsmodified+27 -27
@@ -1,7 +1,9 @@
1 //! Trash: the soft-deleted shelf and its three mutations.1 //! Trash: the soft-deleted shelf and its three mutations.
2 //!2 //!
3-//! `GET /trash` lists the reader's trashed files and folders. Trashed bytes3+//! `GET /trash` lists the reader's trashed files and folders; a file row
4-//! stay on disk and keep counting toward the quota until the purge. Folder4+//! opens the preview — `/view/{id}` — which serves a trashed row to its
5+//! owner alone, download-dead until restored. Trashed bytes stay on disk
6+//! and keep counting toward the quota until the purge. Folder
5 //! trash and restore cascade to descendants under one timestamp, and an item7 //! trash and restore cascade to descendants under one timestamp, and an item
6 //! whose ancestor is still trashed cannot be restored.8 //! whose ancestor is still trashed cannot be restored.
7 //! `POST /api/trash/restore|purge|empty` bring back, destroy one row (plus9 //! `POST /api/trash/restore|purge|empty` bring back, destroy one row (plus
@@ -141,9 +143,9 @@ async fn empty(cx: &Cx) -> Redirect {
141 }143 }
142 }144 }
143 145
144-/// A trashed file's chip: always the mime-class glyph, never the thumbnail146+/// A trashed file's chip: the mime-class glyph, never the thumbnail image —
145-/// image — `/thumb/{id}` 404s trashed rows, so a Ready row would render a147+/// the shelf wears its own mark, thumbnails belong to the live listings.
146-/// broken image. The clone only clears the thumbnail flag for the render.148+/// The clone only clears the thumbnail flag for the render.
147 async fn trash_chip(cx: &Cx, file: &File) -> Result {149 async fn trash_chip(cx: &Cx, file: &File) -> Result {
148 let mut unthumb = file.clone();150 let mut unthumb = file.clone();
149 unthumb.thumb_state = ThumbState::None;151 unthumb.thumb_state = ThumbState::None;
@@ -371,31 +373,29 @@ async fn trash(cx: &Cx) -> Result {
371 </div>373 </div>
372 <div class="drive-list">374 <div class="drive-list">
373 for row in &rows {375 for row in &rows {
374- // Trashed entries open nothing — the row is a fact,376+ // File rows open the preview — the row is a way in
375- // not a way in; the ⋯ holds the way back and the way377+ // for a look; the ⋯ still holds the way back and
376- // gone. The grid is `.drive-cols`, the same columns378+ // the way gone. Folder rows stay facts. The grid is
377- // the drive's anchors wear.379+ // `.drive-cols`' columns, the same ones the drive's
380+ // anchors wear.
378 <div class="drive-row">381 <div class="drive-row">
379- <div class="drive-cols">382+ if let TrashEntry::File(file) = row {
380- match row {383+ <a class="drive-open" href=(format!("/view/{}", file.id))>
381- TrashEntry::Folder(folder) => {384+ (trash_chip(cx, file).await?)
382- <span class="file-chip file-chip-folder" aria-hidden="true">"▤"</span>385+ <span class="dep-title">(file.name.clone())</span>
383- <span class="dep-title">(folder.name.clone())</span>386+ <span class="drive-meta drive-size">(crate::drive::human_size(file.size_bytes))</span>
384- <span class="drive-meta" aria-hidden="true"></span>387+ <span class="drive-meta drive-date">(row.deleted().date().to_string())</span>
385- }
386- TrashEntry::File(file) => {
387- (trash_chip(cx, file).await?)
388- <span class="dep-title">(file.name.clone())</span>
389- <span class="drive-meta drive-size">(crate::drive::human_size(file.size_bytes))</span>
390- }
391- }
392- <span class="drive-meta drive-date">(row.deleted().date().to_string())</span>
393- if let TrashEntry::File(file) = row {
394 <span class="drive-meta drive-dl">(file.download_count.to_string())</span>388 <span class="drive-meta drive-dl">(file.download_count.to_string())</span>
395- } else {389+ </a>
390+ } else if let TrashEntry::Folder(folder) = row {
391+ <div class="drive-cols">
392+ <span class="file-chip file-chip-folder" aria-hidden="true">"▤"</span>
393+ <span class="dep-title">(folder.name.clone())</span>
396 <span class="drive-meta" aria-hidden="true"></span>394 <span class="drive-meta" aria-hidden="true"></span>
397- }395+ <span class="drive-meta drive-date">(row.deleted().date().to_string())</span>
398- </div>396+ <span class="drive-meta" aria-hidden="true"></span>
397+ </div>
398+ }
399 <details class="user-menu entry-options">399 <details class="user-menu entry-options">
400 <summary class="quiet entry-options-trigger" aria-label=(t(language, Key::Options))>"⋯"</summary>400 <summary class="quiet entry-options-trigger" aria-label=(t(language, Key::Options))>"⋯"</summary>
401 <div class="user-menu-panel">401 <div class="user-menu-panel">
crates/in-web/tests/http.rsmodified+52 -3
@@ -1298,9 +1298,58 @@ async fn file_rename_move_and_delete() {
1298 trashed.deleted_at.is_some(),1298 trashed.deleted_at.is_some(),
1299 "delete did not trash the file"1299 "delete did not trash the file"
1300 );1300 );
1301- // And the bytes answer 404 once trashed.1301+ // Trashed bytes still serve the owner's preview inline — only the
1302- let gone = app.get(&format!("/file/{}", file.id), Some(&cookie)).await;1302+ // taking is dead until the row is restored.
1303- assert_eq!(gone.status, StatusCode::NOT_FOUND);1303+ let preview = app.get(&format!("/file/{}", file.id), Some(&cookie)).await;
1304+ assert_eq!(preview.status, StatusCode::OK);
1305+ let forced = app
1306+ .get(&format!("/file/{}?dl=1", file.id), Some(&cookie))
1307+ .await;
1308+ assert_eq!(forced.status, StatusCode::NOT_FOUND);
1309+}
1310+
1311+/// A trashed file previews for its owner: the trash's row opens the
1312+/// viewer, the back link names the trash, the bytes serve inline while
1313+/// `?dl=1` stays dead — and a stranger, grant or no grant, sees nothing.
1314+#[tokio::test]
1315+async fn trashed_file_previews_for_its_owner() {
1316+ let app = TestApp::build().await;
1317+ let admin = app.sign_in("sub-admin", "ada@in.test", "Ada").await;
1318+ let bob = app.sign_in("sub-bob", "bob@in.test", "Bob").await;
1319+ let owner = owner_of(&app, "sub-admin").await;
1320+ let file = file_id(&app, &owner, "gone.png", b"\x89PNG\r\n\x1a\n gone").await;
1321+
1322+ let answer = app
1323+ .post("/api/file/delete", Some(&admin), &[("id", &file)])
1324+ .await;
1325+ assert_eq!(answer.body, "null", "deleting was refused: {}", answer.body);
1326+
1327+ // The trash's file row opens the preview; the folder rows beside it
1328+ // stay facts.
1329+ let shelf = app.get("/trash", Some(&admin)).await;
1330+ assert_eq!(shelf.status, StatusCode::OK, "{}", shelf.text());
1331+ assert!(
1332+ shelf.text().contains(&format!("/view/{file}")),
1333+ "{}",
1334+ shelf.text()
1335+ );
1336+
1337+ // The viewer names the trash as the way back and shows the media,
1338+ // but offers no download for a trashed row.
1339+ let page = app.get(&format!("/view/{file}"), Some(&admin)).await;
1340+ assert_eq!(page.status, StatusCode::OK, "{}", page.text());
1341+ assert!(page.text().contains("Back to trash"), "{}", page.text());
1342+ assert!(page.text().contains("viewer-media"), "{}", page.text());
1343+ assert!(!page.text().contains("?dl=1"), "{}", page.text());
1344+
1345+ // Inline bytes serve the owner's preview, taking stays dead, and a
1346+ // stranger sees the same nothing a missing id would show.
1347+ let inline = app.get(&format!("/file/{file}"), Some(&admin)).await;
1348+ assert_eq!(inline.status, StatusCode::OK, "{}", inline.text());
1349+ let forced = app.get(&format!("/file/{file}?dl=1"), Some(&admin)).await;
1350+ assert_eq!(forced.status, StatusCode::NOT_FOUND);
1351+ let stranger = app.get(&format!("/view/{file}"), Some(&bob)).await;
1352+ assert_eq!(stranger.status, StatusCode::NOT_FOUND);
1304 }1353 }
1305 1354
1306 #[tokio::test]1355 #[tokio::test]