A trashed file previews for its owner — look, never take
Browse files at this commit25da666d10550d90dde4031823f3b3074c85e3f8Parent: ef74392- feat
- a trashed file previews for its owner — look, never take
4 changed files +125 -60
- crates/in-web/src/files.rs +43 -30
- crates/in-web/src/i18n.rs +3 -0
- crates/in-web/src/trash.rs +27 -27
- crates/in-web/tests/http.rs +52 -3
crates/in-web/src/files.rsmodified+43 -30
| @@ -183,8 +183,9 @@ struct ViewQuery { | ||
| 183 | 183 | } |
| 184 | 184 | |
| 185 | 185 | /// 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 trash | |
| 187 | -/// has its own screen. `None` folds "no such file" and "not shared" into | |
| 186 | +/// live grant onto it may. A trashed row opens for its owner only — the | |
| 187 | +/// 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 | 189 | /// one answer. |
| 189 | 190 | async fn visible_file( |
| 190 | 191 | store: &dyn Store, |
| @@ -193,7 +194,7 @@ async fn visible_file( | ||
| 193 | 194 | ) -> Option<in_core::store::File> { |
| 194 | 195 | let file = store.file(file_id).await.ok()??; |
| 195 | 196 | if file.deleted_at.is_some() { |
| 196 | - return None; | |
| 197 | + return (file.owner_id == user_id).then_some(file); | |
| 197 | 198 | } |
| 198 | 199 | if file.owner_id == user_id { |
| 199 | 200 | return Some(file); |
| @@ -211,8 +212,9 @@ async fn visible_file( | ||
| 211 | 212 | /// Serves one file's bytes, or the same not-found a stranger would see for |
| 212 | 213 | /// a file that does not exist — never a `403`, which would confirm the id |
| 213 | 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 a | |
| 215 | -/// different act — `?dl=1` needs the download grant, and only it counts. | |
| 215 | +/// anyone who may see the file reads them, a trashed row's owner included. | |
| 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 | 219 | /// The body streams off disk through [`bytes_response`]: a play starts on |
| 218 | 220 | /// 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 | 241 | .ok() |
| 240 | 242 | .and_then(|query| query.dl.clone()) |
| 241 | 243 | .is_some(); |
| 242 | - // A view-only grant previews but never downloads. | |
| 243 | - let may_download = file.owner_id == user.id | |
| 244 | - || store | |
| 245 | - .can_download(ShareKind::File, id, &user.id) | |
| 246 | - .await | |
| 247 | - .unwrap_or(false); | |
| 244 | + // A view-only grant previews but never downloads, and neither does a | |
| 245 | + // trashed row — its owner may look, not take. | |
| 246 | + let may_download = file.deleted_at.is_none() | |
| 247 | + && (file.owner_id == user.id | |
| 248 | + || store | |
| 249 | + .can_download(ShareKind::File, id, &user.id) | |
| 250 | + .await | |
| 251 | + .unwrap_or(false)); | |
| 248 | 252 | if forced && !may_download { |
| 249 | 253 | return Ok(not_found()); |
| 250 | 254 | } |
| @@ -475,17 +479,19 @@ fn is_archive_mime(mime: &str) -> bool { | ||
| 475 | 479 | } |
| 476 | 480 | |
| 477 | 481 | /// `GET /view/{id}`: one file's viewer page — its name, the inline viewer |
| 478 | -/// for its kind, a download link and its details. Authorization mirrors | |
| 479 | -/// trashed files open for nobody. Anyone who may see the file gets the | |
| 480 | -/// media preview — "view only" means preview, no download: the media | |
| 481 | -/// elements carry no save affordance (no download in the controls, no | |
| 482 | -/// drag, no context menu — `viewer_guard_script`), the Download link shows | |
| 483 | -/// only while the reader may download, and `?dl=1` stays gated on the | |
| 484 | -/// byte route. Documents (PDF, text) frame the bytes in the browser's own | |
| 485 | -/// chrome, whose toolbar downloads — they preview only under the grant. | |
| 486 | -/// Bytes for another owner's file answer 404, not 403. The head's back | |
| 487 | -/// link names the origin: `?from=shared`, the pair the shared page's rows | |
| 488 | -/// carry, returns there; anything else back to the drive. | |
| 482 | +/// for its kind, a download link and its details. Anyone who may see the | |
| 483 | +/// file gets the media preview — "view only" means preview, no download: | |
| 484 | +/// the media elements carry no save affordance (no download in the | |
| 485 | +/// controls, no drag, no context menu — `viewer_guard_script`), the | |
| 486 | +/// Download link shows only while the reader may download, and `?dl=1` | |
| 487 | +/// stays gated on the byte route. A trashed row previews for its owner | |
| 488 | +/// alone, download-dead until restored. Documents (PDF, text) frame the | |
| 489 | +/// bytes in the browser's own chrome, whose toolbar downloads — they | |
| 490 | +/// preview only under the grant. Bytes for another owner's file answer | |
| 491 | +/// 404, not 403. The head's back link names the origin: `?from=shared`, | |
| 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 | 495 | #[page("/view/{id}")] |
| 490 | 496 | async fn view_file(cx: &Cx) -> Result { |
| 491 | 497 | let id: &str = path_param::<Id>(cx); |
| @@ -511,11 +517,20 @@ async fn view_file(cx: &Cx) -> Result { | ||
| 511 | 517 | let Some(file) = visible_file(store.as_ref(), &user.id, id).await else { |
| 512 | 518 | return Err(page_not_found().into()); |
| 513 | 519 | }; |
| 514 | - let may_download = file.owner_id == user.id | |
| 515 | - || store | |
| 516 | - .can_download(ShareKind::File, &file.id, &user.id) | |
| 517 | - .await | |
| 518 | - .unwrap_or(false); | |
| 520 | + let may_download = file.deleted_at.is_none() | |
| 521 | + && (file.owner_id == user.id | |
| 522 | + || store | |
| 523 | + .can_download(ShareKind::File, &file.id, &user.id) | |
| 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 | 535 | let src = format!("/file/{}", file.id); |
| 521 | 536 | let download_href = format!("/file/{}?dl=1", file.id); |
| @@ -530,9 +545,7 @@ async fn view_file(cx: &Cx) -> Result { | ||
| 530 | 545 | (topbar(cx, NavPage::Drive, &user, language).await?) |
| 531 | 546 | <main class="settings-stage stage-wide"> |
| 532 | 547 | <div class="viewer-head"> |
| 533 | - <a class="quiet" href=(if from_shared { "/shared" } else { "/drive" })> | |
| 534 | - (t(language, if from_shared { Key::BackToShared } else { Key::BackToDrive })) | |
| 535 | - </a> | |
| 548 | + <a class="quiet" href=(back_href)>(t(language, back_key))</a> | |
| 536 | 549 | <div class="spacer"></div> |
| 537 | 550 | if may_download { |
| 538 | 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 | 84 | NothingAtThisAddress, |
| 85 | 85 | BackToDrive, |
| 86 | 86 | BackToShared, |
| 87 | + BackToTrash, | |
| 87 | 88 | // Landing. |
| 88 | 89 | WelcomeTitle, |
| 89 | 90 | WelcomeBlurb, |
| @@ -257,6 +258,7 @@ pub fn t(lang: Lang, key: Key) -> &'static str { | ||
| 257 | 258 | NothingAtThisAddress => "There is nothing at this address.", |
| 258 | 259 | BackToDrive => "Back to the drive", |
| 259 | 260 | BackToShared => "Back to shared", |
| 261 | + BackToTrash => "Back to trash", | |
| 260 | 262 | WelcomeTitle => "In: your files", |
| 261 | 263 | WelcomeBlurb => "Your folders and files, shared on your terms.", |
| 262 | 264 | SignIn => "Sign in", |
| @@ -416,6 +418,7 @@ pub fn t(lang: Lang, key: Key) -> &'static str { | ||
| 416 | 418 | NothingAtThisAddress => "Bu adreste bir şey yok.", |
| 417 | 419 | BackToDrive => "Sürücüye dön", |
| 418 | 420 | BackToShared => "Paylaşılanlara dön", |
| 421 | + BackToTrash => "Çöpe dön", | |
| 419 | 422 | WelcomeTitle => "In: dosyaların", |
| 420 | 423 | WelcomeBlurb => "Klasörlerin ve dosyaların, senin kurallarınla.", |
| 421 | 424 | SignIn => "Oturum aç", |
crates/in-web/src/trash.rsmodified+27 -27
| @@ -1,7 +1,9 @@ | ||
| 1 | 1 | //! Trash: the soft-deleted shelf and its three mutations. |
| 2 | 2 | //! |
| 3 | -//! `GET /trash` lists the reader's trashed files and folders. Trashed bytes | |
| 4 | -//! stay on disk and keep counting toward the quota until the purge. Folder | |
| 3 | +//! `GET /trash` lists the reader's trashed files and folders; a file row | |
| 4 | +//! 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 | 7 | //! trash and restore cascade to descendants under one timestamp, and an item |
| 6 | 8 | //! whose ancestor is still trashed cannot be restored. |
| 7 | 9 | //! `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 thumbnail | |
| 145 | -/// image — `/thumb/{id}` 404s trashed rows, so a Ready row would render a | |
| 146 | -/// broken image. The clone only clears the thumbnail flag for the render. | |
| 146 | +/// A trashed file's chip: the mime-class glyph, never the thumbnail image — | |
| 147 | +/// the shelf wears its own mark, thumbnails belong to the live listings. | |
| 148 | +/// The clone only clears the thumbnail flag for the render. | |
| 147 | 149 | async fn trash_chip(cx: &Cx, file: &File) -> Result { |
| 148 | 150 | let mut unthumb = file.clone(); |
| 149 | 151 | unthumb.thumb_state = ThumbState::None; |
| @@ -371,31 +373,29 @@ async fn trash(cx: &Cx) -> Result { | ||
| 371 | 373 | </div> |
| 372 | 374 | <div class="drive-list"> |
| 373 | 375 | for row in &rows { |
| 374 | - // Trashed entries open nothing — the row is a fact, | |
| 375 | - // not a way in; the ⋯ holds the way back and the way | |
| 376 | - // gone. The grid is `.drive-cols`, the same columns | |
| 377 | - // the drive's anchors wear. | |
| 376 | + // File rows open the preview — the row is a way in | |
| 377 | + // for a look; the ⋯ still holds the way back and | |
| 378 | + // the way gone. Folder rows stay facts. The grid is | |
| 379 | + // `.drive-cols`' columns, the same ones the drive's | |
| 380 | + // anchors wear. | |
| 378 | 381 | <div class="drive-row"> |
| 379 | - <div class="drive-cols"> | |
| 380 | - match row { | |
| 381 | - TrashEntry::Folder(folder) => { | |
| 382 | - <span class="file-chip file-chip-folder" aria-hidden="true">"▤"</span> | |
| 383 | - <span class="dep-title">(folder.name.clone())</span> | |
| 384 | - <span class="drive-meta" aria-hidden="true"></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 { | |
| 382 | + if let TrashEntry::File(file) = row { | |
| 383 | + <a class="drive-open" href=(format!("/view/{}", file.id))> | |
| 384 | + (trash_chip(cx, file).await?) | |
| 385 | + <span class="dep-title">(file.name.clone())</span> | |
| 386 | + <span class="drive-meta drive-size">(crate::drive::human_size(file.size_bytes))</span> | |
| 387 | + <span class="drive-meta drive-date">(row.deleted().date().to_string())</span> | |
| 394 | 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 | 394 | <span class="drive-meta" aria-hidden="true"></span> |
| 397 | - } | |
| 398 | - </div> | |
| 395 | + <span class="drive-meta drive-date">(row.deleted().date().to_string())</span> | |
| 396 | + <span class="drive-meta" aria-hidden="true"></span> | |
| 397 | + </div> | |
| 398 | + } | |
| 399 | 399 | <details class="user-menu entry-options"> |
| 400 | 400 | <summary class="quiet entry-options-trigger" aria-label=(t(language, Key::Options))>"⋯"</summary> |
| 401 | 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 | 1298 | trashed.deleted_at.is_some(), |
| 1299 | 1299 | "delete did not trash the file" |
| 1300 | 1300 | ); |
| 1301 | - // And the bytes answer 404 once trashed. | |
| 1302 | - let gone = app.get(&format!("/file/{}", file.id), Some(&cookie)).await; | |
| 1303 | - assert_eq!(gone.status, StatusCode::NOT_FOUND); | |
| 1301 | + // Trashed bytes still serve the owner's preview inline — only the | |
| 1302 | + // taking is dead until the row is restored. | |
| 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 | 1355 | #[tokio::test] |