From ef74392af33182ec50957ccdf3eea5ac68a8467a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Mon, 7 Sep 2026 20:52:01 +0300 Subject: [PATCH] =?UTF-8?q?fix(web):=20the=20viewer's=20back=20link=20name?= =?UTF-8?q?s=20its=20origin=20=E2=80=94=20shared,=20not=20just=20the=20dri?= =?UTF-8?q?ve?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- crates/in-web/src/files.rs | 21 ++++++++- crates/in-web/src/i18n.rs | 23 +++++++--- crates/in-web/src/share.rs | 72 ++++++++++++++++++++++++------ crates/in-web/tests/http.rs | 88 ++++++++++++++++++++++++++++++------- 4 files changed, 167 insertions(+), 37 deletions(-) diff --git a/crates/in-web/src/files.rs b/crates/in-web/src/files.rs index 690d98c..9334c4a 100644 --- a/crates/in-web/src/files.rs +++ b/crates/in-web/src/files.rs @@ -173,6 +173,15 @@ struct DownloadQuery { dl: Option, } +/// The viewer's origin: `from` is `shared` when the shared page's row sent +/// the reader over, and anything else — absent included — is a drive +/// opening. The back link names the page you came from; an unknown value +/// keeps the standing drive answer, never a refusal. +#[query_params] +struct ViewQuery { + from: Option, +} + /// 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 @@ -474,7 +483,9 @@ fn is_archive_mime(mime: &str) -> bool { /// 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. +/// 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. #[page("/view/{id}")] async fn view_file(cx: &Cx) -> Result { let id: &str = path_param::(cx); @@ -491,6 +502,10 @@ async fn view_file(cx: &Cx) -> Result { } }; let language = lang(cx).await; + let from_shared = query_params::(cx) + .ok() + .and_then(|query| query.from.clone()) + .is_some_and(|from| from == "shared"); let store = app(cx).store.clone(); let Some(file) = visible_file(store.as_ref(), &user.id, id).await else { @@ -515,7 +530,9 @@ async fn view_file(cx: &Cx) -> Result { (topbar(cx, NavPage::Drive, &user, language).await?)
- (t(language, Key::BackToDrive)) + + (t(language, if from_shared { Key::BackToShared } else { Key::BackToDrive })) +
if may_download { (t(language, Key::Download)) diff --git a/crates/in-web/src/i18n.rs b/crates/in-web/src/i18n.rs index 2ff9bcc..dea16cd 100644 --- a/crates/in-web/src/i18n.rs +++ b/crates/in-web/src/i18n.rs @@ -83,6 +83,7 @@ pub enum Key { SomethingWentWrong, NothingAtThisAddress, BackToDrive, + BackToShared, // Landing. WelcomeTitle, WelcomeBlurb, @@ -255,6 +256,7 @@ pub fn t(lang: Lang, key: Key) -> &'static str { SomethingWentWrong => "Something went wrong.", NothingAtThisAddress => "There is nothing at this address.", BackToDrive => "Back to the drive", + BackToShared => "Back to shared", WelcomeTitle => "In: your files", WelcomeBlurb => "Your folders and files, shared on your terms.", SignIn => "Sign in", @@ -390,9 +392,13 @@ pub fn t(lang: Lang, key: Key) -> &'static str { ServerAddress => "Server address", EffectiveOrigin => "Effective origin", BaseUrlLabel => "Public base URL", - BaseUrlNote => "The origin share links carry: a saved value wins, an empty one falls back to the configured base_url, then the bind address.", + BaseUrlNote => { + "The origin share links carry: a saved value wins, an empty one falls back to the configured base_url, then the bind address." + } Shared => "Shared", - LegacyLinkNote => "This link predates re-viewable addresses — revoke and mint a new one to carry it forward.", + LegacyLinkNote => { + "This link predates re-viewable addresses — revoke and mint a new one to carry it forward." + } }, Lang::Tr => match key { Cancel => "Vazgeç", @@ -409,6 +415,7 @@ pub fn t(lang: Lang, key: Key) -> &'static str { SomethingWentWrong => "Bir şeyler ters gitti.", NothingAtThisAddress => "Bu adreste bir şey yok.", BackToDrive => "Sürücüye dön", + BackToShared => "Paylaşılanlara dön", WelcomeTitle => "In: dosyaların", WelcomeBlurb => "Klasörlerin ve dosyaların, senin kurallarınla.", SignIn => "Oturum aç", @@ -492,7 +499,9 @@ pub fn t(lang: Lang, key: Key) -> &'static str { EnableUser => "Etkinleştir", DisabledBadge => "devre dışı", LinkCreated => "Bağlantı oluşturuldu.", - CopyLinkOnce => "Şimdi kopyala. Tam adres, Paylaşım bağlantıları panelinde de görünür kalır.", + CopyLinkOnce => { + "Şimdi kopyala. Tam adres, Paylaşım bağlantıları panelinde de görünür kalır." + } ExpiresLabel => "Bitiş", NeverExpires => "Süresiz", ViewOnly => "Yalnızca görüntüleme: önizleme, indirme yok", @@ -544,9 +553,13 @@ pub fn t(lang: Lang, key: Key) -> &'static str { ServerAddress => "Sunucu adresi", EffectiveOrigin => "Etkin kök adres", BaseUrlLabel => "Herkese açık kök adres", - BaseUrlNote => "Paylaşım bağlantılarının taşıdığı kök adres: kaydedilen değer önceliklidir, boş bırakılırsa yapılandırılmış base_url'e, sonra bağlanılan adrese dönülür.", + BaseUrlNote => { + "Paylaşım bağlantılarının taşıdığı kök adres: kaydedilen değer önceliklidir, boş bırakılırsa yapılandırılmış base_url'e, sonra bağlanılan adrese dönülür." + } Shared => "Paylaşılmış", - LegacyLinkNote => "Bu bağlantı, adreslerin yeniden görüntülenebildiği dönemden önce oluşturuldu — devam ettirmek için iptal edip yeni bir tane oluşturun.", + LegacyLinkNote => { + "Bu bağlantı, adreslerin yeniden görüntülenebildiği dönemden önce oluşturuldu — devam ettirmek için iptal edip yeni bir tane oluşturun." + } }, } } diff --git a/crates/in-web/src/share.rs b/crates/in-web/src/share.rs index a72e347..572e1ce 100644 --- a/crates/in-web/src/share.rs +++ b/crates/in-web/src/share.rs @@ -223,10 +223,17 @@ struct CreateLinkForm { async fn create_link(cx: &Cx, Form(input): Form) -> Redirect { let user = match require_user(cx).await { Ok(user) => user, - Err(refusal) => return redirect_back(cx, "/settings?section=links", "create", Some(refusal)), + Err(refusal) => { + return redirect_back(cx, "/settings?section=links", "create", Some(refusal)); + } }; let Some(kind) = parse_kind(&input.kind) else { - return redirect_back(cx, "/settings?section=links", "create", Some(Refusal::NotFound)); + return redirect_back( + cx, + "/settings?section=links", + "create", + Some(Refusal::NotFound), + ); }; if let Err(refusal) = owned_target(app(cx).store.as_ref(), &user, kind, &input.target_id).await { @@ -234,7 +241,9 @@ async fn create_link(cx: &Cx, Form(input): Form) -> Redirect { } let expires_at = match parse_expiry(input.expires_in_days.as_deref()) { Ok(expires_at) => expires_at, - Err(refusal) => return redirect_back(cx, "/settings?section=links", "create", Some(refusal)), + Err(refusal) => { + return redirect_back(cx, "/settings?section=links", "create", Some(refusal)); + } }; // An optional password on the link: a trimmed empty field is no // password at all. Argon2id is CPU-slow on purpose, so the hash runs @@ -244,7 +253,14 @@ async fn create_link(cx: &Cx, Form(input): Form) -> Redirect { Some(password) => { match tokio::task::spawn_blocking(move || hash_link_password(&password)).await { Ok(Ok(hash)) => Some(hash), - _ => return redirect_back(cx, "/settings?section=links", "create", Some(Refusal::Unavailable)), + _ => { + return redirect_back( + cx, + "/settings?section=links", + "create", + Some(Refusal::Unavailable), + ); + } } } None => None, @@ -280,7 +296,12 @@ async fn create_link(cx: &Cx, Form(input): Form) -> Redirect { let location = format!("{back}{separator}created={}", minted.token); Ok((StatusCode::SEE_OTHER, [(header::LOCATION, location)])) } - Err(error) => redirect_back(cx, "/settings?section=links", "create", Some(refusal_of(error))), + Err(error) => redirect_back( + cx, + "/settings?section=links", + "create", + Some(refusal_of(error)), + ), } } @@ -295,7 +316,9 @@ struct RevokeLinkForm { async fn revoke_link(cx: &Cx, Form(input): Form) -> Redirect { let user = match require_user(cx).await { Ok(user) => user, - Err(refusal) => return redirect_back(cx, "/settings?section=links", "revoke", Some(refusal)), + Err(refusal) => { + return redirect_back(cx, "/settings?section=links", "revoke", Some(refusal)); + } }; let store = app(cx).store; let mine = store @@ -303,13 +326,23 @@ async fn revoke_link(cx: &Cx, Form(input): Form) -> Redirect { .await .map_err(|_| Refusal::Unavailable); let Ok(links) = mine else { - return redirect_back(cx, "/settings?section=links", "revoke", Some(Refusal::Unavailable)); + return redirect_back( + cx, + "/settings?section=links", + "revoke", + Some(Refusal::Unavailable), + ); }; if !links .iter() .any(|link| link.id == input.id && link.created_by == user.id) { - return redirect_back(cx, "/settings?section=links", "revoke", Some(Refusal::NotFound)); + return redirect_back( + cx, + "/settings?section=links", + "revoke", + Some(Refusal::NotFound), + ); } match store.revoke_share_link(&input.id).await { Ok(()) => { @@ -322,12 +355,20 @@ async fn revoke_link(cx: &Cx, Form(input): Form) -> Redirect { .await { Ok(()) => redirect_back(cx, "/settings?section=links", "revoke", None), - Err(error) => { - redirect_back(cx, "/settings?section=links", "revoke", Some(refusal_of(error))) - } + Err(error) => redirect_back( + cx, + "/settings?section=links", + "revoke", + Some(refusal_of(error)), + ), } } - Err(error) => redirect_back(cx, "/settings?section=links", "revoke", Some(refusal_of(error))), + Err(error) => redirect_back( + cx, + "/settings?section=links", + "revoke", + Some(refusal_of(error)), + ), } } @@ -473,7 +514,10 @@ fn has_flag(query: &str, key: &str) -> bool { /// `/s` surface stays a `#[route]` and wraps its views itself — in /// `document_shell`, the same shell every `#[page]` under `/` wears, never /// a hand-copied head. -async fn public_page(cx: &Cx, page: Result) -> topcoat::Result { +async fn public_page( + cx: &Cx, + page: Result, +) -> topcoat::Result { Ok(document_shell(cx, page).await?.into_response(cx)?) } @@ -1317,7 +1361,7 @@ async fn shared(cx: &Cx) -> Result {
format!("/drive?folder={}", row.item.target_id), - ShareKind::File => format!("/view/{}", row.item.target_id), + ShareKind::File => format!("/view/{}?from=shared", row.item.target_id), })> if row.item.kind == ShareKind::Folder { diff --git a/crates/in-web/tests/http.rs b/crates/in-web/tests/http.rs index 98fd064..4ae68bc 100644 --- a/crates/in-web/tests/http.rs +++ b/crates/in-web/tests/http.rs @@ -1868,6 +1868,13 @@ async fn per_user_share_lands_in_shared_with_me() { let bobs = app.get("/shared", Some(&bob)).await; assert_eq!(bobs.status, StatusCode::OK, "{}", bobs.text()); assert!(bobs.text().contains("joint.txt"), "{}", bobs.text()); + // The row opens the viewer marked with its origin, so the back link + // can name the shared page. + assert!( + bobs.text().contains(&format!("/view/{file}?from=shared")), + "{}", + bobs.text() + ); let caras = app.get("/shared", Some(&cara)).await; assert_eq!(caras.status, StatusCode::OK, "{}", caras.text()); @@ -2018,7 +2025,11 @@ async fn created_link_is_revisible_and_copyable_everywhere() { .post( "/api/share/link/create", Some(&ada), - &[("kind", "file"), ("target_id", &file), ("can_download", "1")], + &[ + ("kind", "file"), + ("target_id", &file), + ("can_download", "1"), + ], ) .await; assert!(answer.accepted(), "create refused: {:?}", answer.location); @@ -2179,7 +2190,11 @@ async fn drive_marks_shared_rows() { &[("parent_id", ""), ("name", "Dossier")], ) .await; - assert!(answer.accepted(), "folder create refused: {:?}", answer.location); + assert!( + answer.accepted(), + "folder create refused: {:?}", + answer.location + ); let folder = folder_id(&app, &owner, None, "Dossier").await; // A live public link onto the file, another onto the folder. @@ -2264,6 +2279,24 @@ async fn view_only_reader_previews_media_but_cannot_download() { assert!(page.text().contains("viewer-media"), "{}", page.text()); assert!(!page.text().contains("No preview"), "{}", page.text()); assert!(!page.text().contains("?dl=1"), "{}", page.text()); + // The back link names the origin: opened from the shared page (the + // `from=shared` its rows carry) it returns there, a plain opening to + // the drive. + let from_shared = app + .get(&format!("/view/{file}?from=shared"), Some(&bob)) + .await; + assert_eq!(from_shared.status, StatusCode::OK, "{}", from_shared.text()); + assert!( + from_shared.text().contains("Back to shared"), + "{}", + from_shared.text() + ); + assert!( + !from_shared.text().contains("Back to the drive"), + "{}", + from_shared.text() + ); + assert!(page.text().contains("Back to the drive"), "{}", page.text()); // The inline bytes serve for the preview... let inline = app.get(&format!("/file/{file}"), Some(&bob)).await; @@ -2477,13 +2510,14 @@ async fn drive_search_results_merge_into_one_panel() { let file = file_id(&app, &owner, "holiday report.txt", b"sun").await; let page = app.get("/drive?q=holiday", Some(&cookie)).await; - assert_eq!(page.status, StatusCode::OK, "{}", page.text()); let body = page.text(); // Both hits render in one shared list, folders first, wired to their surfaces. assert!(body.contains("holiday photos"), "{body}"); assert!(body.contains("holiday report.txt"), "{body}"); assert!(body.contains(&format!("/drive?folder={folder}")), "{body}"); assert!(body.contains(&format!("/view/{file}")), "{body}"); + // Drive rows stay unmarked: only the shared page names an origin. + assert!(!body.contains("from=shared"), "{body}"); let folder_at = body.find("holiday photos").expect("no folder hit"); let file_at = body.find("holiday report.txt").expect("no file hit"); assert!(folder_at < file_at, "file hit precedes folder hit: {body}"); @@ -2728,7 +2762,10 @@ async fn share_links_carry_the_configured_base_url() { // The banner the mint redirects back to. let page = app - .get(&format!("/settings?section=links&created={token}"), Some(&admin)) + .get( + &format!("/settings?section=links&created={token}"), + Some(&admin), + ) .await; assert_eq!(page.status, StatusCode::OK, "{}", page.text()); assert!( @@ -2789,7 +2826,10 @@ async fn share_links_follow_the_bind_without_a_base_url() { // No `base_url`: the links say the address bound, as they always // have. let page = app - .get(&format!("/settings?section=links&created={token}"), Some(&admin)) + .get( + &format!("/settings?section=links&created={token}"), + Some(&admin), + ) .await; assert_eq!(page.status, StatusCode::OK, "{}", page.text()); assert!( @@ -2883,26 +2923,38 @@ async fn admin_settings_sections_render_alone() { .await .unwrap(); - let body = app.get("/settings?section=profile", Some(&admin)).await.text(); + let body = app + .get("/settings?section=profile", Some(&admin)) + .await + .text(); assert!(body.contains("name=\"ui\""), "{body}"); assert!(!body.contains("/api/share/link/revoke"), "{body}"); assert!(!body.contains("member-table"), "{body}"); assert!(!body.contains("/api/settings/base_url"), "{body}"); - let body = app.get("/settings?section=links", Some(&admin)).await.text(); + let body = app + .get("/settings?section=links", Some(&admin)) + .await + .text(); assert!(body.contains("action=\"/api/share/link/revoke\""), "{body}"); assert!(!body.contains("name=\"ui\""), "{body}"); assert!(!body.contains("member-table"), "{body}"); assert!(!body.contains("/api/settings/base_url"), "{body}"); - let body = app.get("/settings?section=everyone", Some(&admin)).await.text(); + let body = app + .get("/settings?section=everyone", Some(&admin)) + .await + .text(); assert!(body.contains("member-table"), "{body}"); assert!(body.contains("ada@in.test"), "{body}"); assert!(!body.contains("name=\"ui\""), "{body}"); assert!(!body.contains("/api/share/link/revoke"), "{body}"); assert!(!body.contains("/api/settings/base_url"), "{body}"); - let body = app.get("/settings?section=server", Some(&admin)).await.text(); + let body = app + .get("/settings?section=server", Some(&admin)) + .await + .text(); assert!(body.contains("action=\"/api/settings/base_url\""), "{body}"); // The effective origin shows before anything is stored: the bind. assert!(body.contains("http://127.0.0.1:7655"), "{body}"); @@ -2965,8 +3017,7 @@ async fn server_address_setting_drives_share_links() { ) .await; assert!( - page - .text() + page.text() .contains(&format!("https://files.example.com/s/{token}")), "{}", page.text() @@ -3038,8 +3089,7 @@ async fn cleared_setting_falls_back_to_the_configured_base_url() { ) .await; assert!( - page - .text() + page.text() .contains(&format!("https://override.example.com/s/{token}")), "{}", page.text() @@ -3071,7 +3121,11 @@ async fn revoke_redirects_land_on_the_links_section() { let app = TestApp::build().await; let admin = app.sign_in("sub-admin", "ada@in.test", "Ada").await; let answer = app - .post("/api/share/link/revoke", Some(&admin), &[("id", "never-real")]) + .post( + "/api/share/link/revoke", + Some(&admin), + &[("id", "never-real")], + ) .await; assert!( answer @@ -3096,7 +3150,10 @@ async fn the_settings_revoke_button_sits_inline_in_its_row() { .await .unwrap(); - let body = app.get("/settings?section=links", Some(&admin)).await.text(); + let body = app + .get("/settings?section=links", Some(&admin)) + .await + .text(); let row_at = body .find("
") .expect("no share-link row"); @@ -4699,4 +4756,3 @@ async fn an_empty_folder_says_how_to_fill_it() { assert!(body.contains("id=\"upload-form\""), "{body}"); assert!(body.contains("__inDrop"), "{body}"); } -