shares.rs
⎇
Raw
1//! Share management (milestone 6).
2//!
3//! Session-authenticated (management; a share token is never enough — see
4//! [`SessionUser`]):
5//! - `GET /api/shares` — list the current user's shares
6//! - `POST /api/shares` — create a share
7//! - `DELETE /api/shares/{id}` — delete one of the current user's shares
8//!
9//! Public (no login; resolved by token):
10//! - `GET /api/share/{token}` — resolve a share for the share page
11
12use std::sync::Arc;
13
14use api_types::{CreateShare, Mode, OkResp, ShareInfo};
15use axum::Json;
16use axum::extract::{Path as AxumPath, State};
17use axum::http::StatusCode;
18
19use crate::api::common::{SessionUser, display_name, target_rel};
20use crate::auth;
21use crate::db::ShareRow;
22use crate::error::{ApiError, AppState};
23use crate::fs;
24
25/// Shared JSON shape for a share (list / create / public resolve).
26fn share_info(row: &ShareRow, state: &AppState) -> ShareInfo {
27 ShareInfo {
28 id: row.id,
29 token: row.token.clone(),
30 name: display_name(state, &row.target),
31 is_file: row.is_file,
32 writable: row.mode.is_writable(),
33 target: row.target.clone(),
34 created_at: row.created_at.clone(),
35 expires_at: row.expires_at.clone(),
36 // The synthetic root id to use in file API calls.
37 root_id: row.id,
38 kind: None,
39 }
40}
41
42/// GET /api/shares — list the current user's shares.
43pub async fn list(
44 State(state): State<Arc<AppState>>,
45 auth: SessionUser,
46) -> Result<Json<Vec<ShareInfo>>, ApiError> {
47 let rows = state.db.user_shares(auth.user.id).await?;
48 Ok(Json(rows.iter().map(|r| share_info(r, &state)).collect()))
49}
50
51/// POST /api/shares — create a share.
52pub async fn create(
53 State(state): State<Arc<AppState>>,
54 auth: SessionUser,
55 Json(body): Json<CreateShare>,
56) -> Result<Json<ShareInfo>, ApiError> {
57 if body.writable && !state.db.allow_writable_shares().await? {
58 return Err(ApiError::localized(
59 StatusCode::FORBIDDEN,
60 "writable shares are disabled",
61 "err_rw_shares_disabled",
62 ));
63 }
64
65 // Validated at the trust boundary: `is_expired` treats an unparseable
66 // value as "never expires", so garbage here would make a permanent share.
67 if let Some(e) = &body.expires_at
68 && chrono::DateTime::parse_from_rfc3339(e).is_err()
69 {
70 return Err(ApiError::localized(
71 StatusCode::BAD_REQUEST,
72 "expires_at must be an RFC 3339 timestamp",
73 "err_bad_expires_at",
74 ));
75 }
76
77 let root = auth
78 .roots
79 .iter()
80 .find(|r| r.id == body.root_id)
81 .ok_or_else(|| {
82 ApiError::localized(
83 StatusCode::FORBIDDEN,
84 "no such folder",
85 "err_no_such_folder",
86 )
87 })?;
88
89 // A share must never grant more than the source root does, otherwise a
90 // read-only root could be escalated to a writable share of itself.
91 if body.writable && !root.mode.is_writable() {
92 return Err(ApiError::localized(
93 StatusCode::FORBIDDEN,
94 "this folder is read-only for you, so it cannot be shared writably",
95 "err_rw_ro_folder",
96 ));
97 }
98
99 // Resolve the target to a safe absolute path, then re-express it relative
100 // to the server root (the stored `target`).
101 let server_root = state.root.clone();
102 let root_path = root.path.clone();
103 let req = body.path.trim().to_string();
104 let req = if req.is_empty() { ".".to_string() } else { req };
105 let abs = tokio::task::spawn_blocking(move || fs::resolve_path(&server_root, &root_path, &req))
106 .await
107 .map_err(|_| {
108 ApiError::localized(
109 StatusCode::INTERNAL_SERVER_ERROR,
110 "internal error",
111 "err_internal",
112 )
113 })??;
114
115 let target = target_rel(&state, &abs);
116 let is_file = abs.is_file();
117
118 let token = auth::share_token();
119 let mode = if body.writable { Mode::Rw } else { Mode::Ro };
120 let row = state
121 .db
122 .create_share(
123 auth.user.id,
124 &token,
125 &target,
126 is_file,
127 mode,
128 body.expires_at.as_deref(),
129 )
130 .await?;
131
132 Ok(Json(share_info(&row, &state)))
133}
134
135/// DELETE /api/shares/{id} — delete one of the current user's shares.
136pub async fn delete(
137 State(state): State<Arc<AppState>>,
138 auth: SessionUser,
139 AxumPath(id): AxumPath<i64>,
140) -> Result<Json<OkResp>, ApiError> {
141 if !state.db.delete_share(id, auth.user.id).await? {
142 return Err(ApiError::localized(
143 StatusCode::NOT_FOUND,
144 "share not found",
145 "err_share_not_found",
146 ));
147 }
148 Ok(Json(OkResp { ok: true }))
149}
150
151/// GET /api/share/{token} — public resolve for the share page.
152pub async fn resolve(
153 State(state): State<Arc<AppState>>,
154 AxumPath(token): AxumPath<String>,
155) -> Result<Json<ShareInfo>, ApiError> {
156 let Some(row) = state.db.share_by_token(&token).await? else {
157 return Err(ApiError::localized(
158 StatusCode::NOT_FOUND,
159 "share not found",
160 "err_share_not_found",
161 ));
162 };
163 if row.is_expired() {
164 return Err(ApiError::localized(
165 StatusCode::GONE,
166 "this share has expired",
167 "err_share_expired",
168 ));
169 }
170 let mut info = share_info(&row, &state);
171 // A file share opens straight into the viewer, so the client needs the
172 // file's kind up front (it cannot list a file's "contents").
173 if row.is_file {
174 let (server_root, target) = (state.root.clone(), row.target.clone());
175 info.kind = tokio::task::spawn_blocking(move || {
176 fs::resolve_file(&server_root, &target)
177 .ok()
178 .map(|p| fs::detect_kind(&p, false))
179 })
180 .await
181 .ok()
182 .flatten();
183 }
184 Ok(Json(info))
185}
186