services gain a storage limit the family carries: im's admin panel already owns the roster, so the byte cap a storing sibling answers to belongs on the same row — a nullable storage_limit_bytes behind the guarded ALTER (an old-shaped table grows the column on open), edited as an amount plus a MiB/GiB pair on the per-row form the panel already had, and served to every registered app over /family as limit_bytes beside key, name and url (null when unstated, the sibling's own default standing in its place). A register refresh never touches the cap, the way it never touches the name; owner and client_id stay unserialized; and the family shape test now pins the four public keys with null and stated cases instead of the three it grew out of.
Browse files at this commit355890cd828d1b29e312544d888e422dae4e41eeParent: 509d0e76 changed files +463 -30
- crates/im-core/src/services.rs +128 -23
- crates/im-core/src/store.rs +71 -1
- crates/im-web/src/admin.rs +208 -5
- crates/im-web/src/directory.rs +52 -1
- crates/im-web/src/i18n.rs +3 -0
- crates/im-web/src/main.rs +1 -0
crates/im-core/src/services.rsmodified+128 -23
| @@ -26,7 +26,7 @@ pub struct Service { | ||
| 26 | 26 | /// The app that keeps this row — `SELF_OWNER` for im's own entry, a |
| 27 | 27 | /// client id for a sibling that registered itself, `None` for a row the |
| 28 | 28 | /// seed or the panel made. Never serialized: `/family`'s shape is the |
| 29 | - /// three public fields the siblings mirror, and nothing more. | |
| 29 | + /// public fields below, and the ownership stays im's business. | |
| 30 | 30 | #[serde(skip)] |
| 31 | 31 | pub owner: Option<String>, |
| 32 | 32 | /// The relying-party credential this row answers for — the client id |
| @@ -37,6 +37,13 @@ pub struct Service { | ||
| 37 | 37 | /// carries `None` until its app registers. |
| 38 | 38 | #[serde(skip)] |
| 39 | 39 | pub client_id: Option<String>, |
| 40 | + /// The storage cap the panel holds this service to, in bytes — im's | |
| 41 | + /// field alone, never the keeping app's. Served to the family as | |
| 42 | + /// `limit_bytes` (JSON `null` when unstated, the sibling's own default | |
| 43 | + /// standing in its place); a register's refresh never touches it, the | |
| 44 | + /// way it never touches the name. | |
| 45 | + #[serde(rename = "limit_bytes")] | |
| 46 | + pub storage_limit_bytes: Option<u64>, | |
| 40 | 47 | } |
| 41 | 48 | |
| 42 | 49 | /// The owner im writes on its own row, registered from `issuer` on every |
| @@ -92,7 +99,8 @@ pub async fn list(store: &Store) -> Result<Vec<Service>> { | ||
| 92 | 99 | let conn = store.conn.lock().await; |
| 93 | 100 | let mut rows = conn |
| 94 | 101 | .query( |
| 95 | - "SELECT key, name, url, owner, client_id FROM services ORDER BY position, key", | |
| 102 | + "SELECT key, name, url, owner, client_id, storage_limit_bytes \ | |
| 103 | + FROM services ORDER BY position, key", | |
| 96 | 104 | (), |
| 97 | 105 | ) |
| 98 | 106 | .await |
| @@ -105,6 +113,7 @@ pub async fn list(store: &Store) -> Result<Vec<Service>> { | ||
| 105 | 113 | url: store::text(&row, 2)?, |
| 106 | 114 | owner: store::opt_text(&row, 3)?, |
| 107 | 115 | client_id: store::opt_text(&row, 4)?, |
| 116 | + storage_limit_bytes: store::opt_int(&row, 5)?.map(|bytes| bytes.max(0) as u64), | |
| 108 | 117 | }); |
| 109 | 118 | } |
| 110 | 119 | Ok(services) |
| @@ -132,22 +141,34 @@ pub async fn add(store: &Store, service: &Service) -> Result<()> { | ||
| 132 | 141 | ))); |
| 133 | 142 | } |
| 134 | 143 | conn.execute( |
| 135 | - "INSERT INTO services (key, name, url, position) \ | |
| 136 | - VALUES (?1, ?2, ?3, (SELECT COALESCE(MAX(position), -1) + 1 FROM services))", | |
| 137 | - turso::params![service.key.as_str(), service.name.trim(), url.as_str()], | |
| 144 | + "INSERT INTO services (key, name, url, position, storage_limit_bytes) \ | |
| 145 | + VALUES (?1, ?2, ?3, (SELECT COALESCE(MAX(position), -1) + 1 FROM services), ?4)", | |
| 146 | + turso::params![ | |
| 147 | + service.key.as_str(), | |
| 148 | + service.name.trim(), | |
| 149 | + url.as_str(), | |
| 150 | + service.storage_limit_bytes.map(|bytes| bytes as i64) | |
| 151 | + ], | |
| 138 | 152 | ) |
| 139 | 153 | .await |
| 140 | 154 | .map_err(backend)?; |
| 141 | 155 | Ok(()) |
| 142 | 156 | } |
| 143 | 157 | |
| 144 | -/// Rewrites a service's name and address. The key travels in the form's | |
| 145 | -/// hidden field and is not editable; a key not on the list is refused, so a | |
| 146 | -/// form posted against a since-removed row cannot resurrect it. | |
| 147 | -/// A row an app keeps only takes its new name here: the address is the | |
| 158 | +/// Rewrites a service's name, address, and storage limit. The key travels | |
| 159 | +/// in the form's hidden field and is not editable; a key not on the list is | |
| 160 | +/// refused, so a form posted against a since-removed row cannot resurrect | |
| 161 | +/// it. A row an app keeps only takes its new name here: the address is the | |
| 148 | 162 | /// app's own, rewritten on its every boot, so accepting one from the panel |
| 149 | -/// would show an edit the next boot silently undoes. | |
| 150 | -pub async fn edit(store: &Store, key: &str, name: &str, url: &str) -> Result<()> { | |
| 163 | +/// would show an edit the next boot silently undoes. The limit is im's on | |
| 164 | +/// every row — kept or not, an empty field clears it to no limit stated. | |
| 165 | +pub async fn edit( | |
| 166 | + store: &Store, | |
| 167 | + key: &str, | |
| 168 | + name: &str, | |
| 169 | + url: &str, | |
| 170 | + storage_limit_bytes: Option<u64>, | |
| 171 | +) -> Result<()> { | |
| 151 | 172 | validate(key, name, url).map_err(StoreError::Invalid)?; |
| 152 | 173 | let url = url.trim_end_matches('/'); |
| 153 | 174 | if let Some(row) = owned_row(store, key).await? |
| @@ -161,11 +182,16 @@ pub async fn edit(store: &Store, key: &str, name: &str, url: &str) -> Result<()> | ||
| 161 | 182 | let conn = store.conn.lock().await; |
| 162 | 183 | let updated = conn |
| 163 | 184 | .execute( |
| 164 | - "UPDATE services SET name = ?2, url = ?3 WHERE key = ?1", | |
| 165 | - turso::params![key, name.trim(), url], | |
| 185 | + "UPDATE services SET name = ?2, url = ?3, storage_limit_bytes = ?4 WHERE key = ?1", | |
| 186 | + turso::params![ | |
| 187 | + key, | |
| 188 | + name.trim(), | |
| 189 | + url, | |
| 190 | + storage_limit_bytes.map(|bytes| bytes as i64) | |
| 191 | + ], | |
| 166 | 192 | ) |
| 167 | - .await | |
| 168 | - .map_err(backend)?; | |
| 193 | + .await | |
| 194 | + .map_err(backend)?; | |
| 169 | 195 | if updated == 0 { |
| 170 | 196 | return Err(StoreError::Invalid(format!("no service keyed {key:?}"))); |
| 171 | 197 | } |
| @@ -376,6 +402,7 @@ mod tests { | ||
| 376 | 402 | url: url.to_string(), |
| 377 | 403 | owner: None, |
| 378 | 404 | client_id: None, |
| 405 | + storage_limit_bytes: None, | |
| 379 | 406 | } |
| 380 | 407 | } |
| 381 | 408 | |
| @@ -401,7 +428,7 @@ mod tests { | ||
| 401 | 428 | assert_eq!(list(&store).await.unwrap()[0].url, "http://127.0.0.1:7655"); |
| 402 | 429 | // The panel renamed the first entry; a re-boot with the same config |
| 403 | 430 | // must not bring the old name back. |
| 404 | - edit(&store, "in", "Renamed", "http://127.0.0.1:7655") | |
| 431 | + edit(&store, "in", "Renamed", "http://127.0.0.1:7655", None) | |
| 405 | 432 | .await |
| 406 | 433 | .unwrap(); |
| 407 | 434 | assert!(!seed_from(&store, &seed).await.unwrap()); |
| @@ -423,7 +450,7 @@ mod tests { | ||
| 423 | 450 | .unwrap_or_else(|| panic!("a taken key must be refused")); |
| 424 | 451 | // The refused add changed nothing. |
| 425 | 452 | assert_eq!(keys(&store).await, vec!["im".to_string(), "in".to_string()]); |
| 426 | - edit(&store, "in", "Dosyalar", "https://in.example") | |
| 453 | + edit(&store, "in", "Dosyalar", "https://in.example", None) | |
| 427 | 454 | .await |
| 428 | 455 | .unwrap(); |
| 429 | 456 | let services = list(&store).await.unwrap(); |
| @@ -432,7 +459,7 @@ mod tests { | ||
| 432 | 459 | // A form posted against a since-removed key cannot resurrect it. |
| 433 | 460 | remove(&store, "in").await.unwrap(); |
| 434 | 461 | assert!(matches!( |
| 435 | - edit(&store, "in", "Zombie", "https://in.example").await, | |
| 462 | + edit(&store, "in", "Zombie", "https://in.example", None).await, | |
| 436 | 463 | Err(StoreError::Invalid(_)) |
| 437 | 464 | )); |
| 438 | 465 | assert_eq!(keys(&store).await, vec!["im".to_string()]); |
| @@ -598,11 +625,11 @@ mod tests { | ||
| 598 | 625 | Err(StoreError::Invalid(_)) |
| 599 | 626 | )); |
| 600 | 627 | assert!(matches!( |
| 601 | - edit(&store, "in", "Dosyalar", "https://elsewhere.example").await, | |
| 628 | + edit(&store, "in", "Dosyalar", "https://elsewhere.example", None).await, | |
| 602 | 629 | Err(StoreError::Invalid(_)) |
| 603 | 630 | )); |
| 604 | 631 | // The name is still the admin's, and a name-only edit lands. |
| 605 | - edit(&store, "in", "Dosyalar", "https://in.example") | |
| 632 | + edit(&store, "in", "Dosyalar", "https://in.example", None) | |
| 606 | 633 | .await |
| 607 | 634 | .unwrap(); |
| 608 | 635 | let row = &list(&store).await.unwrap()[0]; |
| @@ -617,17 +644,95 @@ mod tests { | ||
| 617 | 644 | } |
| 618 | 645 | |
| 619 | 646 | #[tokio::test] |
| 620 | - async fn the_family_json_never_carries_the_owner() { | |
| 647 | + async fn the_family_json_carries_the_limit_and_never_the_owner() { | |
| 621 | 648 | let store = store().await; |
| 622 | 649 | register(&store, "im", "Account", "http://127.0.0.1:7650", SELF_OWNER, None) |
| 623 | 650 | .await |
| 624 | 651 | .unwrap(); |
| 625 | 652 | let json = serde_json::to_value(list(&store).await.unwrap()).unwrap(); |
| 626 | 653 | let row = &json.as_array().unwrap()[0]; |
| 654 | + let mut keys: Vec<String> = row.as_object().unwrap().keys().cloned().collect(); | |
| 655 | + keys.sort(); | |
| 627 | 656 | assert_eq!( |
| 628 | - row.as_object().unwrap().keys().collect::<Vec<_>>(), | |
| 629 | - vec!["key", "name", "url"], | |
| 657 | + keys, | |
| 658 | + vec!["key", "limit_bytes", "name", "url"], | |
| 630 | 659 | "the sibling mirrors deserialize this shape: {json}" |
| 631 | 660 | ); |
| 661 | + assert!( | |
| 662 | + row["limit_bytes"].is_null(), | |
| 663 | + "no stated limit reads null, not absence" | |
| 664 | + ); | |
| 665 | + // A stated limit rides as plain bytes; the owner never travels. | |
| 666 | + edit( | |
| 667 | + &store, | |
| 668 | + "im", | |
| 669 | + "Account", | |
| 670 | + "http://127.0.0.1:7650", | |
| 671 | + Some(512 * 1024 * 1024), | |
| 672 | + ) | |
| 673 | + .await | |
| 674 | + .unwrap(); | |
| 675 | + let json = serde_json::to_value(list(&store).await.unwrap()).unwrap(); | |
| 676 | + assert_eq!( | |
| 677 | + json[0]["limit_bytes"], | |
| 678 | + serde_json::json!(512 * 1024 * 1024) | |
| 679 | + ); | |
| 680 | + assert!(json[0].get("owner").is_none()); | |
| 681 | + assert!(json[0].get("client_id").is_none()); | |
| 682 | + } | |
| 683 | + | |
| 684 | + #[tokio::test] | |
| 685 | + async fn the_storage_limit_round_trips_and_survives_a_register() { | |
| 686 | + let store = store().await; | |
| 687 | + add( | |
| 688 | + &store, | |
| 689 | + &Service { | |
| 690 | + storage_limit_bytes: Some(512 * 1024 * 1024), | |
| 691 | + ..service("in", "Files", "http://127.0.0.1:7655") | |
| 692 | + }, | |
| 693 | + ) | |
| 694 | + .await | |
| 695 | + .unwrap(); | |
| 696 | + assert_eq!( | |
| 697 | + list(&store).await.unwrap()[0].storage_limit_bytes, | |
| 698 | + Some(512 * 1024 * 1024), | |
| 699 | + "add carries the stated limit in" | |
| 700 | + ); | |
| 701 | + // The edit rewrites the limit; the row holds plain bytes. | |
| 702 | + edit( | |
| 703 | + &store, | |
| 704 | + "in", | |
| 705 | + "Files", | |
| 706 | + "http://127.0.0.1:7655", | |
| 707 | + Some(2 * 1024 * 1024 * 1024), | |
| 708 | + ) | |
| 709 | + .await | |
| 710 | + .unwrap(); | |
| 711 | + assert_eq!( | |
| 712 | + list(&store).await.unwrap()[0].storage_limit_bytes, | |
| 713 | + Some(2 * 1024 * 1024 * 1024) | |
| 714 | + ); | |
| 715 | + // A kept row: the app re-registers, the limit stays im's — the | |
| 716 | + // refresh rewrites the address and credential, never the cap. | |
| 717 | + register( | |
| 718 | + &store, | |
| 719 | + "in", | |
| 720 | + "Files", | |
| 721 | + "http://127.0.0.1:7655", | |
| 722 | + "in-client", | |
| 723 | + Some("in-client"), | |
| 724 | + ) | |
| 725 | + .await | |
| 726 | + .unwrap(); | |
| 727 | + assert_eq!( | |
| 728 | + list(&store).await.unwrap()[0].storage_limit_bytes, | |
| 729 | + Some(2 * 1024 * 1024 * 1024), | |
| 730 | + "the app's register never touches the limit" | |
| 731 | + ); | |
| 732 | + // The field empties to cleared: NULL is no limit stated. | |
| 733 | + edit(&store, "in", "Files", "http://127.0.0.1:7655", None) | |
| 734 | + .await | |
| 735 | + .unwrap(); | |
| 736 | + assert_eq!(list(&store).await.unwrap()[0].storage_limit_bytes, None); | |
| 632 | 737 | } |
| 633 | 738 | } |
crates/im-core/src/store.rsmodified+71 -1
| @@ -155,7 +155,8 @@ CREATE TABLE IF NOT EXISTS services ( | ||
| 155 | 155 | url TEXT NOT NULL, |
| 156 | 156 | position INTEGER NOT NULL, |
| 157 | 157 | owner TEXT, |
| 158 | - client_id TEXT | |
| 158 | + client_id TEXT, | |
| 159 | + storage_limit_bytes INTEGER | |
| 159 | 160 | ); |
| 160 | 161 | "; |
| 161 | 162 | |
| @@ -285,6 +286,18 @@ impl Store { | ||
| 285 | 286 | .await |
| 286 | 287 | .map_err(backend)?; |
| 287 | 288 | } |
| 289 | + // The per-service storage cap: the panel's limit on how many | |
| 290 | + // bytes a sibling that stores (in's Files) may hold. Served to | |
| 291 | + // the family over `/family` as `limit_bytes`; NULL states no | |
| 292 | + // limit, and the sibling's own default stands. | |
| 293 | + if !has_column(&conn, "services", "storage_limit_bytes").await? { | |
| 294 | + conn.execute( | |
| 295 | + "ALTER TABLE services ADD COLUMN storage_limit_bytes INTEGER", | |
| 296 | + (), | |
| 297 | + ) | |
| 298 | + .await | |
| 299 | + .map_err(backend)?; | |
| 300 | + } | |
| 288 | 301 | if !has_column(&conn, "sessions", "ip").await? { |
| 289 | 302 | conn.execute("ALTER TABLE sessions ADD COLUMN ip TEXT", ()) |
| 290 | 303 | .await |
| @@ -403,6 +416,9 @@ pub(crate) fn opt_text(row: &Row, idx: usize) -> Result<Option<String>> { | ||
| 403 | 416 | pub(crate) fn int(row: &Row, idx: usize) -> Result<i64> { |
| 404 | 417 | row.get::<i64>(idx).map_err(backend) |
| 405 | 418 | } |
| 419 | +pub(crate) fn opt_int(row: &Row, idx: usize) -> Result<Option<i64>> { | |
| 420 | + row.get::<Option<i64>>(idx).map_err(backend) | |
| 421 | +} | |
| 406 | 422 | |
| 407 | 423 | #[cfg(test)] |
| 408 | 424 | mod tests { |
| @@ -447,4 +463,58 @@ mod tests { | ||
| 447 | 463 | let _store = Store::open(&path).await.unwrap(); |
| 448 | 464 | assert!(dir.path().join("im.key").exists()); |
| 449 | 465 | } |
| 466 | + | |
| 467 | + #[tokio::test] | |
| 468 | + async fn an_old_shaped_services_table_grows_the_limit_column() { | |
| 469 | + let dir = tempfile::tempdir().unwrap(); | |
| 470 | + let path = dir.path().join("im.db"); | |
| 471 | + // The previous generation's services table, before the storage | |
| 472 | + // limit was a thing. `Store::open` creates the rest of the schema | |
| 473 | + // around it and ALTERs the column in without touching the row. | |
| 474 | + { | |
| 475 | + let db = turso::Builder::new_local(path.to_str().unwrap()) | |
| 476 | + .build() | |
| 477 | + .await | |
| 478 | + .unwrap(); | |
| 479 | + let conn = db.connect().unwrap(); | |
| 480 | + conn.execute_batch( | |
| 481 | + "CREATE TABLE services ( | |
| 482 | + key TEXT PRIMARY KEY, | |
| 483 | + name TEXT NOT NULL, | |
| 484 | + url TEXT NOT NULL, | |
| 485 | + position INTEGER NOT NULL, | |
| 486 | + owner TEXT | |
| 487 | + ); | |
| 488 | + INSERT INTO services (key, name, url, position, owner) | |
| 489 | + VALUES ('in', 'Files', 'http://127.0.0.1:7655', 0, NULL);", | |
| 490 | + ) | |
| 491 | + .await | |
| 492 | + .unwrap(); | |
| 493 | + drop(conn); | |
| 494 | + drop(db); | |
| 495 | + } | |
| 496 | + let store = Store::open(&path).await.unwrap(); | |
| 497 | + let rows = crate::services::list(&store).await.unwrap(); | |
| 498 | + assert_eq!(rows.len(), 1); | |
| 499 | + assert_eq!( | |
| 500 | + rows[0].storage_limit_bytes, None, | |
| 501 | + "a pre-limit row reads no limit" | |
| 502 | + ); | |
| 503 | + crate::services::edit( | |
| 504 | + &store, | |
| 505 | + "in", | |
| 506 | + "Files", | |
| 507 | + "http://127.0.0.1:7655", | |
| 508 | + Some(512 * 1024 * 1024), | |
| 509 | + ) | |
| 510 | + .await | |
| 511 | + .unwrap(); | |
| 512 | + // A re-boot on the already-migrated table is the guarded no-op, | |
| 513 | + // and the stored value survives it. | |
| 514 | + let store = Store::open(&path).await.unwrap(); | |
| 515 | + assert_eq!( | |
| 516 | + crate::services::list(&store).await.unwrap()[0].storage_limit_bytes, | |
| 517 | + Some(512 * 1024 * 1024) | |
| 518 | + ); | |
| 519 | + } | |
| 450 | 520 | } |
crates/im-web/src/admin.rsmodified+208 -5
| @@ -473,6 +473,9 @@ async fn services_section( | ||
| 473 | 473 | // A kept row posts the stored address back in a hidden field: the |
| 474 | 474 | // form's name change is accepted, and the store refuses only a |
| 475 | 475 | // *changed* address. |
| 476 | + // The storage limit: the panel's field on every row — a kept | |
| 477 | + // row's cap is im's to set, like its name. An empty amount posts | |
| 478 | + // no limit; the unit pairs with the amount it edits. | |
| 476 | 479 | let address_field = if kept { |
| 477 | 480 | format!( |
| 478 | 481 | r#"<input type="hidden" name="url" value="{url}">"#, |
| @@ -485,8 +488,19 @@ async fn services_section( | ||
| 485 | 488 | url = url, |
| 486 | 489 | ) |
| 487 | 490 | }; |
| 491 | + let (limit_amount, limit_unit) = service | |
| 492 | + .storage_limit_bytes | |
| 493 | + .map(bytes_as_unit) | |
| 494 | + .unwrap_or_default(); | |
| 495 | + let limit_field = format!( | |
| 496 | + r#"<label class="auth-field"><span class="auth-label">{limit_label}</span><input class="auth-input auth-input-mono" type="number" name="limit_amount" min="0" step="any" value="{limit_amount}"></label><select class="auth-input admin-role" name="limit_unit" aria-label="{limit_label}"><option value="MiB"{mib}>MiB</option><option value="GiB"{gib}>GiB</option></select>"#, | |
| 497 | + limit_label = t(lang, Key::ServicesStorageLimit), | |
| 498 | + limit_amount = limit_amount, | |
| 499 | + mib = if limit_unit == "MiB" { " selected" } else { "" }, | |
| 500 | + gib = if limit_unit == "GiB" { " selected" } else { "" }, | |
| 501 | + ); | |
| 488 | 502 | let edit = format!( |
| 489 | - r#"<details class="admin-confirm"><summary class="admin-action">{edit_word}</summary><div class="admin-confirm-pop"><div class="admin-confirm-title">{edit_title}</div><form method="post" action="/admin/services_edit" class="admin-form"><input type="hidden" name="key" value="{key}"><label class="auth-field"><span class="auth-label">{name_label}</span><input class="auth-input auth-input-mono" type="text" name="name" value="{name}" required></label>{address_field}<button class="admin-action" type="submit">{save}</button></form></div></details>"#, | |
| 503 | + r#"<details class="admin-confirm"><summary class="admin-action">{edit_word}</summary><div class="admin-confirm-pop"><div class="admin-confirm-title">{edit_title}</div><form method="post" action="/admin/services_edit" class="admin-form"><input type="hidden" name="key" value="{key}"><label class="auth-field"><span class="auth-label">{name_label}</span><input class="auth-input auth-input-mono" type="text" name="name" value="{name}" required></label>{address_field}{limit_field}<button class="admin-action" type="submit">{save}</button></form></div></details>"#, | |
| 490 | 504 | edit_word = t(lang, Key::EditWord), |
| 491 | 505 | edit_title = i18n::edit_service_title(lang, &name), |
| 492 | 506 | key = key, |
| @@ -656,13 +670,79 @@ fn client_controls(client_id: &str, name_html: &str, lang: i18n::Lang) -> String | ||
| 656 | 670 | format!("{rotate}{revoke_action}") |
| 657 | 671 | } |
| 658 | 672 | |
| 659 | -/// One user of the services forms: key, name, address. The add form fills | |
| 660 | -/// all three; the edit form carries the key in its hidden field. | |
| 673 | +/// One mebibyte / gibibyte in bytes: the only two units the limit field | |
| 674 | +/// speaks. Raw bytes never reach the browser — the form posts a decimal | |
| 675 | +/// amount plus one of these units, converted back here. | |
| 676 | +const MIB_BYTES: f64 = 1_048_576.0; | |
| 677 | +const GIB_BYTES: f64 = 1_073_741_824.0; | |
| 678 | + | |
| 679 | +/// The `(amount, unit)` pair a stored limit edits as: gibibytes once the | |
| 680 | +/// value reaches one, mebibytes below it, so `2 GiB` edits as `2` GiB | |
| 681 | +/// rather than `2048` MiB, while a small `512 MiB` cap stays addressable. | |
| 682 | +fn bytes_as_unit(bytes: u64) -> (String, &'static str) { | |
| 683 | + if bytes >= GIB_BYTES as u64 { | |
| 684 | + (trim_amount(bytes as f64 / GIB_BYTES), "GiB") | |
| 685 | + } else { | |
| 686 | + (trim_amount(bytes as f64 / MIB_BYTES), "MiB") | |
| 687 | + } | |
| 688 | +} | |
| 689 | + | |
| 690 | +/// A form amount for a number input: two decimals at most, trailing zeros | |
| 691 | +/// (and a bare point) trimmed, so the field reads `2` and `2.5`, never | |
| 692 | +/// `2.00` or `2.5000000001`. | |
| 693 | +fn trim_amount(value: f64) -> String { | |
| 694 | + let rounded = (value * 100.0).round() / 100.0; | |
| 695 | + let text = format!("{rounded:.2}"); | |
| 696 | + text.trim_end_matches('0').trim_end_matches('.').to_string() | |
| 697 | +} | |
| 698 | + | |
| 699 | +/// The bytes an `(amount, unit)` pair names, or `None` when the pair is | |
| 700 | +/// not a usable limit: an unknown unit, or an amount that is not a finite | |
| 701 | +/// non-negative number. | |
| 702 | +fn unit_bytes(amount: &str, unit: &str) -> Option<u64> { | |
| 703 | + let per = match unit { | |
| 704 | + "MiB" => MIB_BYTES, | |
| 705 | + "GiB" => GIB_BYTES, | |
| 706 | + _ => return None, | |
| 707 | + }; | |
| 708 | + let value: f64 = amount.trim().parse().ok()?; | |
| 709 | + if !value.is_finite() || value < 0.0 { | |
| 710 | + return None; | |
| 711 | + } | |
| 712 | + Some((value * per).round() as u64) | |
| 713 | +} | |
| 714 | + | |
| 715 | +/// The limit a services form posts, in bytes: `Ok(None)` when the amount | |
| 716 | +/// is blank — the field empties to clear the limit — `Ok(Some(bytes))` | |
| 717 | +/// when it parses, and `Err` when a non-blank amount is not a usable | |
| 718 | +/// number against its unit, which is the panel's refusal. | |
| 719 | +fn posted_limit( | |
| 720 | + amount: &Option<String>, | |
| 721 | + unit: &Option<String>, | |
| 722 | +) -> Result<Option<u64>, ()> { | |
| 723 | + let Some(amount) = amount | |
| 724 | + .as_deref() | |
| 725 | + .map(str::trim) | |
| 726 | + .filter(|amount| !amount.is_empty()) | |
| 727 | + else { | |
| 728 | + return Ok(None); | |
| 729 | + }; | |
| 730 | + match unit_bytes(amount, unit.as_deref().unwrap_or_default()) { | |
| 731 | + Some(bytes) => Ok(Some(bytes)), | |
| 732 | + None => Err(()), | |
| 733 | + } | |
| 734 | +} | |
| 735 | + | |
| 736 | +/// One user of the services forms: key, name, address — and, on the edit | |
| 737 | +/// form, the storage limit as an amount plus its unit. The add form posts | |
| 738 | +/// neither limit field; the empty amount clears. | |
| 661 | 739 | #[derive(Deserialize)] |
| 662 | 740 | struct ServiceForm { |
| 663 | 741 | key: String, |
| 664 | 742 | name: String, |
| 665 | 743 | url: String, |
| 744 | + limit_amount: Option<String>, | |
| 745 | + limit_unit: Option<String>, | |
| 666 | 746 | } |
| 667 | 747 | |
| 668 | 748 | #[route(POST "/admin/services_add")] |
| @@ -671,6 +751,10 @@ async fn services_add(cx: &Cx, Form(input): Form<ServiceForm>) -> Result<Respons | ||
| 671 | 751 | Ok(me) => me, |
| 672 | 752 | Err(redirect) => return Ok(redirect), |
| 673 | 753 | }; |
| 754 | + let limit = match posted_limit(&input.limit_amount, &input.limit_unit) { | |
| 755 | + Ok(limit) => limit, | |
| 756 | + Err(()) => return back(cx, "services", "&error=bad_service"), | |
| 757 | + }; | |
| 674 | 758 | let outcome = im_core::services::add( |
| 675 | 759 | &app(cx).store, |
| 676 | 760 | &im_core::services::Service { |
| @@ -679,6 +763,7 @@ async fn services_add(cx: &Cx, Form(input): Form<ServiceForm>) -> Result<Respons | ||
| 679 | 763 | url: input.url, |
| 680 | 764 | owner: None, |
| 681 | 765 | client_id: None, |
| 766 | + storage_limit_bytes: limit, | |
| 682 | 767 | }, |
| 683 | 768 | ) |
| 684 | 769 | .await; |
| @@ -691,8 +776,18 @@ async fn services_edit(cx: &Cx, Form(input): Form<ServiceForm>) -> Result<Respon | ||
| 691 | 776 | Ok(me) => me, |
| 692 | 777 | Err(redirect) => return Ok(redirect), |
| 693 | 778 | }; |
| 694 | - let outcome = | |
| 695 | - im_core::services::edit(&app(cx).store, &input.key, &input.name, &input.url).await; | |
| 779 | + let limit = match posted_limit(&input.limit_amount, &input.limit_unit) { | |
| 780 | + Ok(limit) => limit, | |
| 781 | + Err(()) => return back(cx, "services", "&error=bad_service"), | |
| 782 | + }; | |
| 783 | + let outcome = im_core::services::edit( | |
| 784 | + &app(cx).store, | |
| 785 | + &input.key, | |
| 786 | + &input.name, | |
| 787 | + &input.url, | |
| 788 | + limit, | |
| 789 | + ) | |
| 790 | + .await; | |
| 696 | 791 | service_outcome(cx, &me, outcome).await |
| 697 | 792 | } |
| 698 | 793 | |
| @@ -2243,6 +2338,7 @@ mod tests { | ||
| 2243 | 2338 | url: "https://wiki.dizey.sh".into(), |
| 2244 | 2339 | owner: None, |
| 2245 | 2340 | client_id: None, |
| 2341 | + storage_limit_bytes: None, | |
| 2246 | 2342 | }, |
| 2247 | 2343 | ) |
| 2248 | 2344 | .await |
| @@ -2285,4 +2381,111 @@ mod tests { | ||
| 2285 | 2381 | assert_eq!(status, StatusCode::OK); |
| 2286 | 2382 | assert_eq!(alias.contains(">Drive</td>"), body.contains(">Drive</td>")); |
| 2287 | 2383 | } |
| 2384 | + | |
| 2385 | + #[tokio::test] | |
| 2386 | + async fn services_edit_saves_the_limit_and_the_rest_is_refused() { | |
| 2387 | + let setup = setup().await; | |
| 2388 | + im_core::services::add( | |
| 2389 | + &setup.store, | |
| 2390 | + &im_core::services::Service { | |
| 2391 | + key: "in".into(), | |
| 2392 | + name: "Files".into(), | |
| 2393 | + url: "https://in.dizey.sh".into(), | |
| 2394 | + owner: None, | |
| 2395 | + client_id: None, | |
| 2396 | + storage_limit_bytes: None, | |
| 2397 | + }, | |
| 2398 | + ) | |
| 2399 | + .await | |
| 2400 | + .unwrap(); | |
| 2401 | + | |
| 2402 | + // The edit posts an amount plus its unit; the row stores bytes. | |
| 2403 | + let (status, location, _) = post_form( | |
| 2404 | + &setup.router, | |
| 2405 | + "/admin/services_edit", | |
| 2406 | + "key=in&name=Files&url=https://in.dizey.sh&limit_amount=2&limit_unit=GiB", | |
| 2407 | + Some(&setup.admin_cookie), | |
| 2408 | + ) | |
| 2409 | + .await; | |
| 2410 | + assert_eq!(status, StatusCode::SEE_OTHER); | |
| 2411 | + assert_eq!( | |
| 2412 | + location.as_deref(), | |
| 2413 | + Some("/admin?section=services&ok=services") | |
| 2414 | + ); | |
| 2415 | + assert_eq!( | |
| 2416 | + im_core::services::list(&setup.store).await.unwrap()[0].storage_limit_bytes, | |
| 2417 | + Some(2 * 1024 * 1024 * 1024) | |
| 2418 | + ); | |
| 2419 | + | |
| 2420 | + // An empty amount clears the limit — no limit stated. | |
| 2421 | + let (status, location, _) = post_form( | |
| 2422 | + &setup.router, | |
| 2423 | + "/admin/services_edit", | |
| 2424 | + "key=in&name=Files&url=https://in.dizey.sh&limit_amount=&limit_unit=GiB", | |
| 2425 | + Some(&setup.admin_cookie), | |
| 2426 | + ) | |
| 2427 | + .await; | |
| 2428 | + assert_eq!(status, StatusCode::SEE_OTHER); | |
| 2429 | + assert_eq!( | |
| 2430 | + location.as_deref(), | |
| 2431 | + Some("/admin?section=services&ok=services") | |
| 2432 | + ); | |
| 2433 | + assert_eq!( | |
| 2434 | + im_core::services::list(&setup.store).await.unwrap()[0].storage_limit_bytes, | |
| 2435 | + None | |
| 2436 | + ); | |
| 2437 | + | |
| 2438 | + // A non-blank amount that is not a usable number is the section's | |
| 2439 | + // refusal, and the stored limit is left alone. | |
| 2440 | + let (status, location, _) = post_form( | |
| 2441 | + &setup.router, | |
| 2442 | + "/admin/services_edit", | |
| 2443 | + "key=in&name=Files&url=https://in.dizey.sh&limit_amount=abc&limit_unit=GiB", | |
| 2444 | + Some(&setup.admin_cookie), | |
| 2445 | + ) | |
| 2446 | + .await; | |
| 2447 | + assert_eq!(status, StatusCode::SEE_OTHER); | |
| 2448 | + assert_eq!( | |
| 2449 | + location.as_deref(), | |
| 2450 | + Some("/admin?section=services&error=bad_service") | |
| 2451 | + ); | |
| 2452 | + assert_eq!( | |
| 2453 | + im_core::services::list(&setup.store).await.unwrap()[0].storage_limit_bytes, | |
| 2454 | + None | |
| 2455 | + ); | |
| 2456 | + | |
| 2457 | + // A non-admin gets the panel's plain redirect; the row never moves. | |
| 2458 | + let (status, location, _) = post_form( | |
| 2459 | + &setup.router, | |
| 2460 | + "/admin/services_edit", | |
| 2461 | + "key=in&name=Files&url=https://in.dizey.sh&limit_amount=2&limit_unit=GiB", | |
| 2462 | + Some(&setup.plain_cookie), | |
| 2463 | + ) | |
| 2464 | + .await; | |
| 2465 | + assert_eq!(status, StatusCode::SEE_OTHER); | |
| 2466 | + assert_eq!(location.as_deref(), Some("/")); | |
| 2467 | + assert_eq!( | |
| 2468 | + im_core::services::list(&setup.store).await.unwrap()[0].storage_limit_bytes, | |
| 2469 | + None | |
| 2470 | + ); | |
| 2471 | + | |
| 2472 | + // The section renders the amount and its unit pair, empty on a | |
| 2473 | + // row with no limit (bundle permitting). | |
| 2474 | + if setup.assets { | |
| 2475 | + let (status, _, body) = get_full( | |
| 2476 | + &setup.router, | |
| 2477 | + "/admin?section=services", | |
| 2478 | + Some(&setup.admin_cookie), | |
| 2479 | + ) | |
| 2480 | + .await; | |
| 2481 | + assert_eq!(status, StatusCode::OK); | |
| 2482 | + assert!(body.contains("name=\"limit_amount\""), "{body}"); | |
| 2483 | + assert!(body.contains("name=\"limit_unit\""), "{body}"); | |
| 2484 | + assert!(body.contains(">GiB</option>"), "{body}"); | |
| 2485 | + assert!( | |
| 2486 | + body.contains("name=\"limit_amount\" min=\"0\" step=\"any\" value=\"\""), | |
| 2487 | + "a no-limit row edits empty: {body}" | |
| 2488 | + ); | |
| 2489 | + } | |
| 2490 | + } | |
| 2288 | 2491 | } |
crates/im-web/src/directory.rsmodified+52 -1
| @@ -413,6 +413,7 @@ mod tests { | ||
| 413 | 413 | url: service.url.clone(), |
| 414 | 414 | owner: None, |
| 415 | 415 | client_id: None, |
| 416 | + storage_limit_bytes: None, | |
| 416 | 417 | }) |
| 417 | 418 | .collect() |
| 418 | 419 | } |
| @@ -675,6 +676,55 @@ mod tests { | ||
| 675 | 676 | assert_eq!(family, vec!["in", "im", "iz"], "{body}"); |
| 676 | 677 | } |
| 677 | 678 | |
| 679 | + #[tokio::test] | |
| 680 | + async fn family_carries_the_limit_bytes_null_and_stated() { | |
| 681 | + let Setup { | |
| 682 | + router, | |
| 683 | + client_id, | |
| 684 | + secret, | |
| 685 | + store, | |
| 686 | + .. | |
| 687 | + } = setup().await; | |
| 688 | + let pair = basic(&client_id, &secret); | |
| 689 | + // No limit stated anywhere yet: every row's `limit_bytes` is null, | |
| 690 | + // the shape an unconfigured sibling still parses. | |
| 691 | + let (status, body) = get_family(&router, Some(pair.clone())).await; | |
| 692 | + assert_eq!(status, StatusCode::OK); | |
| 693 | + let family = serde_json::from_str::<serde_json::Value>(&body).unwrap(); | |
| 694 | + for row in family.as_array().unwrap() { | |
| 695 | + assert!( | |
| 696 | + row.get("limit_bytes").unwrap().is_null(), | |
| 697 | + "no limit yet: {row}" | |
| 698 | + ); | |
| 699 | + } | |
| 700 | + | |
| 701 | + // The panel sets iz's cap; the next read carries plain bytes. | |
| 702 | + im_core::services::edit( | |
| 703 | + &store, | |
| 704 | + "iz", | |
| 705 | + "Board", | |
| 706 | + "http://127.0.0.1:7654", | |
| 707 | + Some(512 * 1024 * 1024), | |
| 708 | + ) | |
| 709 | + .await | |
| 710 | + .unwrap(); | |
| 711 | + let (status, body) = get_family(&router, Some(pair)).await; | |
| 712 | + assert_eq!(status, StatusCode::OK); | |
| 713 | + let family = serde_json::from_str::<serde_json::Value>(&body).unwrap(); | |
| 714 | + let iz = family | |
| 715 | + .as_array() | |
| 716 | + .unwrap() | |
| 717 | + .iter() | |
| 718 | + .find(|row| row["key"] == "iz") | |
| 719 | + .unwrap(); | |
| 720 | + assert_eq!( | |
| 721 | + iz["limit_bytes"], | |
| 722 | + serde_json::json!(512 * 1024 * 1024), | |
| 723 | + "{body}" | |
| 724 | + ); | |
| 725 | + } | |
| 726 | + | |
| 727 | + | |
| 678 | 728 | #[tokio::test] |
| 679 | 729 | async fn the_panel_edits_the_family_and_every_reader_follows_the_table() { |
| 680 | 730 | let Setup { |
| @@ -874,6 +924,7 @@ mod tests { | ||
| 874 | 924 | url: "http://127.0.0.1:7655".into(), |
| 875 | 925 | owner: None, |
| 876 | 926 | client_id: None, |
| 927 | + storage_limit_bytes: None, | |
| 877 | 928 | }]; |
| 878 | 929 | assert!(!im_core::services::seed_from(&store, &reseed).await.unwrap()); |
| 879 | 930 | let (_, body) = get_family(&router, Some(basic(&client_id, &secret))).await; |
| @@ -946,7 +997,7 @@ mod tests { | ||
| 946 | 997 | assert_eq!(body["error"], "owned"); |
| 947 | 998 | |
| 948 | 999 | // The keeper's next boot moves the address; the name is the panel's. |
| 949 | - im_core::services::edit(&store, "wiki", "Vikipedi", "http://wiki.example") | |
| 1000 | + im_core::services::edit(&store, "wiki", "Vikipedi", "http://wiki.example", None) | |
| 950 | 1001 | .await |
| 951 | 1002 | .unwrap(); |
| 952 | 1003 | let (status, body) = post_register( |
crates/im-web/src/i18n.rsmodified+3 -0
| @@ -147,6 +147,7 @@ pub enum Key { | ||
| 147 | 147 | ServiceKept, |
| 148 | 148 | ServiceMoveUp, |
| 149 | 149 | ServiceMoveDown, |
| 150 | + ServicesStorageLimit, | |
| 150 | 151 | SignOutEverywhere, |
| 151 | 152 | AdminPanelLink, |
| 152 | 153 | ViewPhotoAria, |
| @@ -525,6 +526,8 @@ pub fn t(lang: Lang, key: Key) -> &'static str { | ||
| 525 | 526 | (ServiceMoveUp, Tr) => "Yukarı taşı", |
| 526 | 527 | (ServiceMoveDown, En) => "Move down", |
| 527 | 528 | (ServiceMoveDown, Tr) => "Aşağı taşı", |
| 529 | + (ServicesStorageLimit, En) => "Storage limit", | |
| 530 | + (ServicesStorageLimit, Tr) => "Depolama limiti", | |
| 528 | 531 | (SignOutEverywhere, En) => "Sign out everywhere", |
| 529 | 532 | (SignOutEverywhere, Tr) => "Her yerde oturumu kapat", |
| 530 | 533 | (AdminPanelLink, En) => "Admin panel", |
crates/im-web/src/main.rsmodified+1 -0
| @@ -174,6 +174,7 @@ async fn seed_services(store: &Store, config: &Config) { | ||
| 174 | 174 | url: service.url.clone(), |
| 175 | 175 | owner: None, |
| 176 | 176 | client_id: None, |
| 177 | + storage_limit_bytes: None, | |
| 177 | 178 | }) |
| 178 | 179 | .collect(); |
| 179 | 180 | match im_core::services::seed_from(store, &seed).await { |