Compare commits

...

157 Commits

Author SHA1 Message Date
spinline
fddc81365b feat: complete modernization with shadcn, stateless auth, and performance optimizations
All checks were successful
Build MIPS Binary / build (push) Successful in 5m20s
2026-02-10 22:16:36 +03:00
spinline
8815727620 feat: migrate to stateless server functions for auth with jwt and shadcn ui
Some checks failed
Build MIPS Binary / build (push) Failing after 3s
2026-02-10 19:20:36 +03:00
spinline
c85c75659e feat: modernize stack with shadcn, struct_patch and msgpack
Some checks failed
Build MIPS Binary / build (push) Failing after 6s
2026-02-10 19:02:53 +03:00
spinline
4b3e713657 refactor: move DB to shared crate, convert push endpoints to server functions, remove dead REST handlers
All checks were successful
Build MIPS Binary / build (push) Successful in 5m17s
2026-02-10 02:05:04 +03:00
spinline
c2bf6e6fd5 fix: remove unnecessary SSE user auth check - login already guards access
All checks were successful
Build MIPS Binary / build (push) Successful in 5m17s
2026-02-10 01:43:40 +03:00
spinline
94bc7cb91d fix: patch coarsetime with portable-atomic for MIPS AtomicU64 support
All checks were successful
Build MIPS Binary / build (push) Successful in 5m18s
2026-02-10 01:29:06 +03:00
spinline
5e1f4b18c2 refactor: migrate torrent/settings endpoints to Leptos Server Functions and remove third_party/coarsetime
Some checks failed
Build MIPS Binary / build (push) Failing after 4m15s
2026-02-10 00:27:39 +03:00
spinline
3f370389aa fix: SSE auth by capturing store context before async block and redirect to setup when needed 2026-02-10 00:12:08 +03:00
spinline
a4fe8d065c debug: log user value in SSE loop
All checks were successful
Build MIPS Binary / build (push) Successful in 5m21s
2026-02-09 23:55:57 +03:00
spinline
3215b38272 fix: add missing spawn_local import
All checks were successful
Build MIPS Binary / build (push) Successful in 5m20s
2026-02-09 23:48:52 +03:00
spinline
8eb594e804 refactor: move SSE logic to spawn_local with continuous user check
Some checks failed
Build MIPS Binary / build (push) Failing after 1m15s
2026-02-09 22:48:00 +03:00
spinline
518af10cd7 fix: use .0 for reading and .1 for writing signals
All checks were successful
Build MIPS Binary / build (push) Successful in 5m20s
2026-02-09 22:37:06 +03:00
spinline
0304c5cb7d fix: prevent panic by using signals for redirects and fix auth flow
Some checks failed
Build MIPS Binary / build (push) Failing after 1m15s
2026-02-09 22:32:19 +03:00
spinline
cee609700a fix: use navigate inside Router context and fix auth redirect flow
Some checks failed
Build MIPS Binary / build (push) Has been cancelled
2026-02-09 22:30:59 +03:00
spinline
a9de8aeb5a debug: add more SSE logging to trace message receiving
All checks were successful
Build MIPS Binary / build (push) Successful in 5m20s
2026-02-09 22:21:39 +03:00
spinline
79a88306c3 fix: use .get() instead of .with() for RwSignal and fix indentation
All checks were successful
Build MIPS Binary / build (push) Successful in 5m21s
2026-02-09 22:12:05 +03:00
spinline
96ca09b9bd debug: add TorrentTable logging to trace UI rendering
Some checks failed
Build MIPS Binary / build (push) Has been cancelled
2026-02-09 22:09:55 +03:00
spinline
4d88660d91 debug: add SSE logging to trace torrent loading issue
Some checks failed
Build MIPS Binary / build (push) Has been cancelled
2026-02-09 22:07:44 +03:00
spinline
1c2fa499b8 fix: remove modal module declaration from components/mod.rs
All checks were successful
Build MIPS Binary / build (push) Successful in 5m21s
2026-02-09 21:59:33 +03:00
spinline
f121d5b220 fix: remove unused Modal component
Some checks failed
Build MIPS Binary / build (push) Failing after 1m18s
2026-02-09 21:51:40 +03:00
spinline
449227d019 fix: move #[allow(dead_code)] after #[component] in modal.rs
Some checks failed
Build MIPS Binary / build (push) Has been cancelled
2026-02-09 21:48:26 +03:00
spinline
bc47a4ac5c fix: SSE connection not starting after login
Some checks failed
Build MIPS Binary / build (push) Has been cancelled
- Move SSE initialization inside Effect closure to capture signals properly
- Fix closure capture issue where torrents/stats/notifications were borrowed before being passed to async block
- SSE now starts correctly when user is authenticated
2026-02-09 21:45:36 +03:00
spinline
a3bf33aee4 fix: remove unused scgi import from backend
Some checks failed
Build MIPS Binary / build (push) Has been cancelled
2026-02-09 21:41:31 +03:00
spinline
d7b1cef6d7 fix: syntax error in recursion limit attribute
All checks were successful
Build MIPS Binary / build (push) Successful in 5m22s
- Fix missing opening parenthesis in #![recursion_limit]
2026-02-09 21:33:47 +03:00
spinline
41bd4fcd1b fix: remove unused imports and suppress dead code warning
Some checks failed
Build MIPS Binary / build (push) Failing after 1m11s
- Remove unused leptos::logging, leptos::html, leptos::task::spawn_local from toast.rs
- Remove unused Torrent import from table.rs
- Add #[allow(dead_code)] to modal.rs for unused fields
2026-02-09 21:30:03 +03:00
spinline
4ed3f12d8b fix: add recursion limit for leptos 0.8
Some checks failed
Build MIPS Binary / build (push) Failing after 1m11s
- Add #![recursion_limit = "256"] to frontend crate
- This resolves queries overflow error in complex component trees
2026-02-09 21:28:30 +03:00
spinline
cd7d21cd48 fix: upgrade to leptos 0.8 with compatible deps
Some checks failed
Build MIPS Binary / build (push) Failing after 1m28s
- Update leptos-use from 0.15 to 0.16 for reactive_graph compatibility
- Fix web-sys ProgressElement -> ProgressEvent feature
- Resolve closure ownership issues in context_menu.rs and statusbar.rs
- Update Cargo dependencies for stable compilation
2026-02-09 21:25:46 +03:00
spinline
95a0d59cc4 fix(frontend): leptos 0.8 migration fixes - Callback::run, StoredValue::new_local, traits
Some checks failed
Build MIPS Binary / build (push) Failing after 1m22s
2026-02-09 20:39:23 +03:00
spinline
e1e8a89579 fix(frontend): address move semantics and signal/callback types for Leptos 0.8
Some checks failed
Build MIPS Binary / build (push) Failing after 1m22s
2026-02-09 20:24:42 +03:00
spinline
9a3aae3f37 refactor(frontend): partial fix for Leptos 0.8 migration (imports, view types)
Some checks failed
Build MIPS Binary / build (push) Failing after 1m23s
2026-02-09 20:18:50 +03:00
spinline
e6d00e9d55 modernize: migrate to Leptos 0.8 and Server Functions architecture, break backend->shared loop
Some checks failed
Build MIPS Binary / build (push) Failing after 1m27s
2026-02-09 20:07:28 +03:00
spinline
5a8f5169ea perf: implement smart merge logic for FullList to preserve reactive references
All checks were successful
Build MIPS Binary / build (push) Successful in 4m22s
2026-02-09 00:40:09 +03:00
spinline
afdc34e131 perf: use keyed <For /> and fine-grained reactivity in torrent table
Some checks failed
Build MIPS Binary / build (push) Has been cancelled
2026-02-09 00:39:44 +03:00
spinline
d15392e148 fix: add timeout to SCGI requests to prevent background loop hang
All checks were successful
Build MIPS Binary / build (push) Successful in 4m22s
2026-02-09 00:28:07 +03:00
spinline
f3121898e2 fix: wake up background polling loop immediately when a client connects
All checks were successful
Build MIPS Binary / build (push) Successful in 4m22s
2026-02-09 00:22:28 +03:00
spinline
e1370db6ce ui: rename Down Speed to DL Speed in torrent table
All checks were successful
Build MIPS Binary / build (push) Successful in 4m26s
2026-02-09 00:07:07 +03:00
spinline
1432dec828 perf: implement dynamic polling interval based on active clients
All checks were successful
Build MIPS Binary / build (push) Successful in 4m35s
2026-02-08 23:57:32 +03:00
spinline
1bb3475d61 perf: optimize torrent store with HashMap for O(1) updates
All checks were successful
Build MIPS Binary / build (push) Successful in 4m57s
2026-02-08 23:52:23 +03:00
spinline
cffc88443a feat: add centralized API service layer for frontend
All checks were successful
Build MIPS Binary / build (push) Successful in 5m18s
- Create frontend/src/api/mod.rs with centralized HTTP client and error handling
- Implement api::auth module (login, logout, check_auth, get_user)
- Implement api::torrent module (add, action, delete, start, stop, set_label, set_priority)
- Implement api::setup module (get_status, setup)
- Implement api::settings module (set_global_limits)
- Implement api::push module (get_public_key, subscribe)
- Update all components to use api service layer instead of direct gloo_net calls
- Add thiserror dependency for error handling
2026-02-08 23:27:13 +03:00
spinline
129a4c7586 refactor: move AddTorrentRequest to shared library for type safety
All checks were successful
Build MIPS Binary / build (push) Successful in 4m28s
2026-02-08 22:43:35 +03:00
spinline
f2dfa7963e refactor: modernize browser notifications with leptos-use and refactor utils/notification.rs
All checks were successful
Build MIPS Binary / build (push) Successful in 4m31s
2026-02-08 22:30:46 +03:00
spinline
d3792e78e0 fix: ios safari mobile dropdown and click bubbling issues resolved with global closer and native details
All checks were successful
Build MIPS Binary / build (push) Successful in 5m45s
2026-02-08 21:49:00 +03:00
spinline
384165a958 fix: ios safari mobile dropdown and click bubbling issues resolved with cursor-pointer and css fixes
All checks were successful
Build MIPS Binary / build (push) Successful in 4m27s
2026-02-08 21:28:10 +03:00
spinline
7169e44f4e fix: incorrect import of on_click_outside in statusbar.rs
All checks were successful
Build MIPS Binary / build (push) Successful in 4m28s
2026-02-08 21:00:32 +03:00
spinline
51fb85c2d8 fix: iOS Safari uyumluluğu için menü yapıları ve click bubbling sorunları giderildi
Some checks failed
Build MIPS Binary / build (push) Failing after 1m8s
2026-02-08 20:58:14 +03:00
spinline
46882caea0 fix: mobilde menülerin açılmama sorunu HTML details/summary yapısı ile kökten çözüldü
All checks were successful
Build MIPS Binary / build (push) Successful in 4m31s
2026-02-08 20:47:42 +03:00
spinline
e339ca1c49 fix: menülerin dışarıya tıklayınca kapanmama sorunu giderildi (stop_propagation kaldırıldı ve click event'ine geçildi)
All checks were successful
Build MIPS Binary / build (push) Successful in 4m34s
2026-02-08 20:35:44 +03:00
spinline
a08fd9698f refactor: açılır menüler leptos-use::on_click_outside ile modernize edildi, şeffaf backdrop katmanları kaldırıldı
All checks were successful
Build MIPS Binary / build (push) Successful in 5m52s
2026-02-08 20:16:05 +03:00
spinline
7d46dbd437 refactor: tema yönetimi leptos-use::use_local_storage ile reaktif hale getirildi
All checks were successful
Build MIPS Binary / build (push) Successful in 4m28s
2026-02-08 20:02:01 +03:00
spinline
5f107299e3 refactor: long press mantığı leptos-use::use_timeout_fn ile modernize edildi
All checks were successful
Build MIPS Binary / build (push) Successful in 4m29s
2026-02-08 19:53:10 +03:00
spinline
c34133ded1 refactor: manuel Closure ve timer yönetimi Leptos set_timeout ile değiştirildi
All checks were successful
Build MIPS Binary / build (push) Successful in 4m16s
2026-02-08 19:39:38 +03:00
spinline
0d059cbbd3 fix: push notification permission ve toJSON çağrıları tip güvenli hale getirildi
All checks were successful
Build MIPS Binary / build (push) Successful in 4m14s
2026-02-08 19:30:05 +03:00
spinline
fc83a1cc65 refactor: js_sys::eval kullanımı kaldırıldı, Base64 çözümleme pure Rust ile güncellendi
All checks were successful
Build MIPS Binary / build (push) Successful in 4m14s
2026-02-08 19:25:36 +03:00
spinline
4e81af0599 fix: unused import warning for utoipa::OpenApi in production build
All checks were successful
Build MIPS Binary / build (push) Successful in 4m18s
2026-02-08 18:49:54 +03:00
spinline
74c3c5c17e feat: Swagger UI varsayılan (dev) özelliklere eklendi, production build'inden muaf tutuldu
All checks were successful
Build MIPS Binary / build (push) Successful in 4m18s
2026-02-08 18:43:55 +03:00
spinline
3632a578e1 build: CI/CD ve optimizasyon süreci en sade ve güvenilir haline getirildi
Some checks failed
Build MIPS Binary / build (push) Has been cancelled
2026-02-08 18:41:40 +03:00
spinline
8a9905fc56 fix: WASM dosyasının bozulmasına neden olan hatalı manuel optimizasyon adımı kaldırıldı
All checks were successful
Build MIPS Binary / build (push) Successful in 6m45s
2026-02-08 18:21:40 +03:00
spinline
1e39cbb0c5 perf: backend binary boyutunu düşürmek için Swagger UI opsiyonel yapıldı ve build komutu optimize edildi
All checks were successful
Build MIPS Binary / build (push) Successful in 4m31s
2026-02-08 18:16:45 +03:00
spinline
40be58f2fc perf: backend derleme süreci kök dizine taşınarak workspace optimizasyonları aktif edildi
All checks were successful
Build MIPS Binary / build (push) Successful in 4m30s
2026-02-08 18:10:09 +03:00
spinline
3f08b5b54a perf: WASM boyut takibi loglara eklendi ve profil çakışmaları giderildi
All checks were successful
Build MIPS Binary / build (push) Successful in 4m27s
2026-02-08 18:03:52 +03:00
spinline
bfec99ae35 fix: wasm-opt için --all-features bayrağı kullanılarak flag uyuşmazlığı giderildi
All checks were successful
Build MIPS Binary / build (push) Successful in 4m28s
2026-02-08 17:58:14 +03:00
spinline
d9afd3aa81 fix: wasm-opt için nontrapping-float-to-int-conversions özelliği eklendi
Some checks failed
Build MIPS Binary / build (push) Failing after 1m2s
2026-02-08 17:56:28 +03:00
spinline
e72113d91d perf: manuel WASM optimizasyonu eklendi ve build süreci stabilize edildi
Some checks failed
Build MIPS Binary / build (push) Failing after 1m2s
2026-02-08 17:54:43 +03:00
spinline
7c4ff619c1 fix: .cargo/config.toml yazım hatası düzeltildi
Some checks failed
Build MIPS Binary / build (push) Failing after 1m2s
2026-02-08 17:52:15 +03:00
spinline
9c4217f450 feat: WASM için bulk-memory özelliği aktif edildi
Some checks failed
Build MIPS Binary / build (push) Failing after 3s
2026-02-08 17:51:04 +03:00
spinline
cc09002171 trigger: yeniden build başlatıldı
Some checks failed
Build MIPS Binary / build (push) Failing after 1m3s
2026-02-08 17:49:06 +03:00
spinline
5d8cdd7760 build: build ortamı güncellendi (Trunk v0.21.14 ve binaryen eklendi), optimizasyonlar tekrar açıldı
Some checks failed
Build MIPS Binary / build (push) Failing after 1m2s
2026-02-08 16:51:40 +03:00
spinline
145436eefc fix: build hatasını aşmak için wasm-opt geçici olarak devre dışı bırakıldı
All checks were successful
Build MIPS Binary / build (push) Successful in 4m30s
2026-02-08 16:44:29 +03:00
spinline
10c95c5ff3 fix: wasm-opt build hatası için rustc ve wasm-opt versiyon ayarları güncellendi
Some checks failed
Build MIPS Binary / build (push) Failing after 1m8s
2026-02-08 16:42:13 +03:00
spinline
329654cc4e fix: wasm-opt build hatası için bulk-memory özelliği devre dışı bırakıldı
Some checks failed
Build MIPS Binary / build (push) Failing after 1m31s
2026-02-08 16:37:45 +03:00
spinline
22b592a652 fix: wasm-opt seviyesi 'z' olarak güncellendi
Some checks failed
Build MIPS Binary / build (push) Failing after 1m35s
2026-02-08 16:33:46 +03:00
spinline
817dc49db2 fix: wasm-opt build hatası için --enable-bulk-memory flag'i eklendi
Some checks failed
Build MIPS Binary / build (push) Failing after 3s
2026-02-08 16:29:33 +03:00
spinline
b2a60d3d1e cleanup: kullanılmayan get_vapid_public_key fonksiyonu kaldırıldı
Some checks failed
Build MIPS Binary / build (push) Failing after 1m6s
2026-02-08 16:26:16 +03:00
spinline
520903fa3f perf: push bildirimleri paralel gönderim ve env var önbelleğe alma ile optimize edildi
Some checks failed
Build MIPS Binary / build (push) Has been cancelled
2026-02-08 16:25:44 +03:00
spinline
c45f2f50e9 fix: ARM64 build hatası için wasm-opt versiyonu v117 olarak güncellendi
Some checks failed
Build MIPS Binary / build (push) Has been cancelled
2026-02-08 16:25:02 +03:00
spinline
791eabe9bd fix: SQLite deadlock ve busy_timeout yönetimi iyileştirildi
Some checks failed
Build MIPS Binary / build (push) Failing after 1m2s
2026-02-08 16:20:55 +03:00
spinline
12f93dd640 perf: Trunk WASM optimizasyonu aktif edildi (data-wasm-opt=0 kaldırıldı)
Some checks failed
Build MIPS Binary / build (push) Failing after 1m2s
2026-02-08 16:18:50 +03:00
spinline
7306db8c2f fix: torrent diff algoritması hash tabanlı hale getirilerek sıralama bağımlılığı kaldırıldı
Some checks failed
Build MIPS Binary / build (push) Has been cancelled
2026-02-08 16:17:30 +03:00
spinline
ce0ecd62af fix: index.html yükleme ekranına zaman aşımı (15sn) ve hata mesajı eklendi
Some checks failed
Build MIPS Binary / build (push) Has been cancelled
2026-02-08 16:13:20 +03:00
spinline
f2379b67d8 docs: main.rs içindeki güncelliğini yitirmiş şifre güncelleme yorumu temizlendi
Some checks failed
Build MIPS Binary / build (push) Has been cancelled
2026-02-08 16:11:18 +03:00
spinline
755f35c94c security: gerçek .env dosyası takipten çıkarıldı ve .env.example güncellendi
Some checks failed
Build MIPS Binary / build (push) Has been cancelled
2026-02-08 16:07:26 +03:00
spinline
175cac953e fix: SSE bağlantısı sadece giriş yapıldıktan sonra başlatılacak şekilde düzeltildi
All checks were successful
Build MIPS Binary / build (push) Successful in 4m23s
2026-02-08 15:57:24 +03:00
spinline
2c812fc4f6 fix: login rate limit 5 deneme ve 1 dakika bekleme olarak güncellendi
Some checks failed
Build MIPS Binary / build (push) Has been cancelled
2026-02-08 15:56:07 +03:00
spinline
08df851970 feat: login rate limit için frontend uyarı mesajı ve IP bazlı limit aktif edildi
Some checks failed
Build MIPS Binary / build (push) Has been cancelled
2026-02-08 15:54:54 +03:00
spinline
35faa6bfda test: global rate limit denemesi
All checks were successful
Build MIPS Binary / build (push) Successful in 4m21s
2026-02-08 15:47:00 +03:00
spinline
328019e438 fix: login rate limit ayarları daha katı hale getirildi
All checks were successful
Build MIPS Binary / build (push) Successful in 4m22s
2026-02-08 13:59:08 +03:00
spinline
4f1c6326fd feat: login sistemi için tower-governor ile IP bazlı rate limit eklendi
All checks were successful
Build MIPS Binary / build (push) Successful in 4m21s
2026-02-08 13:48:04 +03:00
spinline
2e36c28c0d fix(frontend): replace unwrap() with expect() for better error messages
All checks were successful
Build MIPS Binary / build (push) Successful in 4m13s
- console_log::init_with_level() now uses expect()
- web_sys::window() now uses expect() with helpful message
- window.document() now uses expect()
- document.body() now uses expect()

This provides meaningful error messages if WASM initialization fails.
2026-02-08 05:41:07 +03:00
spinline
6530e20af2 perf(db): enable SQLite WAL mode and performance settings
All checks were successful
Build MIPS Binary / build (push) Successful in 4m14s
- PRAGMA journal_mode=WAL - concurrent reads while writing
- PRAGMA synchronous=NORMAL - faster than FULL, still safe
- PRAGMA busy_timeout=5000 - reduces database locked errors

Note: Existing databases should be deleted to enable WAL mode properly.
2026-02-08 05:34:06 +03:00
spinline
32f4946530 fix: show N/A for magnet link dates
All checks were successful
Build MIPS Binary / build (push) Successful in 4m14s
Magnet links don't have creation_date, so timestamp is 0.
Now shows 'N/A' instead of 01/01/1970 00:00
2026-02-08 05:28:14 +03:00
spinline
619951fa1c security: remove hardcoded VAPID keys fallback
All checks were successful
Build MIPS Binary / build (push) Successful in 4m14s
VAPID keys must now be set via environment variables or .env file.
This eliminates the security risk of having keys in source code.
2026-02-08 05:16:31 +03:00
spinline
6d45e6773f chore: add DATABASE_URL to .env
All checks were successful
Build MIPS Binary / build (push) Successful in 4m14s
2026-02-08 05:11:31 +03:00
spinline
2c8a2d5956 feat(db): add migrations system and push subscriptions persistence
Some checks failed
Build MIPS Binary / build (push) Has been cancelled
- Add sqlx migration system with migrations/ directory
- Create 001_init.sql and 002_push_subscriptions.sql migration files
- Move from manual CREATE TABLE to version-controlled migrations
- Add push_subscriptions table with DB persistence
- PushSubscriptionStore now loads from DB on startup
- Add save/remove/get methods for push subscriptions in db.rs
- Move VAPID keys to .env file (with fallback to hardcoded values)
- Delete old vibetorrent.db and recreate with migrations
2026-02-08 05:10:57 +03:00
spinline
6acb299fbe fix(mobile): add type=button and remove overlay
All checks were successful
Build MIPS Binary / build (push) Successful in 4m11s
2026-02-08 04:37:16 +03:00
spinline
ab49c2ded5 fix(mobile): use pointerdown like statusbar
All checks were successful
Build MIPS Binary / build (push) Successful in 4m15s
2026-02-08 04:27:12 +03:00
spinline
e4957e930d fix(mobile): fix sort dropdown button events
All checks were successful
Build MIPS Binary / build (push) Successful in 4m14s
2026-02-08 04:17:08 +03:00
spinline
ad2c6dc56e feat(torrent): add date sorting and display
All checks were successful
Build MIPS Binary / build (push) Successful in 4m15s
- Sort torrents by added date (newest first) by default
- Add Date column to desktop table (after ETA)
- Add Date to mobile card view (grid-cols-3 -> grid-cols-4)
- Add Date option to mobile sort dropdown
- Display dates in DD/MM/YYYY HH:mm format
- Add chrono wasm-bindgen feature
2026-02-08 04:10:02 +03:00
spinline
9f009bc18b Auto-login user after setup and redirect to dashboard
All checks were successful
Build MIPS Binary / build (push) Successful in 4m14s
2026-02-07 19:54:14 +03:00
spinline
643b83ac21 Remove unused leptos_router imports from login and setup components
All checks were successful
Build MIPS Binary / build (push) Successful in 4m8s
2026-02-07 19:49:58 +03:00
spinline
90b65240b2 Restore required utoipa::OpenApi import
Some checks failed
Build MIPS Binary / build (push) Has been cancelled
2026-02-07 19:47:06 +03:00
spinline
69243a5590 Redirect authenticated users away from login/setup pages
Some checks failed
Build MIPS Binary / build (push) Failing after 3m27s
2026-02-07 19:39:53 +03:00
spinline
10262142fc Fix unused OpenApi import warning
Some checks failed
Build MIPS Binary / build (push) Failing after 3m25s
2026-02-07 19:34:41 +03:00
spinline
858a1c9b63 Fix compilation errors: Restore missing delete_session method and ApiDoc struct
All checks were successful
Build MIPS Binary / build (push) Successful in 4m7s
2026-02-07 19:28:47 +03:00
spinline
edfb7458f8 Add CLI password reset feature: --reset-password <USERNAME>
Some checks failed
Build MIPS Binary / build (push) Failing after 3m24s
2026-02-07 19:18:10 +03:00
spinline
575cfa4b38 Add 'Remember Me' feature to login (extends session to 30 days)
All checks were successful
Build MIPS Binary / build (push) Successful in 4m7s
2026-02-07 19:05:52 +03:00
spinline
9b18b97c49 Fetch and display actual username in sidebar profile section
All checks were successful
Build MIPS Binary / build (push) Successful in 4m8s
2026-02-07 17:17:16 +03:00
spinline
88723352fd Fix sidebar overlap: Add bottom padding to account for fixed status bar
All checks were successful
Build MIPS Binary / build (push) Successful in 4m12s
2026-02-07 17:07:30 +03:00
spinline
4231e0b3a7 Adjust sidebar layout to push profile section to the bottom and reduce avatar size
All checks were successful
Build MIPS Binary / build (push) Successful in 4m7s
2026-02-07 16:59:31 +03:00
spinline
1177412c87 Add user profile and logout button to sidebar footer
All checks were successful
Build MIPS Binary / build (push) Successful in 4m7s
2026-02-07 16:40:55 +03:00
spinline
aed753c64f Lower bcrypt cost to 6 to improve login speed on low-end hardware
All checks were successful
Build MIPS Binary / build (push) Successful in 4m8s
2026-02-07 16:24:06 +03:00
spinline
9d0eb11f16 Refactor App routing to fix infinite recursion and stack overflow errors
All checks were successful
Build MIPS Binary / build (push) Successful in 4m8s
2026-02-07 16:10:06 +03:00
spinline
3c13f99652 Improve App initialization logic with better error handling and logging to prevent infinite loading state
All checks were successful
Build MIPS Binary / build (push) Successful in 4m6s
2026-02-07 15:45:51 +03:00
spinline
a948215538 Change cookie SameSite policy to Lax to fix login redirection issue
All checks were successful
Build MIPS Binary / build (push) Successful in 4m6s
2026-02-07 15:38:56 +03:00
spinline
13424fceeb Demote 'Torrent status changed' log from INFO to DEBUG to reduce console noise
All checks were successful
Build MIPS Binary / build (push) Successful in 4m6s
2026-02-07 15:33:16 +03:00
spinline
e3eb5fbca9 Add detailed logging to login handler and use full page reload for auth navigation
All checks were successful
Build MIPS Binary / build (push) Successful in 4m7s
2026-02-07 15:28:44 +03:00
spinline
08f2f540fe Fix unused import and dead code warnings
All checks were successful
Build MIPS Binary / build (push) Successful in 4m6s
2026-02-07 15:20:23 +03:00
spinline
7361421641 Fix middleware signature: Specify Request<Body> explicitly
All checks were successful
Build MIPS Binary / build (push) Successful in 4m7s
2026-02-07 15:13:05 +03:00
spinline
d6ecc08398 Upgrade axum-extra to 0.10 for Axum 0.8 compatibility
Some checks failed
Build MIPS Binary / build (push) Has been cancelled
2026-02-07 15:11:08 +03:00
spinline
472bac85f3 Fix compilation errors: Resolve utoipa derive issues, add time dependency, and correct Axum middleware signature
Some checks failed
Build MIPS Binary / build (push) Has been cancelled
2026-02-07 15:08:53 +03:00
spinline
bb3ec14a75 Fix compilation errors: Add missing dependencies, fix module visibility, and update Axum middleware types
Some checks failed
Build MIPS Binary / build (push) Failing after 3m27s
2026-02-07 14:58:35 +03:00
spinline
d53d661ad1 Implement authentication system with SQLite: Add login/setup pages, auth middleware, and database integration
Some checks failed
Build MIPS Binary / build (push) Failing after 3m42s
2026-02-07 14:43:25 +03:00
spinline
92720c15b3 Fix mobile dropdown interaction: Revert to pointerdown with stop_propagation and use overlay for reliable closing
All checks were successful
Build MIPS Binary / build (push) Successful in 3m57s
2026-02-07 14:14:37 +03:00
spinline
5e59f66056 Fix dropdown closing behavior on mobile by adding transparent overlay
All checks were successful
Build MIPS Binary / build (push) Successful in 3m45s
2026-02-07 13:54:00 +03:00
spinline
f2ca741188 Fix cache issue by updating service worker strategy and remove GitHub workflows
All checks were successful
Build MIPS Binary / build (push) Successful in 3m44s
2026-02-07 13:41:00 +03:00
spinline
767077195a chore: trigger ci build to use runner v3 (with file command)
All checks were successful
Build MIPS Binary / build (push) Successful in 4m16s
2026-02-07 01:26:33 +03:00
spinline
5539dc2289 chore: trigger ci build to use runner v2
Some checks failed
Build MIPS Binary / build (push) Failing after 4m17s
2026-02-07 00:50:13 +03:00
spinline
f99fc4a134 fix: use pure MIPS asm for __atomic_is_lock_free, remove unsupported --defsym
Some checks are pending
Build MIPS Binary / build (push) Waiting to run
2026-02-07 00:40:39 +03:00
spinline
f4d0351c5b chore: update runner shim and linker flags for mips atomics
Some checks failed
Build MIPS Binary / build (push) Failing after 3m52s
2026-02-07 00:19:36 +03:00
spinline
c2492b2749 fix(ci): let zig handle CRT object files via link-self-contained=no
Some checks failed
Build MIPS Binary / build (push) Has been cancelled
2026-02-07 00:04:48 +03:00
spinline
18001ed5a2 fix(ci): use build-std for mips target in Dockerfile
Some checks failed
Build MIPS Binary / build (push) Failing after 4m8s
2026-02-06 23:58:19 +03:00
spinline
47da0fca55 ci: test updated runner environment
Some checks failed
Build MIPS Binary / build (push) Failing after 1m2s
2026-02-06 23:48:56 +03:00
spinline
0985f328e2 fix(ci): inject libatomic for mips and force static linking
Some checks failed
Build MIPS Binary / build (push) Failing after 1m2s
2026-02-06 23:41:30 +03:00
spinline
146b312b4c fix(runner): use reliable URL for act_runner and optimize targets
Some checks failed
Build MIPS Binary / build (push) Failing after 4m4s
2026-02-06 23:32:58 +03:00
spinline
153568e81d ci: trigger first super-fast build
Some checks failed
Build MIPS Binary / build (push) Has been cancelled
2026-02-06 23:19:37 +03:00
spinline
fefe86da31 refactor: simplify workflow to use new custom runner with pre-installed tools
Some checks failed
Build MIPS Binary / build (push) Has been cancelled
2026-02-06 22:34:40 +03:00
spinline
11ba548297 chore: remove failing publish workflow due to missing docker in runner
Some checks failed
Build MIPS Binary / build (push) Has been cancelled
2026-02-06 22:31:50 +03:00
spinline
ce9fb6781a ci: configure runner to use host mode with mips-builder label
Some checks failed
Build MIPS Binary / build (push) Has been cancelled
Publish Runner Image / Push Docker image to Docker Hub (push) Failing after 3m18s
2026-02-06 22:22:03 +03:00
spinline
4855b193d4 ci: add workflow to publish runner image to docker hub
Some checks failed
Publish Runner Image / Push Docker image to Docker Hub (push) Has been cancelled
Build MIPS Binary / build (push) Has been cancelled
2026-02-06 22:18:01 +03:00
spinline
1d636d63fa feat: add self-contained gitea runner dockerfile with all build tools included
Some checks failed
Build MIPS Binary / build (push) Has been cancelled
2026-02-06 22:16:04 +03:00
spinline
db4edff957 ci: fix trunk binary arch download logic for arm64 runner
Some checks failed
Build MIPS Binary / build (push) Failing after 8m10s
2026-02-06 22:04:15 +03:00
spinline
921cba2cab ci: remove broken cache and use prebuilt trunk binary for speed
Some checks failed
Build MIPS Binary / build (push) Failing after 1m6s
2026-02-06 22:02:18 +03:00
spinline
c64c95233f ci: add action/cache to speed up builds
Some checks failed
Build MIPS Binary / build (push) Has been cancelled
2026-02-06 21:49:09 +03:00
spinline
d17bfc88ad ci: replace docker build with cargo-zigbuild for mips cross-compilation
Some checks failed
Build MIPS Binary / build (push) Has been cancelled
2026-02-06 21:45:35 +03:00
spinline
b646d0851c ci: remove --locked from trunk install to avoid yanked crate warnings
Some checks failed
Build MIPS Binary / build (push) Failing after 10m36s
2026-02-06 21:33:40 +03:00
spinline
42e03bd2e3 ci: manually install node 20 to path to fix structuredClone error
Some checks failed
Build MIPS Binary / build (push) Has been cancelled
2026-02-06 21:31:23 +03:00
spinline
67a1d96b26 ci: debug tailwind build failure by running explicitly
Some checks failed
Build MIPS Binary / build (push) Failing after 9m30s
2026-02-06 21:20:59 +03:00
spinline
3187ed76b0 ci: install wasm-bindgen-cli from source to fix glibc error
Some checks failed
Build MIPS Binary / build (push) Failing after 10m54s
2026-02-06 21:05:50 +03:00
spinline
4dfce1096e ci: set nightly default and install Node 20 for trunk
Some checks failed
Build MIPS Binary / build (push) Failing after 9m9s
2026-02-06 20:53:41 +03:00
spinline
e3cfc11b65 ci: source cargo env in trunk/frontend steps
Some checks failed
Build MIPS Binary / build (push) Failing after 8m12s
2026-02-06 20:42:22 +03:00
spinline
f579431098 ci: install rustup in runner container if missing
Some checks failed
Build MIPS Binary / build (push) Failing after 1m2s
2026-02-06 20:41:05 +03:00
spinline
6014ec64e8 ci: replace checkout action with direct git fetch
Some checks failed
Build MIPS Binary / build (push) Failing after 1s
2026-02-06 20:39:46 +03:00
spinline
bdb30f33d8 ci: trigger server runner
Some checks failed
Build MIPS Binary / build (push) Failing after 34s
2026-02-06 20:33:16 +03:00
spinline
2ea2894664 ci: test build speed with cached image
All checks were successful
Build MIPS Binary / build (push) Successful in 4m47s
2026-02-06 19:22:37 +03:00
spinline
e329275956 ci: use cross-rs image with Rust for proper sysroot, cache builder image
All checks were successful
Build MIPS Binary / build (push) Successful in 7m39s
2026-02-06 19:10:57 +03:00
spinline
1f7af5fe30 ci: remove RUSTFLAGS, musl target is static by default
All checks were successful
Build MIPS Binary / build (push) Successful in 13m49s
2026-02-06 18:51:49 +03:00
spinline
bf54d2699e ci: force static linking with crt-static and -static flags
Some checks failed
Build MIPS Binary / build (push) Failing after 10m7s
2026-02-06 18:39:40 +03:00
spinline
46d61b5562 fix: suppress unused_assignments warning for got_first_message
Some checks failed
Build MIPS Binary / build (push) Has been cancelled
2026-02-06 18:33:18 +03:00
76 changed files with 6320 additions and 6148 deletions

View File

@@ -9,55 +9,39 @@ env:
jobs:
build:
runs-on: ubuntu-latest
runs-on: mips-builder
steps:
- uses: actions/checkout@v4
- name: Setup Rust
- name: Checkout
env:
GIT_TOKEN: ${{ secrets.RELEASE_TOKEN }}
run: |
rustup toolchain install nightly --profile minimal
rustup target add wasm32-unknown-unknown --toolchain nightly
rustup component add rust-src --toolchain nightly
rustc +nightly --version
- name: Install Trunk
run: |
if ! command -v trunk &> /dev/null; then
cargo install trunk --locked
fi
rm -rf .git
git init .
git remote add origin https://admin:$\{GIT_TOKEN\}@git.karatatar.com/admin/vibetorrent.git
git fetch --depth=1 origin ${{ gitea.sha }}
git checkout FETCH_HEAD
- name: Build Frontend
run: |
cd frontend
npm install
npx @tailwindcss/cli -i input.css -o public/tailwind.css --minify --content './src/**/*.rs'
# Trunk'ın optimizasyonunu kapalı (0) tutuyoruz çünkü Cargo.toml'daki opt-level='z' zaten o işi yapıyor.
trunk build --release
- name: Install Cross
run: |
if ! command -v cross &> /dev/null; then
cargo install cross --locked
fi
- name: Build Backend (MIPS)
env:
# -s ve -w ile binary içindeki gereksiz tüm yükleri siliyoruz.
RUSTFLAGS: "-C target-feature=+crt-static -C link-self-contained=no -C link-arg=-msoft-float -C link-arg=-s -C link-arg=-w"
CFLAGS_mips_unknown_linux_musl: "-msoft-float"
run: |
docker run --rm --platform linux/amd64 \
-v "$PWD":/project \
-v cargo-cache:/usr/local/cargo/registry \
-w /project \
rust:latest \
bash -c '
rustup toolchain install nightly --component rust-src &&
apt-get update -qq && apt-get install -y -qq musl-tools wget >/dev/null 2>&1 &&
wget -qO- https://musl.cc/mips-linux-musl-cross.tgz | tar xz -C /opt/ &&
export PATH="/opt/mips-linux-musl-cross/bin:$PATH" &&
export CARGO_TARGET_MIPS_UNKNOWN_LINUX_MUSL_LINKER=mips-linux-musl-gcc &&
cd backend &&
cargo +nightly build --target mips-unknown-linux-musl --release -Z build-std=std,panic_abort
'
# Sadece gerekli özellikleri derliyoruz (Boyut tasarrufu için swagger kapalı)
cargo zigbuild -p backend --target mips-unknown-linux-musl --release -Z build-std=std,panic_abort --no-default-features --features push-notifications
- name: Rename Binary
run: mv target/mips-unknown-linux-musl/release/backend target/mips-unknown-linux-musl/release/vibetorrent-mips
- name: Create Release Assets
run: |
mv target/mips-unknown-linux-musl/release/backend target/mips-unknown-linux-musl/release/vibetorrent-mips
- name: Generate Release Tag
id: tag
@@ -71,7 +55,6 @@ jobs:
REPO="admin/vibetorrent"
API_URL="${{ gitea.server_url }}/api/v1"
# Create release
RELEASE_RESPONSE=$(curl -s -X POST "${API_URL}/repos/${REPO}/releases" \
-H "Authorization: token ${RELEASE_TOKEN}" \
-H "Content-Type: application/json" \
@@ -84,18 +67,9 @@ jobs:
}")
RELEASE_ID=$(echo "$RELEASE_RESPONSE" | jq -r '.id')
echo "Release ID: $RELEASE_ID"
if [ "$RELEASE_ID" = "null" ] || [ -z "$RELEASE_ID" ]; then exit 1; fi
if [ "$RELEASE_ID" = "null" ] || [ -z "$RELEASE_ID" ]; then
echo "Failed to create release:"
echo "$RELEASE_RESPONSE"
exit 1
fi
# Upload binary as release asset
curl -s -X POST "${API_URL}/repos/${REPO}/releases/${RELEASE_ID}/assets?name=vibetorrent-mips" \
-H "Authorization: token ${RELEASE_TOKEN}" \
-H "Content-Type: application/octet-stream" \
--data-binary @target/mips-unknown-linux-musl/release/vibetorrent-mips
echo "Release ${TAG} created with binary attached."
--data-binary @target/mips-unknown-linux-musl/release/vibetorrent-mips

View File

@@ -1,80 +0,0 @@
name: Build MIPS Binary
on:
push:
branches: [ "main" ]
workflow_dispatch:
permissions:
contents: write
env:
CARGO_TERM_COLOR: always
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Debug - List Files (Pre-Build)
run: ls -R
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: '20'
- name: Install Rust
uses: dtolnay/rust-toolchain@nightly
with:
targets: wasm32-unknown-unknown
components: rust-src
- name: Rust Cache
uses: Swatinem/rust-cache@v2
- name: Install Trunk
uses: jetli/trunk-action@v0.5.0
with:
version: 'latest'
- name: Build Frontend
run: |
cd frontend
npm install
trunk build --release
- name: Install Cross
run: cargo install cross
- name: Build Backend (MIPS)
env:
RUSTUP_TOOLCHAIN: nightly
CROSS_NO_WARNINGS: 0
run: |
cd backend
cross build --target mips-unknown-linux-musl --release -Z build-std=std,panic_abort
- name: Debug - List Files
run: |
echo "Listing target directory..."
find target -maxdepth 5 || true
ls -R target/mips-unknown-linux-musl/release || true
- name: Rename Binary
run: mv target/mips-unknown-linux-musl/release/backend target/mips-unknown-linux-musl/release/vibetorrent-mips
- name: Generate Tag
id: tag
run: echo "release_tag=release-$(date +'%Y%m%d-%H%M')" >> $GITHUB_OUTPUT
- name: Create Release
uses: softprops/action-gh-release@v1
with:
tag_name: ${{ steps.tag.outputs.release_tag }}
name: Release ${{ steps.tag.outputs.release_tag }}
files: target/mips-unknown-linux-musl/release/vibetorrent-mips
draft: false
prerelease: false

2
.gitignore vendored
View File

@@ -6,3 +6,5 @@ result.xml
frontend/dist
backend.log
.runner
.env
backend/.env

2162
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@@ -2,13 +2,20 @@
members = ["backend", "frontend", "shared"]
resolver = "2"
# Optimize for size (aggressive)
[profile.release]
# En küçük binary boyutu
opt-level = "z"
lto = true
# En derin kod temizliği (dead code elimination)
lto = "fat"
# En iyi optimizasyon için tek birim derleme
codegen-units = 1
# Hata izleme kodlarını atarak yer kazan
panic = "abort"
# Sembolleri ve hata ayıklama bilgilerini kesin sil
strip = true
# Artık (incremental) build'i kapat ki optimizasyon tam olsun
incremental = false
[patch.crates-io]
coarsetime = { path = "third_party/coarsetime" }
coarsetime = { path = "patches/coarsetime" }

View File

@@ -3,3 +3,12 @@ RTORRENT_SOCKET=/tmp/rtorrent.sock
# Backend Listen Port
PORT=3000
# Database URL
DATABASE_URL=sqlite:vibetorrent.db
# VAPID Keys for Push Notifications
# Generate new keys for production using: npx web-push generate-vapid-keys
VAPID_PUBLIC_KEY=YOUR_PUBLIC_VAPID_KEY
VAPID_PRIVATE_KEY=YOUR_PRIVATE_VAPID_KEY
VAPID_EMAIL=mailto:your-email@example.com

View File

@@ -4,32 +4,46 @@ version = "0.1.0"
edition = "2021"
[features]
default = ["push-notifications"]
default = ["swagger"] # push-notifications kaldırıldı
push-notifications = ["web-push", "openssl"]
swagger = ["utoipa-swagger-ui"]
[dependencies]
axum = { version = "0.8", features = ["macros", "ws"] }
tokio = { version = "1", features = ["full"] }
tower = { version = "0.4", features = ["util", "timeout"] }
tower-http = { version = "0.5", features = ["fs", "trace", "cors", "compression-full"] }
tower = { version = "0.5", features = ["util", "timeout"] }
tower-http = { version = "0.6", features = ["fs", "trace", "cors", "compression-full"] }
serde = { version = "1", features = ["derive"] }
serde_json = "1"
rmp-serde = "1.3"
struct-patch = "0.5"
tracing = "0.1"
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
tokio-stream = "0.1"
bytes = "1"
futures = "0.3"
quick-xml = { version = "0.31", features = ["serde", "serialize"] }
# We might need `tokio-util` for codecs if we implement SCGI manually
tokio-util = { version = "0.7", features = ["codec", "io"] }
clap = { version = "4.4", features = ["derive", "env"] }
rust-embed = "8.2"
mime_guess = "2.0"
shared = { path = "../shared" }
shared = { path = "../shared", features = ["ssr"] }
thiserror = "2.0.18"
dotenvy = "0.15.7"
utoipa = { version = "5.4.0", features = ["axum_extras"] }
utoipa-swagger-ui = { version = "9.0.2", features = ["axum"] }
utoipa-swagger-ui = { version = "9.0", features = ["axum"], optional = true }
web-push = { version = "0.10", default-features = false, features = ["hyper-client"], optional = true }
base64 = "0.22"
openssl = { version = "0.10", features = ["vendored"], optional = true }
bcrypt = "0.17.0"
axum-extra = { version = "0.10", features = ["cookie"] }
rand = "0.8"
anyhow = "1.0.101"
time = { version = "0.3.47", features = ["serde", "formatting", "parsing"] }
tower_governor = "0.8.0"
governor = "0.10.4"
# Leptos
leptos = { version = "0.8.15", features = ["nightly"] }
leptos_axum = { version = "0.8.7" }
jsonwebtoken = "9"

View File

@@ -1,3 +1,4 @@
use std::collections::HashMap;
use shared::{AppEvent, NotificationLevel, SystemNotification, Torrent, TorrentUpdate};
#[derive(Debug)]
@@ -8,99 +9,54 @@ pub enum DiffResult {
}
pub fn diff_torrents(old: &[Torrent], new: &[Torrent]) -> DiffResult {
// 1. Structural Check (Length or Order changed)
if old.len() != new.len() {
return DiffResult::FullUpdate;
}
for (i, t) in new.iter().enumerate() {
if old[i].hash != t.hash {
let old_map: HashMap<&str, &Torrent> = old.iter().map(|t| (t.hash.as_str(), t)).collect();
for new_t in new {
if !old_map.contains_key(new_t.hash.as_str()) {
return DiffResult::FullUpdate;
}
}
// 2. Field Updates
let mut events = Vec::new();
for (i, new_t) in new.iter().enumerate() {
let old_t = &old[i];
// Initialize with all None
let mut update = TorrentUpdate {
hash: new_t.hash.clone(),
name: None,
size: None,
down_rate: None,
up_rate: None,
percent_complete: None,
completed: None,
eta: None,
status: None,
error_message: None,
label: None,
};
for new_t in new {
let old_t = old_map.get(new_t.hash.as_str()).unwrap();
// Manuel diff creating TorrentUpdate (which is the Patch struct)
let mut patch = TorrentUpdate::default();
let mut has_changes = false;
// Compare fields
if old_t.name != new_t.name {
update.name = Some(new_t.name.clone());
has_changes = true;
}
if old_t.size != new_t.size {
update.size = Some(new_t.size);
has_changes = true;
}
if old_t.down_rate != new_t.down_rate {
update.down_rate = Some(new_t.down_rate);
has_changes = true;
}
if old_t.up_rate != new_t.up_rate {
update.up_rate = Some(new_t.up_rate);
has_changes = true;
}
if (old_t.percent_complete - new_t.percent_complete).abs() > 0.01 {
update.percent_complete = Some(new_t.percent_complete);
has_changes = true;
// Check for torrent completion: reached 100%
if old_t.name != new_t.name { patch.name = Some(new_t.name.clone()); has_changes = true; }
if old_t.size != new_t.size { patch.size = Some(new_t.size); has_changes = true; }
if old_t.down_rate != new_t.down_rate { patch.down_rate = Some(new_t.down_rate); has_changes = true; }
if old_t.up_rate != new_t.up_rate { patch.up_rate = Some(new_t.up_rate); has_changes = true; }
if old_t.completed != new_t.completed { patch.completed = Some(new_t.completed); has_changes = true; }
if old_t.eta != new_t.eta { patch.eta = Some(new_t.eta); has_changes = true; }
if (old_t.percent_complete - new_t.percent_complete).abs() > 0.01 {
patch.percent_complete = Some(new_t.percent_complete);
has_changes = true;
if old_t.percent_complete < 100.0 && new_t.percent_complete >= 100.0 {
tracing::info!("Torrent completed: {} ({})", new_t.name, new_t.hash);
events.push(AppEvent::Notification(SystemNotification {
level: NotificationLevel::Success,
message: format!("Torrent tamamlandı: {}", new_t.name),
}));
}
}
if old_t.completed != new_t.completed {
update.completed = Some(new_t.completed);
has_changes = true;
}
if old_t.eta != new_t.eta {
update.eta = Some(new_t.eta);
has_changes = true;
}
if old_t.status != new_t.status {
update.status = Some(new_t.status.clone());
has_changes = true;
// Log status changes for debugging
tracing::info!(
"Torrent status changed: {} ({}) {:?} -> {:?}",
new_t.name, new_t.hash, old_t.status, new_t.status
);
}
if old_t.error_message != new_t.error_message {
update.error_message = Some(new_t.error_message.clone());
has_changes = true;
}
if old_t.label != new_t.label {
update.label = new_t.label.clone();
has_changes = true;
}
if old_t.status != new_t.status { patch.status = Some(new_t.status.clone()); has_changes = true; }
if old_t.error_message != new_t.error_message { patch.error_message = Some(new_t.error_message.clone()); has_changes = true; }
if old_t.label != new_t.label { patch.label = Some(new_t.label.clone()); has_changes = true; }
if has_changes {
events.push(AppEvent::Update(update));
// Set the hash (not an Option in Patch usually, but check shared/src/lib.rs)
// Wait, TorrentUpdate is a Patch, does it have 'hash' field?
// Yes, because Torrent has 'hash' field.
patch.hash = Some(new_t.hash.clone());
events.push(AppEvent::Update(patch));
}
}
@@ -110,4 +66,4 @@ pub fn diff_torrents(old: &[Torrent], new: &[Torrent]) -> DiffResult {
tracing::debug!("Generated {} partial updates", events.len());
DiffResult::Partial(events)
}
}
}

View File

@@ -0,0 +1,154 @@
use crate::AppState;
use axum::{
extract::{State, Json},
http::StatusCode,
response::IntoResponse,
};
use serde::{Deserialize, Serialize};
use utoipa::ToSchema;
use axum_extra::extract::cookie::{Cookie, CookieJar, SameSite};
use time::Duration;
#[derive(Deserialize, ToSchema)]
pub struct LoginRequest {
username: String,
password: String,
#[serde(default)]
remember_me: bool,
}
#[derive(Serialize, ToSchema)]
pub struct UserResponse {
username: String,
}
#[utoipa::path(
post,
path = "/api/auth/login",
request_body = LoginRequest,
responses(
(status = 200, description = "Login successful"),
(status = 401, description = "Invalid credentials"),
(status = 500, description = "Internal server error")
)
)]
pub async fn login_handler(
State(state): State<AppState>,
jar: CookieJar,
Json(payload): Json<LoginRequest>,
) -> impl IntoResponse {
tracing::info!("Login attempt for user: {}", payload.username);
let user = match state.db.get_user_by_username(&payload.username).await {
Ok(Some(u)) => u,
Ok(None) => {
tracing::warn!("Login failed: User not found for {}", payload.username);
return (StatusCode::UNAUTHORIZED, "Invalid credentials").into_response();
}
Err(e) => {
tracing::error!("DB error during login for {}: {}", payload.username, e);
return (StatusCode::INTERNAL_SERVER_ERROR, "Database error").into_response();
}
};
let (user_id, password_hash) = user;
match bcrypt::verify(&payload.password, &password_hash) {
Ok(true) => {
tracing::info!("Password verified for user: {}", payload.username);
// Create session
let token: String = (0..32).map(|_| {
use rand::{distributions::Alphanumeric, Rng};
rand::thread_rng().sample(Alphanumeric) as char
}).collect();
// Expiration: 30 days if remember_me is true, else 1 day
let expires_in = if payload.remember_me {
60 * 60 * 24 * 30
} else {
60 * 60 * 24
};
let expires_at = time::OffsetDateTime::now_utc().unix_timestamp() + expires_in;
if let Err(e) = state.db.create_session(user_id, &token, expires_at).await {
tracing::error!("Failed to create session for {}: {}", payload.username, e);
return (StatusCode::INTERNAL_SERVER_ERROR, "Failed to create session").into_response();
}
let mut cookie = Cookie::build(("auth_token", token))
.path("/")
.http_only(true)
.same_site(SameSite::Lax)
.build();
cookie.set_max_age(Duration::seconds(expires_in));
tracing::info!("Session created and cookie set for user: {}", payload.username);
(StatusCode::OK, jar.add(cookie), Json(UserResponse { username: payload.username })).into_response()
}
Ok(false) => {
tracing::warn!("Login failed: Invalid password for {}", payload.username);
(StatusCode::UNAUTHORIZED, "Invalid credentials").into_response()
}
Err(e) => {
tracing::error!("Bcrypt error for {}: {}", payload.username, e);
(StatusCode::INTERNAL_SERVER_ERROR, "Auth error").into_response()
}
}
}
#[utoipa::path(
post,
path = "/api/auth/logout",
responses(
(status = 200, description = "Logged out")
)
)]
pub async fn logout_handler(
State(state): State<AppState>,
jar: CookieJar,
) -> impl IntoResponse {
if let Some(token) = jar.get("auth_token") {
let _ = state.db.delete_session(token.value()).await;
}
let cookie = Cookie::build(("auth_token", ""))
.path("/")
.http_only(true)
.max_age(Duration::seconds(-1)) // Expire immediately
.build();
(StatusCode::OK, jar.add(cookie), "Logged out").into_response()
}
#[utoipa::path(
get,
path = "/api/auth/check",
responses(
(status = 200, description = "Authenticated", body = UserResponse),
(status = 401, description = "Not authenticated")
)
)]
pub async fn check_auth_handler(
State(state): State<AppState>,
jar: CookieJar,
) -> impl IntoResponse {
if let Some(token) = jar.get("auth_token") {
match state.db.get_session_user(token.value()).await {
Ok(Some(user_id)) => {
// Fetch username
// We need a helper in db.rs to get username by id, or we can use a direct query here if we don't want to change db.rs interface yet.
// But better to add `get_username_by_id` to db.rs
// For now let's query directly or via a new db method.
if let Ok(Some(username)) = state.db.get_username_by_id(user_id).await {
return (StatusCode::OK, Json(UserResponse { username })).into_response();
}
},
_ => {} // Invalid session
}
}
StatusCode::UNAUTHORIZED.into_response()
}

View File

@@ -1,34 +1,17 @@
use crate::{
xmlrpc::{self, RpcParam},
AppState,
};
#[cfg(feature = "push-notifications")]
use crate::push;
use axum::{
extract::{Json, Path, State},
http::{header, StatusCode, Uri},
response::IntoResponse,
BoxError,
};
use rust_embed::RustEmbed;
use serde::Deserialize;
use shared::{
GlobalLimitRequest, SetFilePriorityRequest, SetLabelRequest, TorrentActionRequest, TorrentFile,
TorrentPeer, TorrentTracker,
};
use utoipa::ToSchema;
pub mod auth;
pub mod setup;
#[derive(RustEmbed)]
#[folder = "../frontend/dist"]
pub struct Asset;
#[derive(Deserialize, ToSchema)]
pub struct AddTorrentRequest {
/// Magnet link or Torrent file URL
#[schema(example = "magnet:?xt=urn:btih:...")]
uri: String,
}
pub async fn static_handler(uri: Uri) -> impl IntoResponse {
let mut path = uri.path().trim_start_matches('/').to_string();
@@ -45,7 +28,6 @@ pub async fn static_handler(uri: Uri) -> impl IntoResponse {
if path.contains('.') {
return StatusCode::NOT_FOUND.into_response();
}
// Fallback to index.html for SPA routing
match Asset::get("index.html") {
Some(content) => {
let mime = mime_guess::from_path("index.html").first_or_octet_stream();
@@ -57,614 +39,6 @@ pub async fn static_handler(uri: Uri) -> impl IntoResponse {
}
}
// --- TORRENT ACTIONS ---
/// Add a new torrent via magnet link or URL
#[utoipa::path(
post,
path = "/api/torrents/add",
request_body = AddTorrentRequest,
responses(
(status = 200, description = "Torrent added successfully"),
(status = 500, description = "Internal server error or rTorrent fault")
)
)]
pub async fn add_torrent_handler(
State(state): State<AppState>,
Json(payload): Json<AddTorrentRequest>,
) -> StatusCode {
tracing::info!(
"Received add_torrent request. URI length: {}",
payload.uri.len()
);
let client = xmlrpc::RtorrentClient::new(&state.scgi_socket_path);
let params = vec![RpcParam::from(""), RpcParam::from(payload.uri.as_str())];
match client.call("load.start", &params).await {
Ok(response) => {
tracing::debug!("rTorrent response to load.start: {}", response);
if response.contains("faultCode") {
tracing::error!("rTorrent returned fault: {}", response);
return StatusCode::INTERNAL_SERVER_ERROR;
}
// Note: Frontend shows its own toast, no SSE notification needed
StatusCode::OK
}
Err(e) => {
tracing::error!("Failed to add torrent: {}", e);
// Note: Frontend shows its own toast, no SSE notification needed
StatusCode::INTERNAL_SERVER_ERROR
}
}
}
/// Perform an action on a torrent (start, stop, delete)
#[utoipa::path(
post,
path = "/api/torrents/action",
request_body = TorrentActionRequest,
responses(
(status = 200, description = "Action executed successfully"),
(status = 400, description = "Invalid action or request"),
(status = 403, description = "Forbidden: Security risk detected"),
(status = 500, description = "Internal server error")
)
)]
pub async fn handle_torrent_action(
State(state): State<AppState>,
Json(payload): Json<TorrentActionRequest>,
) -> impl IntoResponse {
tracing::info!(
"Received action: {} for hash: {}",
payload.action,
payload.hash
);
let client = xmlrpc::RtorrentClient::new(&state.scgi_socket_path);
// Special handling for delete_with_data
if payload.action == "delete_with_data" {
return match delete_torrent_with_data(&client, &payload.hash).await {
Ok(msg) => {
// Note: Frontend shows its own toast
(StatusCode::OK, msg).into_response()
}
Err((status, msg)) => (status, msg).into_response(),
};
}
let method = match payload.action.as_str() {
"start" => "d.start",
"stop" => "d.stop",
"delete" => "d.erase",
_ => return (StatusCode::BAD_REQUEST, "Invalid action").into_response(),
};
let params = vec![RpcParam::from(payload.hash.as_str())];
match client.call(method, &params).await {
Ok(_) => {
// Note: Frontend shows its own toast, no SSE notification needed
(StatusCode::OK, "Action executed").into_response()
}
Err(e) => {
tracing::error!("RPC error: {}", e);
(
StatusCode::INTERNAL_SERVER_ERROR,
"Failed to execute action",
)
.into_response()
}
}
}
/// Helper function to handle secure deletion of torrent data
async fn delete_torrent_with_data(
client: &xmlrpc::RtorrentClient,
hash: &str,
) -> Result<&'static str, (StatusCode, String)> {
let params_hash = vec![RpcParam::from(hash)];
// 1. Get Base Path
let path_xml = client
.call("d.base_path", &params_hash)
.await
.map_err(|e| {
(
StatusCode::INTERNAL_SERVER_ERROR,
format!("Failed to call rTorrent: {}", e),
)
})?;
let path = xmlrpc::parse_string_response(&path_xml).map_err(|e| {
(
StatusCode::INTERNAL_SERVER_ERROR,
format!("Failed to parse path: {}", e),
)
})?;
// 1.5 Get Default Download Directory (Sandbox Root)
let root_xml = client.call("directory.default", &[]).await.map_err(|e| {
(
StatusCode::INTERNAL_SERVER_ERROR,
format!("Failed to get valid download root: {}", e),
)
})?;
let root_path_str = xmlrpc::parse_string_response(&root_xml).map_err(|e| {
(
StatusCode::INTERNAL_SERVER_ERROR,
format!("Failed to parse root path: {}", e),
)
})?;
// Resolve Paths (Canonicalize) to prevent .. traversal and symlink attacks
let root_path = tokio::fs::canonicalize(std::path::Path::new(&root_path_str))
.await
.map_err(|e| {
(
StatusCode::INTERNAL_SERVER_ERROR,
format!("Invalid download root configuration (on server): {}", e),
)
})?;
// Check if target path exists before trying to resolve it
let target_path_raw = std::path::Path::new(&path);
if !tokio::fs::try_exists(target_path_raw)
.await
.unwrap_or(false)
{
tracing::warn!(
"Data path not found: {:?}. Removing torrent only.",
target_path_raw
);
// If file doesn't exist, we just remove the torrent entry
client.call("d.erase", &params_hash).await.map_err(|e| {
(
StatusCode::INTERNAL_SERVER_ERROR,
format!("Failed to erase torrent: {}", e),
)
})?;
return Ok("Torrent removed (Data not found)");
}
let target_path = tokio::fs::canonicalize(target_path_raw)
.await
.map_err(|e| {
(
StatusCode::INTERNAL_SERVER_ERROR,
format!("Invalid data path: {}", e),
)
})?;
tracing::info!(
"Delete request: Target='{:?}', Root='{:?}'",
target_path,
root_path
);
// SECURITY CHECK: Ensure path is inside root_path
if !target_path.starts_with(&root_path) {
tracing::error!(
"Security Risk: Attempted to delete path outside download directory: {:?}",
target_path
);
return Err((
StatusCode::FORBIDDEN,
"Security Error: Cannot delete files outside default download directory".to_string(),
));
}
// SECURITY CHECK: Ensure we are not deleting the root itself
if target_path == root_path {
return Err((
StatusCode::BAD_REQUEST,
"Security Error: Cannot delete the download root directory itself".to_string(),
));
}
// 2. Erase Torrent first
client.call("d.erase", &params_hash).await.map_err(|e| {
tracing::warn!("Failed to erase torrent entry: {}", e);
(
StatusCode::INTERNAL_SERVER_ERROR,
format!("Failed to erase torrent: {}", e),
)
})?;
// 3. Delete Files via Native FS
let delete_result = if target_path.is_dir() {
tokio::fs::remove_dir_all(&target_path).await
} else {
tokio::fs::remove_file(&target_path).await
};
match delete_result {
Ok(_) => Ok("Torrent and data deleted"),
Err(e) => {
tracing::error!("Failed to delete data at {:?}: {}", target_path, e);
Err((
StatusCode::INTERNAL_SERVER_ERROR,
format!("Failed to delete data: {}", e),
))
}
}
}
// --- NEW HANDLERS ---
/// Get rTorrent version
#[utoipa::path(
get,
path = "/api/system/version",
responses(
(status = 200, description = "rTorrent version", body = String),
(status = 500, description = "Internal server error")
)
)]
pub async fn get_version_handler(State(state): State<AppState>) -> impl IntoResponse {
let client = xmlrpc::RtorrentClient::new(&state.scgi_socket_path);
match client.call("system.client_version", &[]).await {
Ok(xml) => {
let version = xmlrpc::parse_string_response(&xml).unwrap_or(xml);
(StatusCode::OK, version).into_response()
}
Err(e) => {
tracing::error!("Failed to get version: {}", e);
(StatusCode::INTERNAL_SERVER_ERROR, "Failed to get version").into_response()
}
}
}
/// Get files for a torrent
#[utoipa::path(
get,
path = "/api/torrents/{hash}/files",
responses(
(status = 200, description = "Files list", body = Vec<TorrentFile>),
(status = 500, description = "Internal server error")
),
params(
("hash" = String, Path, description = "Torrent Hash")
)
)]
pub async fn get_files_handler(
State(state): State<AppState>,
Path(hash): Path<String>,
) -> impl IntoResponse {
let client = xmlrpc::RtorrentClient::new(&state.scgi_socket_path);
let params = vec![
RpcParam::from(hash.as_str()),
RpcParam::from(""),
RpcParam::from("f.path="),
RpcParam::from("f.size_bytes="),
RpcParam::from("f.completed_chunks="),
RpcParam::from("f.priority="),
];
match client.call("f.multicall", &params).await {
Ok(xml) => match xmlrpc::parse_multicall_response(&xml) {
Ok(rows) => {
let files: Vec<TorrentFile> = rows
.into_iter()
.enumerate()
.map(|(idx, row)| TorrentFile {
index: idx as u32,
path: row.get(0).cloned().unwrap_or_default(),
size: row.get(1).and_then(|s| s.parse().ok()).unwrap_or(0),
completed_chunks: row.get(2).and_then(|s| s.parse().ok()).unwrap_or(0),
priority: row.get(3).and_then(|s| s.parse().ok()).unwrap_or(0),
})
.collect();
(StatusCode::OK, Json(files)).into_response()
}
Err(e) => (
StatusCode::INTERNAL_SERVER_ERROR,
format!("Parse error: {}", e),
)
.into_response(),
},
Err(e) => (
StatusCode::INTERNAL_SERVER_ERROR,
format!("RPC error: {}", e),
)
.into_response(),
}
}
/// Get peers for a torrent
#[utoipa::path(
get,
path = "/api/torrents/{hash}/peers",
responses(
(status = 200, description = "Peers list", body = Vec<TorrentPeer>),
(status = 500, description = "Internal server error")
),
params(
("hash" = String, Path, description = "Torrent Hash")
)
)]
pub async fn get_peers_handler(
State(state): State<AppState>,
Path(hash): Path<String>,
) -> impl IntoResponse {
let client = xmlrpc::RtorrentClient::new(&state.scgi_socket_path);
let params = vec![
RpcParam::from(hash.as_str()),
RpcParam::from(""),
RpcParam::from("p.address="),
RpcParam::from("p.client_version="),
RpcParam::from("p.down_rate="),
RpcParam::from("p.up_rate="),
RpcParam::from("p.completed_percent="),
];
match client.call("p.multicall", &params).await {
Ok(xml) => match xmlrpc::parse_multicall_response(&xml) {
Ok(rows) => {
let peers: Vec<TorrentPeer> = rows
.into_iter()
.map(|row| TorrentPeer {
ip: row.get(0).cloned().unwrap_or_default(),
client: row.get(1).cloned().unwrap_or_default(),
down_rate: row.get(2).and_then(|s| s.parse().ok()).unwrap_or(0),
up_rate: row.get(3).and_then(|s| s.parse().ok()).unwrap_or(0),
progress: row.get(4).and_then(|s| s.parse().ok()).unwrap_or(0.0),
})
.collect();
(StatusCode::OK, Json(peers)).into_response()
}
Err(e) => (
StatusCode::INTERNAL_SERVER_ERROR,
format!("Parse error: {}", e),
)
.into_response(),
},
Err(e) => (
StatusCode::INTERNAL_SERVER_ERROR,
format!("RPC error: {}", e),
)
.into_response(),
}
}
/// Get trackers for a torrent
#[utoipa::path(
get,
path = "/api/torrents/{hash}/trackers",
responses(
(status = 200, description = "Trackers list", body = Vec<TorrentTracker>),
(status = 500, description = "Internal server error")
),
params(
("hash" = String, Path, description = "Torrent Hash")
)
)]
pub async fn get_trackers_handler(
State(state): State<AppState>,
Path(hash): Path<String>,
) -> impl IntoResponse {
let client = xmlrpc::RtorrentClient::new(&state.scgi_socket_path);
let params = vec![
RpcParam::from(hash.as_str()),
RpcParam::from(""),
RpcParam::from("t.url="),
RpcParam::from("t.activity_date_last="),
RpcParam::from("t.message="),
];
match client.call("t.multicall", &params).await {
Ok(xml) => {
match xmlrpc::parse_multicall_response(&xml) {
Ok(rows) => {
let trackers: Vec<TorrentTracker> = rows
.into_iter()
.map(|row| {
TorrentTracker {
url: row.get(0).cloned().unwrap_or_default(),
status: "Unknown".to_string(), // Derive from type/activity?
message: row.get(2).cloned().unwrap_or_default(),
}
})
.collect();
(StatusCode::OK, Json(trackers)).into_response()
}
Err(e) => (
StatusCode::INTERNAL_SERVER_ERROR,
format!("Parse error: {}", e),
)
.into_response(),
}
}
Err(e) => (
StatusCode::INTERNAL_SERVER_ERROR,
format!("RPC error: {}", e),
)
.into_response(),
}
}
/// Set file priority
#[utoipa::path(
post,
path = "/api/torrents/files/priority",
request_body = SetFilePriorityRequest,
responses(
(status = 200, description = "Priority updated"),
(status = 500, description = "Internal server error")
)
)]
pub async fn set_file_priority_handler(
State(state): State<AppState>,
Json(payload): Json<SetFilePriorityRequest>,
) -> impl IntoResponse {
let client = xmlrpc::RtorrentClient::new(&state.scgi_socket_path);
// f.set_priority takes "hash", index, priority
// Priority: 0 (off), 1 (normal), 2 (high)
// f.set_priority is tricky. Let's send as string first as before, or int if we knew.
// Usually priorities are small integers.
// But since we are updating everything to RpcParam, let's use Int if possible or String.
// The previous implementation used string. Let's stick to string for now or try Int.
// Actually, f.set_priority likely takes an integer.
let target = format!("{}:f{}", payload.hash, payload.file_index);
let params = vec![
RpcParam::from(target.as_str()),
RpcParam::from(payload.priority as i64),
];
match client.call("f.set_priority", &params).await {
Ok(_) => {
let _ = client
.call(
"d.update_priorities",
&[RpcParam::from(payload.hash.as_str())],
)
.await;
(StatusCode::OK, "Priority updated").into_response()
}
Err(e) => (
StatusCode::INTERNAL_SERVER_ERROR,
format!("RPC error: {}", e),
)
.into_response(),
}
}
/// Set torrent label
#[utoipa::path(
post,
path = "/api/torrents/label",
request_body = SetLabelRequest,
responses(
(status = 200, description = "Label updated"),
(status = 500, description = "Internal server error")
)
)]
pub async fn set_label_handler(
State(state): State<AppState>,
Json(payload): Json<SetLabelRequest>,
) -> impl IntoResponse {
let client = xmlrpc::RtorrentClient::new(&state.scgi_socket_path);
let params = vec![
RpcParam::from(payload.hash.as_str()),
RpcParam::from(payload.label),
];
match client.call("d.custom1.set", &params).await {
Ok(_) => (StatusCode::OK, "Label updated").into_response(),
Err(e) => (
StatusCode::INTERNAL_SERVER_ERROR,
format!("RPC error: {}", e),
)
.into_response(),
}
}
/// Get global speed limits
#[utoipa::path(
get,
path = "/api/settings/global-limits",
responses(
(status = 200, description = "Current limits", body = GlobalLimitRequest),
(status = 500, description = "Internal server error")
)
)]
pub async fn get_global_limit_handler(State(state): State<AppState>) -> impl IntoResponse {
let client = xmlrpc::RtorrentClient::new(&state.scgi_socket_path);
// throttle.global_down.max_rate, throttle.global_up.max_rate
let down_fut = client.call("throttle.global_down.max_rate", &[]);
let up_fut = client.call("throttle.global_up.max_rate", &[]);
let down = match down_fut.await {
Ok(xml) => xmlrpc::parse_i64_response(&xml).unwrap_or(0),
Err(_) => -1,
};
let up = match up_fut.await {
Ok(xml) => xmlrpc::parse_i64_response(&xml).unwrap_or(0),
Err(_) => -1,
};
let resp = GlobalLimitRequest {
max_download_rate: Some(down),
max_upload_rate: Some(up),
};
(StatusCode::OK, Json(resp)).into_response()
}
/// Set global speed limits
#[utoipa::path(
post,
path = "/api/settings/global-limits",
request_body = GlobalLimitRequest,
responses(
(status = 200, description = "Limits updated"),
(status = 500, description = "Internal server error")
)
)]
pub async fn set_global_limit_handler(
State(state): State<AppState>,
Json(payload): Json<GlobalLimitRequest>,
) -> impl IntoResponse {
let client = xmlrpc::RtorrentClient::new(&state.scgi_socket_path);
// Use throttle.global_*.max_rate.set_kb which is more reliable than .set (which is buggy)
// The .set_kb method expects KB/s, so we convert bytes to KB
if let Some(down) = payload.max_download_rate {
// Convert bytes/s to KB/s (divide by 1024)
let down_kb = down / 1024;
tracing::info!(
"Setting download limit: {} bytes/s = {} KB/s",
down,
down_kb
);
// Use set_kb with empty string as first param (throttle name), then value
if let Err(e) = client
.call(
"throttle.global_down.max_rate.set_kb",
&[RpcParam::from(""), RpcParam::Int(down_kb)],
)
.await
{
tracing::error!("Failed to set download limit: {}", e);
return (
StatusCode::INTERNAL_SERVER_ERROR,
format!("Failed to set down limit: {}", e),
)
.into_response();
}
}
if let Some(up) = payload.max_upload_rate {
// Convert bytes/s to KB/s
let up_kb = up / 1024;
tracing::info!("Setting upload limit: {} bytes/s = {} KB/s", up, up_kb);
if let Err(e) = client
.call(
"throttle.global_up.max_rate.set_kb",
&[RpcParam::from(""), RpcParam::Int(up_kb)],
)
.await
{
tracing::error!("Failed to set upload limit: {}", e);
return (
StatusCode::INTERNAL_SERVER_ERROR,
format!("Failed to set up limit: {}", e),
)
.into_response();
}
}
(StatusCode::OK, "Limits updated").into_response()
}
pub async fn handle_timeout_error(err: BoxError) -> (StatusCode, &'static str) {
if err.is::<tower::timeout::error::Elapsed>() {
(StatusCode::REQUEST_TIMEOUT, "Request timed out")
@@ -675,42 +49,3 @@ pub async fn handle_timeout_error(err: BoxError) -> (StatusCode, &'static str) {
)
}
}
// --- PUSH NOTIFICATION HANDLERS ---
#[cfg(feature = "push-notifications")]
/// Get VAPID public key for push subscription
#[utoipa::path(
get,
path = "/api/push/public-key",
responses(
(status = 200, description = "VAPID public key", body = String)
)
)]
pub async fn get_push_public_key_handler() -> impl IntoResponse {
let public_key = push::get_vapid_public_key();
(StatusCode::OK, Json(serde_json::json!({ "publicKey": public_key }))).into_response()
}
#[cfg(feature = "push-notifications")]
/// Subscribe to push notifications
#[utoipa::path(
post,
path = "/api/push/subscribe",
request_body = push::PushSubscription,
responses(
(status = 200, description = "Subscription saved"),
(status = 400, description = "Invalid subscription data")
)
)]
pub async fn subscribe_push_handler(
State(state): State<AppState>,
Json(subscription): Json<push::PushSubscription>,
) -> impl IntoResponse {
tracing::info!("Received push subscription: {:?}", subscription);
state.push_store.add_subscription(subscription).await;
(StatusCode::OK, "Subscription saved").into_response()
}

View File

@@ -0,0 +1,125 @@
use crate::AppState;
use axum::{
extract::{State, Json},
http::StatusCode,
response::IntoResponse,
};
use serde::{Deserialize, Serialize};
use utoipa::ToSchema;
use axum_extra::extract::cookie::{Cookie, CookieJar, SameSite};
use time::Duration;
#[derive(Deserialize, ToSchema)]
pub struct SetupRequest {
username: String,
password: String,
}
#[derive(Serialize, ToSchema)]
pub struct SetupStatusResponse {
completed: bool,
}
#[utoipa::path(
get,
path = "/api/setup/status",
responses(
(status = 200, description = "Setup status", body = SetupStatusResponse)
)
)]
pub async fn get_setup_status_handler(State(state): State<AppState>) -> impl IntoResponse {
let completed = match state.db.has_users().await {
Ok(has) => has,
Err(e) => {
tracing::error!("DB error checking users: {}", e);
false
}
};
Json(SetupStatusResponse { completed }).into_response()
}
#[utoipa::path(
post,
path = "/api/setup",
request_body = SetupRequest,
responses(
(status = 200, description = "Setup completed and logged in"),
(status = 400, description = "Invalid request"),
(status = 403, description = "Setup already completed"),
(status = 500, description = "Internal server error")
)
)]
pub async fn setup_handler(
State(state): State<AppState>,
jar: CookieJar,
Json(payload): Json<SetupRequest>,
) -> impl IntoResponse {
// 1. Check if setup is already completed (i.e., users exist)
match state.db.has_users().await {
Ok(true) => return (StatusCode::FORBIDDEN, "Setup already completed").into_response(),
Err(e) => {
tracing::error!("DB error checking users: {}", e);
return (StatusCode::INTERNAL_SERVER_ERROR, "Database error").into_response();
}
Ok(false) => {} // Proceed
}
// 2. Validate input
if payload.username.len() < 3 || payload.password.len() < 6 {
return (StatusCode::BAD_REQUEST, "Username must be at least 3 chars, password at least 6").into_response();
}
// 3. Create User
// Lower cost for faster login on low-power devices (MIPS routers etc.)
let password_hash = match bcrypt::hash(&payload.password, 6) {
Ok(h) => h,
Err(e) => {
tracing::error!("Failed to hash password: {}", e);
return (StatusCode::INTERNAL_SERVER_ERROR, "Failed to process password").into_response();
}
};
if let Err(e) = state.db.create_user(&payload.username, &password_hash).await {
tracing::error!("Failed to create user: {}", e);
return (StatusCode::INTERNAL_SERVER_ERROR, "Failed to create user").into_response();
}
// 4. Auto-Login (Create Session)
// Get the created user's ID
let user = match state.db.get_user_by_username(&payload.username).await {
Ok(Some(u)) => u,
Ok(None) => return (StatusCode::INTERNAL_SERVER_ERROR, "User created but not found").into_response(),
Err(e) => {
tracing::error!("DB error fetching new user: {}", e);
return (StatusCode::INTERNAL_SERVER_ERROR, "Database error").into_response();
}
};
let (user_id, _) = user;
// Create session token
let token: String = (0..32).map(|_| {
use rand::{distributions::Alphanumeric, Rng};
rand::thread_rng().sample(Alphanumeric) as char
}).collect();
// Default expiration: 1 day (since it's not "remember me")
let expires_in = 60 * 60 * 24;
let expires_at = time::OffsetDateTime::now_utc().unix_timestamp() + expires_in;
if let Err(e) = state.db.create_session(user_id, &token, expires_at).await {
tracing::error!("Failed to create session for new user: {}", e);
// Even if session fails, setup is technically complete, but login failed.
// We return OK but user will have to login manually.
return (StatusCode::OK, "Setup completed, please login").into_response();
}
let mut cookie = Cookie::build(("auth_token", token))
.path("/")
.http_only(true)
.same_site(SameSite::Lax)
.build();
cookie.set_max_age(Duration::seconds(expires_in));
(StatusCode::OK, jar.add(cookie), "Setup completed and logged in").into_response()
}

View File

@@ -2,15 +2,21 @@ mod diff;
mod handlers;
#[cfg(feature = "push-notifications")]
mod push;
mod scgi;
mod rate_limit;
mod sse;
mod xmlrpc;
use shared::xmlrpc;
use axum::error_handling::HandleErrorLayer;
use axum::{
routing::{get, post},
Router,
middleware::{self, Next},
response::Response,
http::{StatusCode, Request},
body::Body,
};
use axum_extra::extract::cookie::CookieJar;
use clap::Parser;
use dotenvy::dotenv;
use shared::{AppEvent, Torrent};
@@ -19,12 +25,15 @@ use std::sync::Arc;
use std::time::Duration;
use tokio::sync::{broadcast, watch};
use tower::ServiceBuilder;
use tower_governor::GovernorLayer;
use tower_http::{
compression::{CompressionLayer, CompressionLevel},
cors::CorsLayer,
trace::TraceLayer,
};
#[cfg(feature = "swagger")]
use utoipa::OpenApi;
#[cfg(feature = "swagger")]
use utoipa_swagger_ui::SwaggerUi;
#[derive(Clone)]
@@ -32,10 +41,50 @@ pub struct AppState {
pub tx: Arc<watch::Sender<Vec<Torrent>>>,
pub event_bus: broadcast::Sender<AppEvent>,
pub scgi_socket_path: String,
pub db: shared::db::Db,
#[cfg(feature = "push-notifications")]
pub push_store: push::PushSubscriptionStore,
pub notify_poll: Arc<tokio::sync::Notify>,
}
async fn auth_middleware(
state: axum::extract::State<AppState>,
jar: CookieJar,
request: Request<Body>,
next: Next,
) -> Result<Response, StatusCode> {
// Skip auth for public paths
let path = request.uri().path();
if path.starts_with("/api/server_fns/Login") // Login server fn
|| path.starts_with("/api/server_fns/GetSetupStatus")
|| path.starts_with("/api/server_fns/Setup")
|| path.starts_with("/swagger-ui")
|| path.starts_with("/api-docs")
|| !path.starts_with("/api/") // Allow static files (frontend)
{
return Ok(next.run(request).await);
}
// Check token
if let Some(token) = jar.get("auth_token") {
use jsonwebtoken::{decode, Validation, DecodingKey};
use shared::server_fns::auth::Claims;
let secret = std::env::var("JWT_SECRET").unwrap_or_else(|_| "secret".to_string());
let validation = Validation::default();
match decode::<Claims>(
token.value(),
&DecodingKey::from_secret(secret.as_bytes()),
&validation,
) {
Ok(_) => return Ok(next.run(request).await),
Err(_) => {} // Invalid token
}
}
Err(StatusCode::UNAUTHORIZED)
}
#[derive(Parser, Debug)]
#[command(author, version, about, long_about = None)]
struct Args {
@@ -51,28 +100,29 @@ struct Args {
/// Port to listen on
#[arg(short, long, env = "PORT", default_value_t = 3000)]
port: u16,
/// Database URL
#[arg(long, env = "DATABASE_URL", default_value = "sqlite:vibetorrent.db")]
db_url: String,
/// Reset password for the specified user
#[arg(long)]
reset_password: Option<String>,
}
#[cfg(feature = "push-notifications")]
#[cfg(feature = "swagger")]
#[derive(OpenApi)]
#[openapi(
paths(
handlers::add_torrent_handler,
handlers::handle_torrent_action,
handlers::get_version_handler,
handlers::get_files_handler,
handlers::get_peers_handler,
handlers::get_trackers_handler,
handlers::set_file_priority_handler,
handlers::set_label_handler,
handlers::get_global_limit_handler,
handlers::set_global_limit_handler,
handlers::get_push_public_key_handler,
handlers::subscribe_push_handler
handlers::auth::login_handler,
handlers::auth::logout_handler,
handlers::auth::check_auth_handler,
handlers::setup::setup_handler,
handlers::setup::get_setup_status_handler
),
components(
schemas(
handlers::AddTorrentRequest,
shared::AddTorrentRequest,
shared::TorrentActionRequest,
shared::Torrent,
shared::TorrentStatus,
@@ -82,43 +132,10 @@ struct Args {
shared::SetFilePriorityRequest,
shared::SetLabelRequest,
shared::GlobalLimitRequest,
push::PushSubscription,
push::PushKeys
)
),
tags(
(name = "vibetorrent", description = "VibeTorrent API")
)
)]
struct ApiDoc;
#[cfg(not(feature = "push-notifications"))]
#[derive(OpenApi)]
#[openapi(
paths(
handlers::add_torrent_handler,
handlers::handle_torrent_action,
handlers::get_version_handler,
handlers::get_files_handler,
handlers::get_peers_handler,
handlers::get_trackers_handler,
handlers::set_file_priority_handler,
handlers::set_label_handler,
handlers::get_global_limit_handler,
handlers::set_global_limit_handler
),
components(
schemas(
handlers::AddTorrentRequest,
shared::TorrentActionRequest,
shared::Torrent,
shared::TorrentStatus,
shared::TorrentFile,
shared::TorrentPeer,
shared::TorrentTracker,
shared::SetFilePriorityRequest,
shared::SetLabelRequest,
shared::GlobalLimitRequest
handlers::auth::LoginRequest,
handlers::setup::SetupRequest,
handlers::setup::SetupStatusResponse,
handlers::auth::UserResponse
)
),
tags(
@@ -142,10 +159,80 @@ async fn main() {
// Parse CLI Args
let args = Args::parse();
// Initialize Database
tracing::info!("Connecting to database: {}", args.db_url);
// Redundant manual creation removed, shared::db handles it
let db: shared::db::Db = match shared::db::Db::new(&args.db_url).await {
Ok(db) => db,
Err(e) => {
tracing::error!("Failed to connect to database: {}", e);
std::process::exit(1);
}
};
tracing::info!("Database connected successfully.");
// Handle Password Reset
if let Some(username) = args.reset_password {
tracing::info!("Resetting password for user: {}", username);
// Check if user exists
let user_result = db.get_user_by_username(&username).await;
match user_result {
Ok(Some((user_id, _))) => {
// Generate random password
use rand::{distributions::Alphanumeric, Rng};
let new_password: String = rand::thread_rng()
.sample_iter(&Alphanumeric)
.take(12)
.map(char::from)
.collect();
// Hash password (low cost for performance)
let password_hash = match bcrypt::hash(&new_password, 6) {
Ok(h) => h,
Err(e) => {
tracing::error!("Failed to hash password: {}", e);
std::process::exit(1);
}
};
// Update in DB
if let Err(e) = db.update_password(user_id, &password_hash).await {
tracing::error!("Failed to update password in DB: {}", e);
std::process::exit(1);
}
println!("--------------------------------------------------");
println!("Password reset successfully for user: {}", username);
println!("New Password: {}", new_password);
println!("--------------------------------------------------");
// Invalidate existing sessions for security
if let Err(e) = db.delete_all_sessions_for_user(user_id).await {
tracing::warn!("Failed to invalidate existing sessions: {}", e);
}
std::process::exit(0);
},
Ok(None) => {
tracing::error!("User '{}' not found.", username);
std::process::exit(1);
},
Err(e) => {
tracing::error!("Database error: {}", e);
std::process::exit(1);
}
}
}
tracing::info!("Starting VibeTorrent Backend...");
tracing::info!("Socket: {}", args.socket);
tracing::info!("Port: {}", args.port);
// ... rest of the main function ...
// Startup Health Check
let socket_path = std::path::Path::new(&args.socket);
if !socket_path.exists() {
@@ -177,12 +264,28 @@ async fn main() {
// Channel for Events (Diffs)
let (event_bus, _) = broadcast::channel::<AppEvent>(1024);
#[cfg(feature = "push-notifications")]
let push_store = match push::PushSubscriptionStore::with_db(&db).await {
Ok(store) => store,
Err(e) => {
tracing::error!("Failed to initialize push store: {}", e);
push::PushSubscriptionStore::new()
}
};
#[cfg(not(feature = "push-notifications"))]
let _push_store = ();
let notify_poll = Arc::new(tokio::sync::Notify::new());
let app_state = AppState {
tx: tx.clone(),
event_bus: event_bus.clone(),
scgi_socket_path: args.socket.clone(),
db: db.clone(),
#[cfg(feature = "push-notifications")]
push_store: push::PushSubscriptionStore::new(),
push_store,
notify_poll: notify_poll.clone(),
};
// Spawn background task to poll rTorrent
@@ -191,6 +294,7 @@ async fn main() {
let socket_path = args.socket.clone(); // Clone for background task
#[cfg(feature = "push-notifications")]
let push_store_clone = app_state.push_store.clone();
let notify_poll_clone = notify_poll.clone();
tokio::spawn(async move {
let client = xmlrpc::RtorrentClient::new(&socket_path);
@@ -199,6 +303,14 @@ async fn main() {
let mut backoff_duration = Duration::from_secs(1);
loop {
// Determine polling interval based on active clients
let active_clients = event_bus_tx.receiver_count();
let loop_interval = if active_clients > 0 {
Duration::from_secs(1)
} else {
Duration::from_secs(30)
};
// 1. Fetch Torrents
let torrents_result = sse::fetch_torrents(&client).await;
@@ -269,6 +381,14 @@ async fn main() {
}
previous_torrents = new_torrents;
// Success case: wait for the determined interval OR a wakeup notification
tokio::select! {
_ = tokio::time::sleep(loop_interval) => {},
_ = notify_poll_clone.notified() => {
tracing::debug!("Background loop awakened by new client connection");
}
}
}
Err(e) => {
tracing::error!("Error fetching torrents in background: {}", e);
@@ -289,61 +409,51 @@ async fn main() {
"Backoff: Sleeping for {:?} due to rTorrent error.",
backoff_duration
);
tokio::time::sleep(backoff_duration).await;
}
}
// Handle Stats
match stats_result {
Ok(stats) => {
let _ = event_bus_tx.send(AppEvent::Stats(stats));
}
Err(e) => {
tracing::warn!("Error fetching global stats: {}", e);
}
if let Ok(stats) = stats_result {
let _ = event_bus_tx.send(AppEvent::Stats(stats));
}
tokio::time::sleep(backoff_duration).await;
}
});
let app = Router::new()
.merge(SwaggerUi::new("/swagger-ui").url("/api-docs/openapi.json", ApiDoc::openapi()))
let app = Router::new();
#[cfg(feature = "swagger")]
let app = app.merge(SwaggerUi::new("/swagger-ui").url("/api-docs/openapi.json", ApiDoc::openapi()));
// Setup & Auth Routes (cookie-based, stay as REST)
let scgi_path_for_ctx = args.socket.clone();
let db_for_ctx = db.clone();
let app = app
.route("/api/events", get(sse::sse_handler))
.route("/api/torrents/add", post(handlers::add_torrent_handler))
.route(
"/api/torrents/action",
post(handlers::handle_torrent_action),
)
.route("/api/system/version", get(handlers::get_version_handler))
.route(
"/api/torrents/{hash}/files",
get(handlers::get_files_handler),
)
.route(
"/api/torrents/{hash}/peers",
get(handlers::get_peers_handler),
)
.route(
"/api/torrents/{hash}/trackers",
get(handlers::get_trackers_handler),
)
.route(
"/api/torrents/files/priority",
post(handlers::set_file_priority_handler),
)
.route("/api/torrents/label", post(handlers::set_label_handler))
.route(
"/api/settings/global-limits",
get(handlers::get_global_limit_handler).post(handlers::set_global_limit_handler),
)
.fallback(handlers::static_handler); // Serve static files for everything else
#[cfg(feature = "push-notifications")]
let app = app
.route("/api/push/public-key", get(handlers::get_push_public_key_handler))
.route("/api/push/subscribe", post(handlers::subscribe_push_handler));
.route("/api/server_fns/{*fn_name}", post({
let scgi_path = scgi_path_for_ctx.clone();
let db = db_for_ctx.clone();
move |req: Request<Body>| {
let scgi_path = scgi_path.clone();
let db = db.clone();
leptos_axum::handle_server_fns_with_context(
move || {
leptos::context::provide_context(shared::ServerContext {
scgi_socket_path: scgi_path.clone(),
});
leptos::context::provide_context(shared::DbContext {
db: db.clone(),
});
},
req,
)
}
}))
.fallback(handlers::static_handler);
let app = app
.layer(middleware::from_fn_with_state(app_state.clone(), auth_middleware))
.layer(TraceLayer::new_for_http())
.layer(
CompressionLayer::new()
@@ -372,7 +482,12 @@ async fn main() {
}
};
tracing::info!("Backend listening on {}", addr);
if let Err(e) = axum::serve(listener, app).await {
if let Err(e) = axum::serve(
listener,
app.into_make_service_with_connect_info::<SocketAddr>(),
)
.await
{
tracing::error!("Server error: {}", e);
std::process::exit(1);
}

View File

@@ -5,11 +5,9 @@ use utoipa::ToSchema;
use web_push::{
HyperWebPushClient, SubscriptionInfo, VapidSignatureBuilder, WebPushClient, WebPushMessageBuilder,
};
use futures::StreamExt;
// VAPID keys - PRODUCTION'DA ENVIRONMENT VARIABLE'DAN ALINMALI!
const VAPID_PUBLIC_KEY: &str = "BEdPj6XQR7MGzM28Nev9wokF5upHoydNDahouJbQ9ZdBJpEFAN1iNfANSEvY0ItasNY5zcvvqN_tjUt64Rfd0gU";
const VAPID_PRIVATE_KEY: &str = "aUcCYJ7kUd9UClCaWwad0IVgbYJ6svwl19MjSX7GH10";
const VAPID_EMAIL: &str = "mailto:admin@vibetorrent.app";
use shared::db::Db;
#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
pub struct PushSubscription {
@@ -23,39 +21,107 @@ pub struct PushKeys {
pub auth: String,
}
/// In-memory store for push subscriptions
/// TODO: Replace with database in production
#[derive(Default, Clone)]
#[derive(Clone)]
pub struct VapidConfig {
pub private_key: String,
pub public_key: String,
pub email: String,
}
#[derive(Clone)]
pub struct PushSubscriptionStore {
db: Option<Db>,
subscriptions: Arc<RwLock<Vec<PushSubscription>>>,
vapid_config: VapidConfig,
}
impl PushSubscriptionStore {
pub fn new() -> Self {
let private_key = std::env::var("VAPID_PRIVATE_KEY").expect("VAPID_PRIVATE_KEY must be set in .env");
let public_key = std::env::var("VAPID_PUBLIC_KEY").expect("VAPID_PUBLIC_KEY must be set in .env");
let email = std::env::var("VAPID_EMAIL").expect("VAPID_EMAIL must be set in .env");
Self {
db: None,
subscriptions: Arc::new(RwLock::new(Vec::new())),
vapid_config: VapidConfig {
private_key,
public_key,
email,
},
}
}
pub async fn with_db(db: &Db) -> Result<Self, Box<dyn std::error::Error>> {
let mut subscriptions_vec: Vec<PushSubscription> = Vec::new();
// Load existing subscriptions from DB
let subs = db.get_all_push_subscriptions().await?;
for (endpoint, p256dh, auth) in subs {
subscriptions_vec.push(PushSubscription {
endpoint,
keys: PushKeys { p256dh, auth },
});
}
tracing::info!("Loaded {} push subscriptions from database", subscriptions_vec.len());
let private_key = std::env::var("VAPID_PRIVATE_KEY").expect("VAPID_PRIVATE_KEY must be set in .env");
let public_key = std::env::var("VAPID_PUBLIC_KEY").expect("VAPID_PUBLIC_KEY must be set in .env");
let email = std::env::var("VAPID_EMAIL").expect("VAPID_EMAIL must be set in .env");
Ok(Self {
db: Some(db.clone()),
subscriptions: Arc::new(RwLock::new(subscriptions_vec)),
vapid_config: VapidConfig {
private_key,
public_key,
email,
},
})
}
pub async fn add_subscription(&self, subscription: PushSubscription) {
// Add to memory
let mut subs = self.subscriptions.write().await;
// Remove duplicate endpoint if exists
subs.retain(|s| s.endpoint != subscription.endpoint);
subs.push(subscription);
subs.push(subscription.clone());
tracing::info!("Added push subscription. Total: {}", subs.len());
// Save to DB if available
if let Some(db) = &self.db {
if let Err(e) = db.save_push_subscription(
&subscription.endpoint,
&subscription.keys.p256dh,
&subscription.keys.auth,
).await {
tracing::error!("Failed to save push subscription to DB: {}", e);
}
}
}
pub async fn remove_subscription(&self, endpoint: &str) {
// Remove from memory
let mut subs = self.subscriptions.write().await;
subs.retain(|s| s.endpoint != endpoint);
tracing::info!("Removed push subscription. Total: {}", subs.len());
// Remove from DB if available
if let Some(db) = &self.db {
if let Err(e) = db.remove_push_subscription(endpoint).await {
tracing::error!("Failed to remove push subscription from DB: {}", e);
}
}
}
pub async fn get_all_subscriptions(&self) -> Vec<PushSubscription> {
self.subscriptions.read().await.clone()
}
pub fn get_public_key(&self) -> &str {
&self.vapid_config.public_key
}
}
/// Send push notification to all subscribed clients
@@ -65,7 +131,7 @@ pub async fn send_push_notification(
body: &str,
) -> Result<(), Box<dyn std::error::Error>> {
let subscriptions = store.get_all_subscriptions().await;
if subscriptions.is_empty() {
tracing::debug!("No push subscriptions to send to");
return Ok(());
@@ -81,47 +147,68 @@ pub async fn send_push_notification(
"tag": "vibetorrent"
});
let client = HyperWebPushClient::new();
let client = Arc::new(HyperWebPushClient::new());
let vapid_config = store.vapid_config.clone();
let payload_str = payload.to_string();
for subscription in subscriptions {
let subscription_info = SubscriptionInfo {
endpoint: subscription.endpoint.clone(),
keys: web_push::SubscriptionKeys {
p256dh: subscription.keys.p256dh.clone(),
auth: subscription.keys.auth.clone(),
},
};
// Send notifications concurrently
futures::stream::iter(subscriptions)
.for_each_concurrent(10, |subscription| {
let client = client.clone();
let vapid_config = vapid_config.clone();
let payload_str = payload_str.clone();
let mut sig_builder = VapidSignatureBuilder::from_base64(
VAPID_PRIVATE_KEY,
web_push::URL_SAFE_NO_PAD,
&subscription_info,
)?;
sig_builder.add_claim("sub", VAPID_EMAIL);
sig_builder.add_claim("aud", subscription.endpoint.clone());
let signature = sig_builder.build()?;
async move {
let subscription_info = SubscriptionInfo {
endpoint: subscription.endpoint.clone(),
keys: web_push::SubscriptionKeys {
p256dh: subscription.keys.p256dh.clone(),
auth: subscription.keys.auth.clone(),
},
};
let mut builder = WebPushMessageBuilder::new(&subscription_info);
builder.set_vapid_signature(signature);
let payload_str = payload.to_string();
builder.set_payload(web_push::ContentEncoding::Aes128Gcm, payload_str.as_bytes());
let sig_res = VapidSignatureBuilder::from_base64(
&vapid_config.private_key,
web_push::URL_SAFE_NO_PAD,
&subscription_info,
);
match client.send(builder.build()?).await {
Ok(_) => {
tracing::debug!("Push notification sent to: {}", subscription.endpoint);
match sig_res {
Ok(mut sig_builder) => {
sig_builder.add_claim("sub", vapid_config.email.as_str());
sig_builder.add_claim("aud", subscription.endpoint.as_str());
match sig_builder.build() {
Ok(signature) => {
let mut builder = WebPushMessageBuilder::new(&subscription_info);
builder.set_vapid_signature(signature);
builder.set_payload(web_push::ContentEncoding::Aes128Gcm, payload_str.as_bytes());
match builder.build() {
Ok(msg) => {
match client.send(msg).await {
Ok(_) => {
tracing::debug!("Push notification sent to: {}", subscription.endpoint);
}
Err(e) => {
tracing::error!("Failed to send push notification to {}: {}", subscription.endpoint, e);
}
}
}
Err(e) => tracing::error!("Failed to build push message: {}", e),
}
}
Err(e) => tracing::error!("Failed to build VAPID signature: {}", e),
}
}
Err(e) => tracing::error!("Failed to create VAPID signature builder: {}", e),
}
}
Err(e) => {
tracing::error!("Failed to send push notification: {}", e);
// TODO: Remove invalid subscriptions
}
}
})
.await;
Ok(())
}
Ok(())
}
pub fn get_vapid_public_key() -> &'static str {
VAPID_PUBLIC_KEY
}

16
backend/src/rate_limit.rs Normal file
View File

@@ -0,0 +1,16 @@
use governor::clock::QuantaInstant;
use governor::middleware::NoOpMiddleware;
use tower_governor::governor::GovernorConfig;
use tower_governor::governor::GovernorConfigBuilder;
use tower_governor::key_extractor::SmartIpKeyExtractor;
pub fn get_login_rate_limit_config() -> GovernorConfig<SmartIpKeyExtractor, NoOpMiddleware<QuantaInstant>> {
// 5 yanlış denemeden sonra bloklanır.
// Her yeni hak için 60 saniye (1 dakika) bekleme süresi.
GovernorConfigBuilder::default()
.key_extractor(SmartIpKeyExtractor)
.per_second(60)
.burst_size(5)
.finish()
.unwrap()
}

View File

@@ -1,4 +1,4 @@
use crate::xmlrpc::{
use shared::xmlrpc::{
parse_i64_response, parse_multicall_response, RpcParam, RtorrentClient, XmlRpcError,
};
use crate::AppState;
@@ -8,6 +8,8 @@ use futures::stream::{self, Stream};
use shared::{AppEvent, GlobalStats, Torrent, TorrentStatus};
use std::convert::Infallible;
use tokio_stream::StreamExt;
use axum::response::IntoResponse;
use base64::{Engine as _, engine::general_purpose::STANDARD as BASE64};
// Field definitions to keep query and parser in sync
mod fields {
@@ -194,7 +196,10 @@ pub async fn fetch_global_stats(client: &RtorrentClient) -> Result<GlobalStats,
pub async fn sse_handler(
State(state): State<AppState>,
) -> Sse<impl Stream<Item = Result<Event, Infallible>>> {
) -> impl IntoResponse {
// Notify background worker to wake up and poll immediately
state.notify_poll.notify_one();
// Get initial value synchronously (from the watch channel's current state)
let initial_rx = state.tx.subscribe();
let initial_torrents = initial_rx.borrow().clone();
@@ -210,8 +215,8 @@ pub async fn sse_handler(
timestamp,
};
match serde_json::to_string(&event_data) {
Ok(json) => Event::default().data(json),
match rmp_serde::to_vec(&event_data) {
Ok(bytes) => Event::default().data(BASE64.encode(bytes)),
Err(_) => Event::default().comment("init_error"),
}
};
@@ -223,10 +228,10 @@ pub async fn sse_handler(
let rx = state.event_bus.subscribe();
let update_stream = stream::unfold(rx, |mut rx| async move {
match rx.recv().await {
Ok(event) => match serde_json::to_string(&event) {
Ok(json) => Some((Ok::<Event, Infallible>(Event::default().data(json)), rx)),
Ok(event) => match rmp_serde::to_vec(&event) {
Ok(bytes) => Some((Ok::<Event, Infallible>(Event::default().data(BASE64.encode(bytes))), rx)),
Err(e) => {
tracing::warn!("Failed to serialize SSE event: {}", e);
tracing::warn!("Failed to serialize SSE event (MessagePack): {}", e);
Some((
Ok::<Event, Infallible>(Event::default().comment("error")),
rx,
@@ -241,6 +246,11 @@ pub async fn sse_handler(
}
});
Sse::new(initial_stream.chain(update_stream))
.keep_alive(axum::response::sse::KeepAlive::default())
}
let sse = Sse::new(initial_stream.chain(update_stream))
.keep_alive(axum::response::sse::KeepAlive::default());
(
[("content-type", "application/x-msgpack")],
sse
)
}

0
backend_log.txt Normal file
View File

96
docker/runner/Dockerfile Normal file
View File

@@ -0,0 +1,96 @@
# Use a base image that supports multi-arch (x64 and arm64)
# We use debian:bookworm-slim as base to install everything manually
# and then install the act_runner binary.
FROM debian:bookworm-slim
ENV DEBIAN_FRONTEND=noninteractive
ENV PATH="/root/.cargo/bin:/root/.node/bin:/root/zig:${PATH}"
# 1. Install Basic Dependencies
RUN apt-get update && apt-get install -y \
curl \
git \
build-essential \
ca-certificates \
wget \
xz-utils \
libssl-dev \
pkg-config \
file \
jq \
# Needed for some crate compilations
protobuf-compiler \
# Install binaryen to have wasm-opt available system-wide
binaryen \
&& rm -rf /var/lib/apt/lists/*
# 2. Install Node.js v20 (Manual install to support multi-arch cleanly)
RUN ARCH=$(dpkg --print-architecture) && \
if [ "$ARCH" = "amd64" ]; then NODE_ARCH="x64"; \
elif [ "$ARCH" = "arm64" ]; then NODE_ARCH="arm64"; fi && \
NODE_VER="v20.11.1" && \
curl -fsSL "https://nodejs.org/dist/$NODE_VER/node-$NODE_VER-linux-$NODE_ARCH.tar.xz" -o node.tar.xz && \
mkdir -p /root/.node && \
tar -xJf node.tar.xz -C /root/.node --strip-components=1 && \
rm node.tar.xz
# 3. Install Rust (Nightly) + Targets
RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y --default-toolchain nightly --profile minimal && \
. "$HOME/.cargo/env" && \
rustup target add wasm32-unknown-unknown && \
rustup component add rust-src
# 4. Install Zig (for Cross Compilation)
RUN ARCH=$(dpkg --print-architecture) && \
if [ "$ARCH" = "amd64" ]; then ZIG_ARCH="x86_64"; \
elif [ "$ARCH" = "arm64" ]; then ZIG_ARCH="aarch64"; fi && \
ZIG_VER="0.13.0" && \
curl -fsSL "https://ziglang.org/download/$ZIG_VER/zig-linux-$ZIG_ARCH-$ZIG_VER.tar.xz" -o zig.tar.xz && \
tar -xf zig.tar.xz && \
mv "zig-linux-$ZIG_ARCH-$ZIG_VER" /root/zig && \
rm zig.tar.xz
# 5. Fix: Create libatomic.a with __atomic_is_lock_free for MIPS
# MIPS musl static build misses __atomic_is_lock_free.
# We provide it via pure assembly to avoid Clang builtin conflicts.
RUN . "$HOME/.cargo/env" && \
printf '.text\n.globl __atomic_is_lock_free\n.type __atomic_is_lock_free, @function\n__atomic_is_lock_free:\n sltiu $v0, $a0, 5\n jr $ra\n nop\n.size __atomic_is_lock_free, .-__atomic_is_lock_free\n' > atomic.s && \
/root/zig/zig cc -target mips-linux-musl -msoft-float -c -o atomic.o atomic.s && \
/root/zig/zig ar rcs libatomic.a atomic.o && \
RUST_SYSROOT=$(rustc --print sysroot) && \
TARGET_LIB_DIR="$RUST_SYSROOT/lib/rustlib/mips-unknown-linux-musl/lib" && \
mkdir -p "$TARGET_LIB_DIR" && \
cp libatomic.a "$TARGET_LIB_DIR/" && \
rm atomic.s atomic.o libatomic.a
# 6. Install Tools (Trunk, cargo-zigbuild, wasm-bindgen protocol aligned)
# We install trunk binary to save time, others via cargo
RUN . "$HOME/.cargo/env" && \
# Install cargo-zigbuild
cargo install cargo-zigbuild && \
# Install trunk (Binary)
ARCH=$(dpkg --print-architecture) && \
if [ "$ARCH" = "amd64" ]; then TRUNK_ARCH="x86_64-unknown-linux-gnu"; \
elif [ "$ARCH" = "arm64" ]; then TRUNK_ARCH="aarch64-unknown-linux-gnu"; fi && \
wget -qO- "https://github.com/trunk-rs/trunk/releases/download/v0.21.14/trunk-$TRUNK_ARCH.tar.gz" | tar -xzf - -C /root/.cargo/bin/ && \
chmod +x /root/.cargo/bin/trunk && \
# Install wasm-bindgen-cli (Compiling from source to avoid glibc issues, doing it ONCE here)
cargo install wasm-bindgen-cli --version 0.2.108
# 7. Install Gitea Act Runner Binary
# We fetch the binary directly to run as the entrypoint
RUN ARCH=$(dpkg --print-architecture) && \
VERSION="0.2.11" && \
curl -fsSL -o /usr/local/bin/act_runner "https://dl.gitea.com/act_runner/$VERSION/act_runner-$VERSION-linux-$ARCH" && \
chmod +x /usr/local/bin/act_runner
# Create a volume for registration data
VOLUME /data
WORKDIR /data
# Define entrypoint to run the registration or daemon
# We will use a script to handle auto-registration if env vars are present
COPY entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh
ENTRYPOINT ["/entrypoint.sh"]

View File

@@ -0,0 +1,23 @@
#!/bin/bash
set -e
# If GITEA_INSTANCE_URL and GITEA_RUNNER_TOKEN are provided, try to register
if [ -n "$GITEA_INSTANCE_URL" ] && [ -n "$GITEA_RUNNER_TOKEN" ]; then
if [ ! -f ".runner" ]; then
echo "Registering runner..."
# Register with label 'mips-builder' valid for host execution
# plus 'ubuntu-latest' mapped to host for convenience if needed
act_runner register \
--instance "$GITEA_INSTANCE_URL" \
--token "$GITEA_RUNNER_TOKEN" \
--name "vibetorrent-mips-runner-$(hostname)" \
--labels "mips-builder:host,ubuntu-latest:host" \
--no-interactive
else
echo "Runner already registered."
fi
fi
# Run the daemon
echo "Starting act_runner daemon..."
exec act_runner daemon

View File

@@ -7,48 +7,30 @@ edition = "2021"
crate-type = ["cdylib", "rlib"]
[dependencies]
leptos = { version = "0.6", features = ["csr"] }
leptos_router = { version = "0.6", features = ["csr"] }
leptos = { version = "0.8.15", features = ["csr"] }
leptos_router = { version = "0.8.11" }
console_error_panic_hook = "0.1"
console_log = "1"
log = "0.4"
serde = { version = "1", features = ["derive"] }
serde_json = "1"
gloo-net = "0.5"
gloo-net = "0.6"
gloo-timers = { version = "0.3", features = ["futures"] }
wasm-bindgen = "0.2"
wasm-bindgen-futures = "0.4"
uuid = { version = "1", features = ["v4", "js"] }
futures = "0.3"
chrono = { version = "0.4", features = ["serde"] }
web-sys = { version = "0.3", features = [
"HtmlDivElement",
"HtmlUListElement",
"HtmlLiElement",
"HtmlAnchorElement",
"MouseEvent",
"Event",
"Window",
"Document",
"Element",
"DomTokenList",
"CssStyleDeclaration",
"Storage",
"TouchEvent",
"TouchList",
"Touch",
"Navigator",
"Notification",
"NotificationOptions",
"NotificationPermission",
"ServiceWorkerContainer",
"ServiceWorkerRegistration",
"PushManager",
"PushSubscription",
"PushSubscriptionOptions",
"PushSubscriptionOptionsInit"
] }
shared = { path = "../shared" }
chrono = { version = "0.4", features = ["serde", "wasm-bindgen"] }
web-sys = { version = "0.3", features = ["HtmlDivElement", "HtmlUListElement", "HtmlLiElement", "HtmlAnchorElement", "MouseEvent", "Event", "Window", "Document", "Element", "DomTokenList", "CssStyleDeclaration", "Storage", "TouchEvent", "TouchList", "Touch", "Navigator", "Notification", "NotificationOptions", "NotificationPermission", "ServiceWorkerContainer", "ServiceWorkerRegistration", "PushManager", "PushSubscription", "PushSubscriptionOptions", "PushSubscriptionOptionsInit", "HtmlDetailsElement", "HtmlInputElement", "HtmlFormElement", "HtmlDialogElement", "ProgressEvent"] }
shared = { path = "../shared", features = ["hydrate"] }
tailwind_fuse = "0.3.2"
js-sys = "0.3.85"
base64 = "0.22.1"
serde-wasm-bindgen = "0.6.5"
leptos-use = { version = "0.16", features = ["storage"] }
codee = "0.3"
thiserror = "2.0"
rmp-serde = "1.3"
struct-patch = "0.5"
leptos-shadcn-ui = { version = "0.9.0", default-features = false, features = ["button", "input"] }

View File

@@ -4,7 +4,7 @@
<meta charset="utf-8" />
<meta
name="viewport"
content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no, viewport-fit=cover"
content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, viewport-fit=cover"
/>
<title>VibeTorrent</title>
@@ -81,17 +81,20 @@
</script>
</head>
<body>
<body style="cursor: pointer;">
<div
id="app-loading"
style="
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
height: 100vh;
font-family: sans-serif;
"
>
<div
id="app-loading-spinner"
style="
width: 40px;
height: 40px;
@@ -102,6 +105,32 @@
opacity: 0.5;
"
></div>
<div
id="app-loading-error"
style="display: none; text-align: center; margin-top: 20px; padding: 0 20px"
>
<p style="color: #ef4444; font-weight: bold; margin-bottom: 8px">
Uygulama yüklenemedi
</p>
<p style="font-size: 14px; opacity: 0.7">
Bağlantınız yavaş olabilir veya bir sistem hatası oluşmuş olabilir.
</p>
<button
onclick="location.reload()"
style="
margin-top: 16px;
padding: 8px 16px;
background: #3b82f6;
color: white;
border: none;
border-radius: 6px;
cursor: pointer;
font-weight: 500;
"
>
Sayfayı Yenile
</button>
</div>
</div>
<style>
@keyframes spin {
@@ -113,10 +142,62 @@
body.app-loaded #app-loading {
display: none !important;
}
/* iOS Safari Click Fixes */
body {
cursor: pointer;
-webkit-tap-highlight-color: transparent;
}
summary {
list-style: none;
}
summary::-webkit-details-marker {
display: none;
}
</style>
<script>
// App loading timeout handler
(function () {
var timeout = setTimeout(function () {
if (!document.body.classList.contains("app-loaded")) {
var spinner = document.getElementById("app-loading-spinner");
var error = document.getElementById("app-loading-error");
if (spinner) spinner.style.display = "none";
if (error) error.style.display = "block";
}
}, 15000); // 15 seconds timeout
// Clean up timeout if app loads
var observer = new MutationObserver(function (mutations) {
mutations.forEach(function (mutation) {
if (
mutation.attributeName === "class" &&
document.body.classList.contains("app-loaded")
) {
clearTimeout(timeout);
observer.disconnect();
}
});
});
observer.observe(document.body, { attributes: true });
})();
</script>
<!-- Service Worker Registration & PWA Setup -->
<script>
// Global Dropdown Closer for iOS/Mobile
document.addEventListener('click', function(event) {
const details = document.querySelectorAll('details[open]');
details.forEach(detail => {
// Eğer tıklanan yer bu details'in içinde değilse kapat
if (!detail.contains(event.target)) {
detail.removeAttribute('open');
}
});
}, true); // Use capture phase for better mobile support
if ("serviceWorker" in navigator) {
window.addEventListener("load", () => {
navigator.serviceWorker

View File

@@ -1,16 +1,130 @@
@import "tailwindcss";
@config "./tailwind.config.js";
@plugin "daisyui" {
themes:
light, dark, dim, nord, cupcake, dracula, cyberpunk, emerald, sunset,
abyss;
@theme {
/* Shadcn Colors */
--color-border: hsl(var(--border));
--color-input: hsl(var(--input));
--color-ring: hsl(var(--ring));
--color-background: hsl(var(--background));
--color-foreground: hsl(var(--foreground));
--color-primary: hsl(var(--primary));
--color-primary-foreground: hsl(var(--primary-foreground));
--color-secondary: hsl(var(--secondary));
--color-secondary-foreground: hsl(var(--secondary-foreground));
--color-destructive: hsl(var(--destructive));
--color-destructive-foreground: hsl(var(--destructive-foreground));
--color-muted: hsl(var(--muted));
--color-muted-foreground: hsl(var(--muted-foreground));
--color-accent: hsl(var(--accent));
--color-accent-foreground: hsl(var(--accent-foreground));
--color-popover: hsl(var(--popover));
--color-popover-foreground: hsl(var(--popover-foreground));
--color-card: hsl(var(--card));
--color-card-foreground: hsl(var(--card-foreground));
--radius-lg: var(--radius);
--radius-md: calc(var(--radius) - 2px);
--radius-sm: calc(var(--radius) - 4px);
--animate-accordion-down: accordion-down 0.2s ease-out;
--animate-accordion-up: accordion-up 0.2s ease-out;
@keyframes accordion-down {
from {
height: 0;
}
to {
height: var(--radix-accordion-content-height);
}
}
@keyframes accordion-up {
from {
height: var(--radix-accordion-content-height);
}
to {
height: 0;
}
}
}
@layer base {
html,
:root {
--background: 0 0% 100%;
--foreground: 222.2 84% 4.9%;
--card: 0 0% 100%;
--card-foreground: 222.2 84% 4.9%;
--popover: 0 0% 100%;
--popover-foreground: 222.2 84% 4.9%;
--primary: 222.2 47.4% 11.2%;
--primary-foreground: 210 40% 98%;
--secondary: 210 40% 96.1%;
--secondary-foreground: 222.2 47.4% 11.2%;
--muted: 210 40% 96.1%;
--muted-foreground: 215.4 16.3% 46.9%;
--accent: 210 40% 96.1%;
--accent-foreground: 222.2 47.4% 11.2%;
--destructive: 0 84.2% 60.2%;
--destructive-foreground: 210 40% 98%;
--border: 214.3 31.8% 91.4%;
--input: 214.3 31.8% 91.4%;
--ring: 222.2 84% 4.9%;
--radius: 0.5rem;
}
.dark {
--background: 222.2 84% 4.9%;
--foreground: 210 40% 98%;
--card: 222.2 84% 4.9%;
--card-foreground: 210 40% 98%;
--popover: 222.2 84% 4.9%;
--popover-foreground: 210 40% 98%;
--primary: 210 40% 98%;
--primary-foreground: 222.2 47.4% 11.2%;
--secondary: 217.2 32.6% 17.5%;
--secondary-foreground: 210 40% 98%;
--muted: 217.2 32.6% 17.5%;
--muted-foreground: 215 20.2% 65.1%;
--accent: 217.2 32.6% 17.5%;
--accent-foreground: 210 40% 98%;
--destructive: 0 62.8% 30.6%;
--destructive-foreground: 210 40% 98%;
--border: 217.2 32.6% 17.5%;
--input: 217.2 32.6% 17.5%;
--ring: 212.7 26.8% 83.9%;
}
}
@layer base {
* {
@apply border-border;
}
body {
@apply min-h-dvh w-full overflow-hidden bg-base-100 text-base-content overscroll-y-none;
@apply bg-background text-foreground;
}
}

View File

@@ -11,7 +11,6 @@
"license": "ISC",
"devDependencies": {
"autoprefixer": "^10.4.23",
"daisyui": "^5.5.1-beta.2",
"postcss": "^8.5.6",
"tailwindcss": "^4.1.18"
},

File diff suppressed because it is too large Load Diff

210
frontend/src/api/mod.rs Normal file
View File

@@ -0,0 +1,210 @@
use gloo_net::http::Request;
use thiserror::Error;
#[derive(Debug, Error)]
pub enum ApiError {
#[error("Network error")]
Network,
#[error("Server error: {status}")]
Server { status: u16 },
#[error("Login failed")]
LoginFailed,
#[error("Unauthorized")]
Unauthorized,
#[error("Too many requests")]
RateLimited,
#[error("Server function error: {0}")]
ServerFn(String),
}
fn base_url() -> String {
"/api".to_string()
}
pub mod auth {
use super::*;
#[derive(serde::Serialize)]
pub struct LoginRequest {
pub username: String,
pub password: String,
pub remember_me: bool,
}
pub async fn login(
username: &str,
password: &str,
remember_me: bool,
) -> Result<(), ApiError> {
let req = LoginRequest {
username: username.to_string(),
password: password.to_string(),
remember_me,
};
let resp = Request::post(&format!("{}/auth/login", base_url()))
.json(&req)
.map_err(|_| ApiError::Network)?
.send()
.await
.map_err(|_| ApiError::Network)?;
if resp.ok() {
Ok(())
} else if resp.status() == 429 {
Err(ApiError::RateLimited)
} else {
Err(ApiError::LoginFailed)
}
}
pub async fn logout() -> Result<(), ApiError> {
Request::post(&format!("{}/auth/logout", base_url()))
.send()
.await
.map_err(|_| ApiError::Network)?;
Ok(())
}
pub async fn check_auth() -> Result<bool, ApiError> {
let resp = Request::get(&format!("{}/auth/check", base_url()))
.send()
.await
.map_err(|_| ApiError::Network)?;
Ok(resp.ok())
}
#[derive(serde::Deserialize)]
pub struct UserResponse {
pub username: String,
}
pub async fn get_user() -> Result<UserResponse, ApiError> {
let resp = Request::get(&format!("{}/auth/check", base_url()))
.send()
.await
.map_err(|_| ApiError::Network)?;
let user = resp.json().await.map_err(|_| ApiError::Network)?;
Ok(user)
}
}
pub mod setup {
use super::*;
#[derive(serde::Serialize)]
pub struct SetupRequest {
pub username: String,
pub password: String,
}
#[derive(serde::Deserialize)]
pub struct SetupStatusResponse {
pub completed: bool,
}
pub async fn get_status() -> Result<SetupStatusResponse, ApiError> {
let resp = Request::get(&format!("{}/setup/status", base_url()))
.send()
.await
.map_err(|_| ApiError::Network)?;
let status = resp.json().await.map_err(|_| ApiError::Network)?;
Ok(status)
}
pub async fn setup(username: &str, password: &str) -> Result<(), ApiError> {
let req = SetupRequest {
username: username.to_string(),
password: password.to_string(),
};
Request::post(&format!("{}/setup", base_url()))
.json(&req)
.map_err(|_| ApiError::Network)?
.send()
.await
.map_err(|_| ApiError::Network)?;
Ok(())
}
}
pub mod settings {
use super::*;
use shared::GlobalLimitRequest;
pub async fn set_global_limits(req: &GlobalLimitRequest) -> Result<(), ApiError> {
shared::server_fns::settings::set_global_limits(
req.max_download_rate,
req.max_upload_rate,
)
.await
.map_err(|e| ApiError::ServerFn(e.to_string()))
}
}
pub mod push {
use super::*;
pub async fn get_public_key() -> Result<String, ApiError> {
shared::server_fns::push::get_public_key()
.await
.map_err(|e| ApiError::ServerFn(e.to_string()))
}
pub async fn subscribe(endpoint: &str, p256dh: &str, auth: &str) -> Result<(), ApiError> {
shared::server_fns::push::subscribe_push(
endpoint.to_string(),
p256dh.to_string(),
auth.to_string(),
)
.await
.map_err(|e| ApiError::ServerFn(e.to_string()))
}
}
pub mod torrent {
use super::*;
pub async fn add(uri: &str) -> Result<(), ApiError> {
shared::server_fns::torrent::add_torrent(uri.to_string())
.await
.map_err(|e| ApiError::ServerFn(e.to_string()))
}
pub async fn action(hash: &str, action: &str) -> Result<(), ApiError> {
shared::server_fns::torrent::torrent_action(hash.to_string(), action.to_string())
.await
.map(|_| ())
.map_err(|e| ApiError::ServerFn(e.to_string()))
}
pub async fn delete(hash: &str) -> Result<(), ApiError> {
action(hash, "delete").await
}
pub async fn delete_with_data(hash: &str) -> Result<(), ApiError> {
action(hash, "delete_with_data").await
}
pub async fn start(hash: &str) -> Result<(), ApiError> {
action(hash, "start").await
}
pub async fn stop(hash: &str) -> Result<(), ApiError> {
action(hash, "stop").await
}
pub async fn set_label(hash: &str, label: &str) -> Result<(), ApiError> {
shared::server_fns::torrent::set_label(hash.to_string(), label.to_string())
.await
.map_err(|e| ApiError::ServerFn(e.to_string()))
}
pub async fn set_priority(hash: &str, file_index: u32, priority: u8) -> Result<(), ApiError> {
shared::server_fns::torrent::set_file_priority(
hash.to_string(),
file_index,
priority,
)
.await
.map_err(|e| ApiError::ServerFn(e.to_string()))
}
}

View File

@@ -1,94 +1,158 @@
use crate::components::layout::sidebar::Sidebar;
use crate::components::layout::statusbar::StatusBar;
use crate::components::layout::toolbar::Toolbar;
use crate::components::layout::protected::Protected;
use crate::components::toast::ToastContainer;
use crate::components::torrent::table::TorrentTable;
use leptos::*;
use leptos_router::*;
use crate::components::auth::login::Login;
use crate::components::auth::setup::Setup;
use crate::api;
use leptos::prelude::*;
use leptos::task::spawn_local;
use leptos_router::components::{Router, Routes, Route};
use leptos_router::hooks::use_navigate;
#[component]
pub fn App() -> impl IntoView {
crate::store::provide_torrent_store();
let store = use_context::<crate::store::TorrentStore>();
// Initialize push notifications after user grants permission
create_effect(move |_| {
spawn_local(async {
// Wait a bit for service worker to be ready
gloo_timers::future::TimeoutFuture::new(2000).await;
// Check if running on iOS and not standalone
if let Some(ios_message) = crate::utils::platform::get_ios_notification_info() {
log::warn!("iOS detected: {}", ios_message);
// Show toast to inform user
if let Some(store) = use_context::<crate::store::TorrentStore>() {
crate::store::show_toast_with_signal(
store.notifications,
shared::NotificationLevel::Info,
ios_message,
);
let is_loading = signal(true);
let is_authenticated = signal(false);
let needs_setup = signal(false);
Effect::new(move |_| {
spawn_local(async move {
log::info!("App initialization started...");
// Check if setup is needed via Server Function
match shared::server_fns::auth::get_setup_status().await {
Ok(status) => {
if !status.completed {
log::info!("Setup not completed");
needs_setup.1.set(true);
is_loading.1.set(false);
return;
}
}
return;
Err(e) => log::error!("Failed to get setup status: {:?}", e),
}
// Check if push notifications are supported
if !crate::utils::platform::supports_push_notifications() {
log::warn!("Push notifications not supported on this platform");
return;
}
// Safari requires user gesture for notification permission
// Don't auto-request on Safari - user should click a button
if crate::utils::platform::is_safari() {
log::info!("Safari detected - notification permission requires user interaction");
if let Some(store) = use_context::<crate::store::TorrentStore>() {
crate::store::show_toast_with_signal(
store.notifications,
shared::NotificationLevel::Info,
"Bildirim izni için sağ alttaki ayarlar ⚙️ ikonuna basın.".to_string(),
);
// Check authentication via GetUser Server Function
match shared::server_fns::auth::get_user().await {
Ok(Some(user_info)) => {
log::info!("Authenticated as {}", user_info.username);
if let Some(s) = store {
s.user.set(Some(user_info.username));
}
is_authenticated.1.set(true);
}
Ok(None) => {
log::info!("Not authenticated");
}
Err(e) => {
log::error!("Auth check failed: {:?}", e);
}
return;
}
// For non-Safari browsers (Chrome, Firefox, Edge), attempt auto-subscribe
log::info!("Attempting to subscribe to push notifications...");
crate::store::subscribe_to_push_notifications().await;
is_loading.1.set(false);
});
});
Effect::new(move |_| {
if is_authenticated.0.get() {
spawn_local(async {
gloo_timers::future::TimeoutFuture::new(2000).await;
if crate::utils::platform::supports_push_notifications() && !crate::utils::platform::is_safari() {
crate::store::subscribe_to_push_notifications().await;
}
});
}
});
view! {
// Main app wrapper - ensures proper stacking context
<div class="relative w-full h-screen" style="height: 100dvh;">
// Drawer layout
<div class="drawer lg:drawer-open h-full w-full">
<input id="my-drawer" type="checkbox" class="drawer-toggle" />
<Router>
<Routes fallback=|| view! { <div class="p-4">"404 Not Found"</div> }>
<Route path=leptos_router::path!("/login") view=move || {
let authenticated = is_authenticated.0.get();
let setup_needed = needs_setup.0.get();
Effect::new(move |_| {
if setup_needed {
let navigate = use_navigate();
navigate("/setup", Default::default());
} else if authenticated {
log::info!("Already authenticated, redirecting to home");
let navigate = use_navigate();
navigate("/", Default::default());
}
});
view! { <Login /> }
} />
<Route path=leptos_router::path!("/setup") view=move || {
Effect::new(move |_| {
if is_authenticated.0.get() {
let navigate = use_navigate();
navigate("/", Default::default());
}
});
view! { <Setup /> }
} />
<div class="drawer-content flex flex-col h-full overflow-hidden bg-base-100 text-base-content text-sm select-none">
<Toolbar />
<Route path=leptos_router::path!("/") view=move || {
let navigate = use_navigate();
Effect::new(move |_| {
if !is_loading.0.get() {
if needs_setup.0.get() {
log::info!("Setup not completed, redirecting to setup");
navigate("/setup", Default::default());
} else if !is_authenticated.0.get() {
log::info!("Not authenticated, redirecting to login");
navigate("/login", Default::default());
}
}
});
view! {
<Show when=move || !is_loading.0.get() fallback=|| view! {
<div class="flex items-center justify-center h-screen bg-background">
<div class="flex flex-col items-center gap-4">
<div class="animate-spin h-8 w-8 border-4 border-primary border-t-transparent rounded-full"></div>
<p class="text-sm text-muted-foreground">"Yükleniyor..."</p>
</div>
</div>
}>
<Show when=move || is_authenticated.0.get() fallback=|| ()>
<Protected>
<TorrentTable />
</Protected>
</Show>
</Show>
}
}/>
<main class="flex-1 flex flex-col min-w-0 bg-base-100 overflow-hidden pb-8">
<Router>
<Routes>
<Route path="/" view=move || view! { <TorrentTable /> } />
<Route path="/settings" view=move || view! { <div class="p-4">"Settings Page (Coming Soon)"</div> } />
</Routes>
</Router>
</main>
<Route path=leptos_router::path!("/settings") view=move || {
Effect::new(move |_| {
if !is_authenticated.0.get() {
let navigate = use_navigate();
navigate("/login", Default::default());
}
});
view! {
<Show when=move || !is_loading.0.get() fallback=|| ()>
<Show when=move || is_authenticated.0.get() fallback=|| ()>
<Protected>
<div class="p-4">"Settings Page (Coming Soon)"</div>
</Protected>
</Show>
</Show>
}
}/>
</Routes>
</Router>
// StatusBar is rendered via fixed positioning, just mount it here
<StatusBar />
</div>
<div class="drawer-side z-40 transition-none duration-0">
<label for="my-drawer" aria-label="close sidebar" class="drawer-overlay transition-none duration-0"></label>
<div class="menu p-0 min-h-full bg-base-200 text-base-content border-r border-base-300 transition-none duration-0">
<Sidebar />
</div>
</div>
</div>
// Toast container - fixed positioning relative to viewport
<ToastContainer />
</div>
}

View File

@@ -0,0 +1,105 @@
use leptos::prelude::*;
use leptos::task::spawn_local;
#[component]
pub fn Login() -> impl IntoView {
let username = signal(String::new());
let password = signal(String::new());
let error = signal(Option::<String>::None);
let loading = signal(false);
let handle_login = move |ev: web_sys::SubmitEvent| {
ev.prevent_default();
loading.1.set(true);
error.1.set(None);
let user = username.0.get();
let pass = password.0.get();
log::info!("Attempting login for user: {}", user);
spawn_local(async move {
match shared::server_fns::auth::login(user, pass).await {
Ok(_) => {
log::info!("Login successful, redirecting...");
let window = web_sys::window().expect("window should exist");
let _ = window.location().set_href("/");
}
Err(e) => {
log::error!("Login failed: {:?}", e);
error.1.set(Some("Geçersiz kullanıcı adı veya şifre".to_string()));
loading.1.set(false);
}
}
});
};
view! {
<div class="flex items-center justify-center min-h-screen bg-muted/40">
<div class="w-full max-w-sm rounded-xl border border-border bg-card text-card-foreground shadow-lg">
<div class="flex flex-col space-y-1.5 p-6 pb-2 items-center">
<div class="w-12 h-12 bg-primary rounded-xl flex items-center justify-center text-primary-foreground shadow-sm mb-4">
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="w-6 h-6">
<path stroke-linecap="round" stroke-linejoin="round" d="M15.362 5.214A8.252 8.252 0 0112 21 8.25 8.25 0 016.038 7.048 8.287 8.287 0 009 9.6a8.983 8.983 0 013.361-6.867 8.21 8.25 0 003 2.48z" />
<path stroke-linecap="round" stroke-linejoin="round" d="M12 18a3.75 3.75 0 00.495-7.467 5.99 5.99 0 00-1.925 3.546 5.974 5.974 0 01-2.133-1A3.75 3.75 0 0012 18z" />
</svg>
</div>
<h3 class="font-semibold tracking-tight text-2xl">"VibeTorrent"</h3>
<p class="text-sm text-muted-foreground">"Hesabınıza giriş yapın"</p>
</div>
<div class="p-6 pt-4">
<form on:submit=handle_login class="space-y-4">
<div class="space-y-2">
<label class="text-sm font-medium leading-none peer-disabled:cursor-not-allowed peer-disabled:opacity-70">
"Kullanıcı Adı"
</label>
<input
type="text"
placeholder="Kullanıcı adınız"
class="flex h-9 w-full rounded-md border border-input bg-transparent px-3 py-1 text-sm shadow-sm transition-colors file:border-0 file:bg-transparent file:text-sm file:font-medium placeholder:text-muted-foreground focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring disabled:cursor-not-allowed disabled:opacity-50"
prop:value=move || username.0.get()
on:input=move |ev| username.1.set(event_target_value(&ev))
disabled=move || loading.0.get()
required
/>
</div>
<div class="space-y-2">
<label class="text-sm font-medium leading-none peer-disabled:cursor-not-allowed peer-disabled:opacity-70">
"Şifre"
</label>
<input
type="password"
placeholder="******"
class="flex h-9 w-full rounded-md border border-input bg-transparent px-3 py-1 text-sm shadow-sm transition-colors file:border-0 file:bg-transparent file:text-sm file:font-medium placeholder:text-muted-foreground focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring disabled:cursor-not-allowed disabled:opacity-50"
prop:value=move || password.0.get()
on:input=move |ev| password.1.set(event_target_value(&ev))
disabled=move || loading.0.get()
required
/>
</div>
<Show when=move || error.0.get().is_some() fallback=|| ()>
<div class="rounded-md border border-destructive/50 bg-destructive/10 p-3 text-sm text-destructive dark:border-destructive dark:bg-destructive/50 dark:text-destructive-foreground">
<span>{move || error.0.get().unwrap_or_default()}</span>
</div>
</Show>
<div class="pt-2">
<button
class="inline-flex items-center justify-center whitespace-nowrap rounded-md text-sm font-medium transition-colors focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring disabled:pointer-events-none disabled:opacity-50 bg-primary text-primary-foreground shadow hover:bg-primary/90 h-9 px-4 py-2 w-full"
type="submit"
disabled=move || loading.0.get()
>
<Show when=move || loading.0.get() fallback=|| "Giriş Yap">
<span class="animate-spin mr-2 h-4 w-4 border-2 border-current border-t-transparent rounded-full"></span>
"Giriş Yapılıyor..."
</Show>
</button>
</div>
</form>
</div>
</div>
</div>
}
}

View File

@@ -0,0 +1,2 @@
pub mod login;
pub mod setup;

View File

@@ -0,0 +1,130 @@
use leptos::prelude::*;
use leptos::task::spawn_local;
#[component]
pub fn Setup() -> impl IntoView {
let username = signal(String::new());
let password = signal(String::new());
let confirm_password = signal(String::new());
let error = signal(Option::<String>::None);
let loading = signal(false);
let handle_setup = move |ev: web_sys::SubmitEvent| {
ev.prevent_default();
let pass = password.0.get();
let confirm = confirm_password.0.get();
if pass != confirm {
error.1.set(Some("Şifreler eşleşmiyor".to_string()));
return;
}
if pass.len() < 6 {
error.1.set(Some("Şifre en az 6 karakter olmalıdır".to_string()));
return;
}
loading.1.set(true);
error.1.set(None);
let user = username.0.get();
spawn_local(async move {
match shared::server_fns::auth::setup(user, pass).await {
Ok(_) => {
log::info!("Setup completed successfully, redirecting...");
let window = web_sys::window().expect("window should exist");
let _ = window.location().set_href("/");
}
Err(e) => {
log::error!("Setup failed: {:?}", e);
error.1.set(Some("Kurulum sırasında bir hata oluştu".to_string()));
loading.1.set(false);
}
}
});
};
view! {
<div class="flex items-center justify-center min-h-screen bg-muted/40 px-4">
<div class="w-full max-w-md rounded-xl border border-border bg-card text-card-foreground shadow-lg overflow-hidden">
<div class="flex flex-col space-y-1.5 p-6 pb-2 items-center text-center">
<div class="w-12 h-12 bg-primary rounded-xl flex items-center justify-center text-primary-foreground shadow-sm mb-4">
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="w-6 h-6">
<path stroke-linecap="round" stroke-linejoin="round" d="M11.42 15.17L17.25 21A2.652 2.652 0 0021 17.25l-5.877-5.877M11.42 15.17l2.496-3.03c.317-.384.74-.626 1.208-.766M11.42 15.17l-4.655 5.653a2.548 2.548 0 11-3.586-3.586l6.837-5.63m5.108-3.497a2.548 2.548 0 113.586 3.586l-6.837 5.63m-5.108 3.497l2.496-3.03c.317-.384.74-.626 1.208-.766M15.75 9.25a2.548 2.548 0 11-5.096 0 2.548 2.548 0 015.096 0z" />
</svg>
</div>
<h3 class="font-semibold tracking-tight text-2xl">"VibeTorrent Kurulumu"</h3>
<p class="text-sm text-muted-foreground">"Yönetici hesabınızı oluşturun"</p>
</div>
<div class="p-6 pt-4">
<form on:submit=handle_setup class="space-y-4">
<div class="space-y-2">
<label class="text-sm font-medium leading-none peer-disabled:cursor-not-allowed peer-disabled:opacity-70">
"Yönetici Kullanıcı Adı"
</label>
<input
type="text"
placeholder="admin"
class="flex h-9 w-full rounded-md border border-input bg-transparent px-3 py-1 text-sm shadow-sm transition-colors placeholder:text-muted-foreground focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring disabled:opacity-50"
prop:value=move || username.0.get()
on:input=move |ev| username.1.set(event_target_value(&ev))
disabled=move || loading.0.get()
required
/>
</div>
<div class="space-y-2">
<label class="text-sm font-medium leading-none peer-disabled:cursor-not-allowed peer-disabled:opacity-70">
"Şifre"
</label>
<input
type="password"
placeholder="******"
class="flex h-9 w-full rounded-md border border-input bg-transparent px-3 py-1 text-sm shadow-sm transition-colors placeholder:text-muted-foreground focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring disabled:opacity-50"
prop:value=move || password.0.get()
on:input=move |ev| password.1.set(event_target_value(&ev))
disabled=move || loading.0.get()
required
/>
</div>
<div class="space-y-2">
<label class="text-sm font-medium leading-none peer-disabled:cursor-not-allowed peer-disabled:opacity-70">
"Şifre Onay"
</label>
<input
type="password"
placeholder="******"
class="flex h-9 w-full rounded-md border border-input bg-transparent px-3 py-1 text-sm shadow-sm transition-colors placeholder:text-muted-foreground focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring disabled:opacity-50"
prop:value=move || confirm_password.0.get()
on:input=move |ev| confirm_password.1.set(event_target_value(&ev))
disabled=move || loading.0.get()
required
/>
</div>
<Show when=move || error.0.get().is_some() fallback=|| ()>
<div class="rounded-md border border-destructive/50 bg-destructive/10 p-3 text-sm text-destructive dark:border-destructive dark:bg-destructive/50 dark:text-destructive-foreground">
<span>{move || error.0.get().unwrap_or_default()}</span>
</div>
</Show>
<div class="pt-2">
<button
class="inline-flex items-center justify-center whitespace-nowrap rounded-md text-sm font-medium transition-colors focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring disabled:pointer-events-none disabled:opacity-50 bg-primary text-primary-foreground shadow hover:bg-primary/90 h-9 px-4 py-2 w-full"
type="submit"
disabled=move || loading.0.get()
>
<Show when=move || loading.0.get() fallback=|| "Kurulumu Tamamla">
<span class="animate-spin mr-2 h-4 w-4 border-2 border-current border-t-transparent rounded-full"></span>
"Kuruluyor..."
</Show>
</button>
</div>
</form>
</div>
</div>
</div>
}
}

View File

@@ -1,98 +1,97 @@
use leptos::*;
use leptos::prelude::*;
use leptos::html;
use leptos_use::on_click_outside;
fn handle_action(
hash: String,
action: &str,
on_action: Callback<(String, String)>,
on_close: Callback<()>,
) {
log::info!("ContextMenu: Action '{}' for hash '{}'", action, hash);
on_action.run((action.to_string(), hash));
on_close.run(());
}
#[component]
pub fn ContextMenu(
position: (i32, i32),
visible: bool,
torrent_hash: String,
on_close: Callback<()>,
on_action: Callback<(String, String)>, // (Action, Hash)
on_action: Callback<(String, String)>,
) -> impl IntoView {
let handle_action = move |action: &str| {
let hash = torrent_hash.clone();
let action_str = action.to_string();
logging::log!("ContextMenu: Action '{}' for hash '{}'", action_str, hash);
on_action.call((action_str, hash)); // Delegate FIRST
on_close.call(()); // Close menu AFTER
};
let container_ref = NodeRef::<html::Div>::new();
let _ = on_click_outside(container_ref, move |_| on_close.run(()));
if !visible {
return view! {}.into_view();
}
let (x, y) = position;
let hash1 = torrent_hash.clone();
let hash2 = torrent_hash.clone();
let hash3 = torrent_hash.clone();
let hash4 = torrent_hash.clone();
let hash5 = torrent_hash;
view! {
// Backdrop to catch clicks outside
<div
class="fixed inset-0 z-[99] cursor-default"
role="button"
tabindex="-1"
on:click=move |_| on_close.call(())
on:contextmenu=move |e| e.prevent_default()
></div>
<div
node_ref=container_ref
class="fixed z-[100] min-w-[200px] animate-in fade-in zoom-in-95 duration-100"
style=format!("left: {}px; top: {}px", position.0, position.1)
style=format!("left: {}px; top: {}px;", x, y)
on:contextmenu=move |e| e.prevent_default()
>
<ul class="menu bg-base-200 text-base-content rounded-box shadow-xl border border-white/5 p-2 gap-1">
<ul class="menu bg-base-200 shadow-xl rounded-box border border-base-300 p-1 gap-0.5">
<li>
<button
class="gap-3 active:bg-primary active:text-primary-content"
on:click={
let handle_action = handle_action.clone();
move |_| handle_action("start")
}
>
<svg class="w-4 h-4 text-success" fill="none" viewBox="0 0 24 24" stroke="currentColor"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M14.752 11.168l-3.197-2.132A1 1 0 0010 9.87v4.263a1 1 0 001.555.832l3.197-2.132a1 1 0 000-1.664z" /><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M21 12a9 9 0 11-18 0 9 9 0 0118 0z" /></svg>
"Resume"
<button class="flex items-center gap-3 px-3 py-2 hover:bg-primary hover:text-primary-content rounded-lg transition-colors" on:click=move |_| {
handle_action(hash1.clone(), "start", on_action.clone(), on_close.clone());
}>
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="w-4 h-4">
<path stroke-linecap="round" stroke-linejoin="round" d="M5.25 5.653c0-.856.917-1.398 1.667-.986l11.54 6.348a1.125 1.125 0 010 1.971l-11.54 6.347a1.125 1.125 0 01-1.667-.985V5.653z" />
</svg>
<span>"Start"</span>
</button>
</li>
<li>
<button
class="gap-3 active:bg-primary active:text-primary-content"
on:click={
let handle_action = handle_action.clone();
move |_| handle_action("stop")
}
>
<svg class="w-4 h-4 text-warning" fill="none" viewBox="0 0 24 24" stroke="currentColor"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M10 9v6m4-6v6m7-3a9 9 0 11-18 0 9 9 0 0118 0z" /></svg>
"Pause"
<li>
<button class="flex items-center gap-3 px-3 py-2 hover:bg-primary hover:text-primary-content rounded-lg transition-colors" on:click=move |_| {
handle_action(hash2.clone(), "stop", on_action.clone(), on_close.clone());
}>
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="w-4 h-4">
<path stroke-linecap="round" stroke-linejoin="round" d="M15.75 5.25v13.5m-7.5-13.5v13.5" />
</svg>
<span>"Stop"</span>
</button>
</li>
<div class="divider my-0 h-px p-0 opacity-10"></div>
<li>
<button
class="gap-3 text-error hover:bg-error/10 active:bg-error active:text-error-content"
on:click={
let handle_action = handle_action.clone();
move |_| handle_action("delete")
}
>
<svg class="w-4 h-4" fill="none" viewBox="0 0 24 24" stroke="currentColor"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 7l-.867 12.142A2 2 0 0116.138 21H7.862a2 2 0 01-1.995-1.858L5 7m5 4v6m4-6v6m1-10V4a1 1 0 00-1-1h-4a1 1 0 00-1 1v3M4 7h16" /></svg>
"Delete"
<button class="flex items-center gap-3 px-3 py-2 hover:bg-primary hover:text-primary-content rounded-lg transition-colors" on:click=move |_| {
handle_action(hash3.clone(), "recheck", on_action.clone(), on_close.clone());
}>
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="w-4 h-4">
<path stroke-linecap="round" stroke-linejoin="round" d="M16.023 9.348h4.992v-.001M2.985 19.644v-4.992m0 0h4.992m-4.993 0l3.181 3.183a8.25 8.25 0 0013.803-3.7M4.031 9.865a8.25 8.25 0 0113.803-3.7l3.181 3.182m0-4.991v4.99" />
</svg>
<span>"Recheck"</span>
</button>
</li>
<div class="divider my-0.5 opacity-50"></div>
<li>
<button
class="gap-3 text-error hover:bg-error/10 active:bg-error active:text-error-content text-xs"
on:click={
let handle_action = handle_action.clone();
move |_| handle_action("delete_with_data")
}
>
<svg class="w-4 h-4" fill="none" viewBox="0 0 24 24" stroke="currentColor"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M13 10V3L4 14h7v7l9-11h-7z" /></svg>
<span>"Delete with Data"</span>
<button class="flex items-center gap-3 px-3 py-2 text-error hover:bg-error hover:text-error-content rounded-lg transition-colors" on:click=move |_| {
handle_action(hash4.clone(), "delete", on_action.clone(), on_close.clone());
}>
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="w-4 h-4">
<path stroke-linecap="round" stroke-linejoin="round" d="M14.74 9l-.346 9m-4.788 0L9.26 9m9.968-3.21c.342.052.682.107 1.022.166m-1.022-.165L18.16 19.673a2.25 2.25 0 01-2.244 2.077H8.084a2.25 2.25 0 01-2.244-2.077L4.772 5.79m14.456 0a48.108 48.108 0 00-3.478-.397m-12 .562c.34-.059.68-.114 1.022-.165m0 0a48.11 48.11 0 013.478-.397m7.5 0v-.916c0-1.18-.91-2.164-2.09-2.164h-2.34c-1.18 0-2.09.984-2.09 2.164v.916m7.5 0a48.667 48.667 0 00-7.5 0" />
</svg>
<span>"Remove"</span>
</button>
</li>
<li>
<button class="flex items-center gap-3 px-3 py-2 text-error hover:bg-error hover:text-error-content rounded-lg transition-colors" on:click=move |_| {
handle_action(hash5.clone(), "delete_with_data", on_action.clone(), on_close.clone());
}>
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="w-4 h-4">
<path stroke-linecap="round" stroke-linejoin="round" d="M20.25 7.5l-.625 10.632a2.25 2.25 0 01-2.247 2.118H6.622a2.25 2.25 0 01-2.247-2.118L3.75 7.5m6 4.125l2.25 2.25m0 0l2.25 2.25M12 13.875l2.25-2.25M12 13.875l-2.25-2.25M3.375 7.5h17.25c.621 0 1.125-.504 1.125-1.125v-1.5c0-.621-.504-1.125-1.125-1.125H3.375c-.621 0-1.125.504-1.125 1.125v1.5c0 .621.504 1.125 1.125 1.125z" />
</svg>
<span>"Remove Data"</span>
</button>
</li>
</ul>
</div>
}.into_view()
}
}

View File

@@ -1,3 +1,4 @@
pub mod sidebar;
pub mod toolbar;
pub mod statusbar;
pub mod toolbar;
pub mod protected;

View File

@@ -0,0 +1,32 @@
use leptos::prelude::*;
use crate::components::layout::sidebar::Sidebar;
use crate::components::layout::toolbar::Toolbar;
use crate::components::layout::statusbar::StatusBar;
#[component]
pub fn Protected(children: Children) -> impl IntoView {
view! {
<div class="drawer lg:drawer-open h-full w-full">
<input id="my-drawer" type="checkbox" class="drawer-toggle" />
<div class="drawer-content flex flex-col h-full overflow-hidden bg-base-100">
// --- TOOLBAR (TOP) ---
<Toolbar />
// --- MAIN CONTENT ---
<main class="flex-1 overflow-hidden relative">
{children()}
</main>
// --- STATUS BAR (BOTTOM) ---
<StatusBar />
</div>
// --- SIDEBAR (DRAWER) ---
<div class="drawer-side z-[100]">
<label for="my-drawer" aria-label="close sidebar" class="drawer-overlay"></label>
<Sidebar />
</div>
</div>
}
}

View File

@@ -1,63 +1,59 @@
use leptos::prelude::*;
use leptos::wasm_bindgen::JsCast;
use leptos::*;
use leptos::task::spawn_local;
use crate::api;
#[component]
pub fn Sidebar() -> impl IntoView {
let store = use_context::<crate::store::TorrentStore>().expect("store not provided");
let total_count = move || store.torrents.get().len();
let total_count = move || store.torrents.with(|map| map.len());
let downloading_count = move || {
store
.torrents
.get()
.iter()
.filter(|t| t.status == shared::TorrentStatus::Downloading)
.count()
store.torrents.with(|map| {
map.values()
.filter(|t| t.status == shared::TorrentStatus::Downloading)
.count()
})
};
let seeding_count = move || {
store
.torrents
.get()
.iter()
.filter(|t| t.status == shared::TorrentStatus::Seeding)
.count()
store.torrents.with(|map| {
map.values()
.filter(|t| t.status == shared::TorrentStatus::Seeding)
.count()
})
};
let completed_count = move || {
store
.torrents
.get()
.iter()
.filter(|t| {
t.status == shared::TorrentStatus::Seeding
|| (t.status == shared::TorrentStatus::Paused && t.percent_complete >= 100.0)
})
.count()
store.torrents.with(|map| {
map.values()
.filter(|t| {
t.status == shared::TorrentStatus::Seeding
|| (t.status == shared::TorrentStatus::Paused && t.percent_complete >= 100.0)
})
.count()
})
};
let paused_count = move || {
store
.torrents
.get()
.iter()
.filter(|t| t.status == shared::TorrentStatus::Paused)
.count()
store.torrents.with(|map| {
map.values()
.filter(|t| t.status == shared::TorrentStatus::Paused)
.count()
})
};
let inactive_count = move || {
store
.torrents
.get()
.iter()
.filter(|t| {
t.status == shared::TorrentStatus::Paused
|| t.status == shared::TorrentStatus::Error
})
.count()
store.torrents.with(|map| {
map.values()
.filter(|t| {
t.status == shared::TorrentStatus::Paused
|| t.status == shared::TorrentStatus::Error
})
.count()
})
};
let close_drawer = move || {
if let Some(element) = document().get_element_by_id("my-drawer") {
if let Ok(input) = element.dyn_into::<web_sys::HtmlInputElement>() {
input.set_checked(false);
}
// With Shadcn Sheet, this logic might change, but for now we keep DOM manipulation minimal or handled by parent
if let Some(element) = document().get_element_by_id("mobile-sheet-trigger") {
// Logic to close sheet if open (simulated click or state change)
}
};
@@ -67,76 +63,112 @@ pub fn Sidebar() -> impl IntoView {
};
let filter_class = move |f: crate::store::FilterStatus| {
let base = "w-full justify-start gap-2 h-9 px-4 py-2 inline-flex items-center whitespace-nowrap rounded-md text-sm font-medium transition-colors focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring disabled:pointer-events-none disabled:opacity-50";
if store.filter.get() == f {
"active"
format!("{} bg-secondary text-secondary-foreground shadow-sm hover:bg-secondary/80", base)
} else {
""
format!("{} hover:bg-accent hover:text-accent-foreground text-muted-foreground", base)
}
};
let handle_logout = move |_| {
spawn_local(async move {
if api::auth::logout().await.is_ok() {
let window = web_sys::window().expect("window should exist");
let _ = window.location().set_href("/login");
}
});
};
let username = move || {
store.user.get().unwrap_or_else(|| "User".to_string())
};
let first_letter = move || {
username().chars().next().unwrap_or('?').to_uppercase().to_string()
};
view! {
<div class="w-64 h-full flex flex-col bg-base-200 border-r border-base-300" style="padding-top: env(safe-area-inset-top);">
<div class="p-2">
<ul class="menu w-full rounded-box gap-1">
<li class="menu-title text-primary uppercase font-bold px-4">"Filters"</li>
<li>
<button class={move || format!("cursor-pointer {}", filter_class(crate::store::FilterStatus::All))} on:click=move |_| set_filter(crate::store::FilterStatus::All)>
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="w-5 h-5">
<path stroke-linecap="round" stroke-linejoin="round" d="M3.75 6.75h16.5M3.75 12h16.5m-16.5 5.25h16.5" />
</svg>
"All"
<span class="badge badge-sm badge-ghost ml-auto">{total_count}</span>
</button>
</li>
<li>
<button class={move || format!("cursor-pointer {}", filter_class(crate::store::FilterStatus::Downloading))} on:click=move |_| set_filter(crate::store::FilterStatus::Downloading)>
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="w-5 h-5">
<path stroke-linecap="round" stroke-linejoin="round" d="M3 16.5v2.25A2.25 2.25 0 005.25 21h13.5A2.25 2.25 0 0021 18.75V16.5M16.5 12L12 16.5m0 0L7.5 12m4.5 4.5V3" />
</svg>
"Downloading"
<span class="badge badge-sm badge-ghost ml-auto">{downloading_count}</span>
</button>
</li>
<li>
<button class={move || format!("cursor-pointer {}", filter_class(crate::store::FilterStatus::Seeding))} on:click=move |_| set_filter(crate::store::FilterStatus::Seeding)>
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="w-5 h-5">
<path stroke-linecap="round" stroke-linejoin="round" d="M3 16.5v2.25A2.25 2.25 0 005.25 21h13.5A2.25 2.25 0 0021 18.75V16.5m-13.5-9L12 3m0 0l4.5 4.5M12 3v13.5" />
</svg>
"Seeding"
<span class="badge badge-sm badge-ghost ml-auto">{seeding_count}</span>
</button>
</li>
<li>
<button class={move || format!("cursor-pointer {}", filter_class(crate::store::FilterStatus::Completed))} on:click=move |_| set_filter(crate::store::FilterStatus::Completed)>
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="w-5 h-5">
<path stroke-linecap="round" stroke-linejoin="round" d="M9 12.75L11.25 15 15 9.75M21 12a9 9 0 11-18 0 9 9 0 0118 0z" />
</svg>
"Completed"
<span class="badge badge-sm badge-ghost ml-auto">{completed_count}</span>
</button>
</li>
<li>
<button class={move || format!("cursor-pointer {}", filter_class(crate::store::FilterStatus::Paused))} on:click=move |_| set_filter(crate::store::FilterStatus::Paused)>
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="w-5 h-5">
<path stroke-linecap="round" stroke-linejoin="round" d="M15.75 5.25v13.5m-7.5-13.5v13.5" />
</svg>
"Paused"
<span class="badge badge-sm badge-ghost ml-auto">{paused_count}</span>
</button>
</li>
<li>
<button class={move || format!("cursor-pointer {}", filter_class(crate::store::FilterStatus::Inactive))} on:click=move |_| set_filter(crate::store::FilterStatus::Inactive)>
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="w-5 h-5">
<path stroke-linecap="round" stroke-linejoin="round" d="M18.364 18.364A9 9 0 005.636 5.636m12.728 12.728A9 9 0 015.636 5.636m12.728 12.728L5.636 5.636" />
</svg>
"Inactive"
<span class="badge badge-sm badge-ghost ml-auto">{inactive_count}</span>
</button>
</li>
</ul>
<div class="w-64 min-h-[100dvh] flex flex-col bg-card border-r border-border pb-8" style="padding-top: env(safe-area-inset-top);">
<div class="p-4 flex-1 overflow-y-auto">
<div class="mb-4 px-2 text-lg font-semibold tracking-tight text-foreground">
"VibeTorrent"
</div>
<div class="space-y-1">
<h4 class="mb-1 rounded-md px-2 py-1 text-sm font-semibold text-muted-foreground">"Filters"</h4>
<button class={move || filter_class(crate::store::FilterStatus::All)} on:click=move |_| set_filter(crate::store::FilterStatus::All)>
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="w-4 h-4 mr-2">
<path stroke-linecap="round" stroke-linejoin="round" d="M3.75 6.75h16.5M3.75 12h16.5m-16.5 5.25h16.5" />
</svg>
"All"
<span class="ml-auto text-xs font-mono opacity-70">{total_count}</span>
</button>
<button class={move || filter_class(crate::store::FilterStatus::Downloading)} on:click=move |_| set_filter(crate::store::FilterStatus::Downloading)>
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="w-4 h-4 mr-2">
<path stroke-linecap="round" stroke-linejoin="round" d="M3 16.5v2.25A2.25 2.25 0 005.25 21h13.5A2.25 2.25 0 0021 18.75V16.5M16.5 12L12 16.5m0 0L7.5 12m4.5 4.5V3" />
</svg>
"Downloading"
<span class="ml-auto text-xs font-mono opacity-70">{downloading_count}</span>
</button>
<button class={move || filter_class(crate::store::FilterStatus::Seeding)} on:click=move |_| set_filter(crate::store::FilterStatus::Seeding)>
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="w-4 h-4 mr-2">
<path stroke-linecap="round" stroke-linejoin="round" d="M3 16.5v2.25A2.25 2.25 0 005.25 21h13.5A2.25 2.25 0 0021 18.75V16.5m-13.5-9L12 3m0 0l4.5 4.5M12 3v13.5" />
</svg>
"Seeding"
<span class="ml-auto text-xs font-mono opacity-70">{seeding_count}</span>
</button>
<button class={move || filter_class(crate::store::FilterStatus::Completed)} on:click=move |_| set_filter(crate::store::FilterStatus::Completed)>
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="w-4 h-4 mr-2">
<path stroke-linecap="round" stroke-linejoin="round" d="M9 12.75L11.25 15 15 9.75M21 12a9 9 0 11-18 0 9 9 0 0118 0z" />
</svg>
"Completed"
<span class="ml-auto text-xs font-mono opacity-70">{completed_count}</span>
</button>
<button class={move || filter_class(crate::store::FilterStatus::Paused)} on:click=move |_| set_filter(crate::store::FilterStatus::Paused)>
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="w-4 h-4 mr-2">
<path stroke-linecap="round" stroke-linejoin="round" d="M15.75 5.25v13.5m-7.5-13.5v13.5" />
</svg>
"Paused"
<span class="ml-auto text-xs font-mono opacity-70">{paused_count}</span>
</button>
<button class={move || filter_class(crate::store::FilterStatus::Inactive)} on:click=move |_| set_filter(crate::store::FilterStatus::Inactive)>
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="w-4 h-4 mr-2">
<path stroke-linecap="round" stroke-linejoin="round" d="M18.364 18.364A9 9 0 005.636 5.636m12.728 12.728A9 9 0 015.636 5.636m12.728 12.728L5.636 5.636" />
</svg>
"Inactive"
<span class="ml-auto text-xs font-mono opacity-70">{inactive_count}</span>
</button>
</div>
</div>
<div class="p-4 border-t border-border bg-card">
<div class="flex items-center gap-3">
<div class="relative flex h-8 w-8 shrink-0 overflow-hidden rounded-full bg-muted">
<span class="flex h-full w-full items-center justify-center rounded-full bg-primary text-primary-foreground text-xs font-medium">
{first_letter}
</span>
</div>
<div class="flex-1 overflow-hidden">
<div class="font-medium text-sm truncate text-foreground">{username}</div>
<div class="text-[10px] text-muted-foreground truncate">"Online"</div>
</div>
<button
class="inline-flex items-center justify-center whitespace-nowrap rounded-md text-sm font-medium ring-offset-background transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:pointer-events-none disabled:opacity-50 hover:bg-accent hover:text-accent-foreground h-8 w-8 text-destructive"
title="Logout"
on:click=handle_logout
>
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="w-4 h-4">
<path stroke-linecap="round" stroke-linejoin="round" d="M15.75 9V5.25A2.25 2.25 0 0013.5 3h-6a2.25 2.25 0 00-2.25 2.25v13.5A2.25 2.25 0 007.5 21h6a2.25 2.25 0 002.25-2.25V15M12 9l-3 3m0 0l3 3m-3-3h12.75" />
</svg>
</button>
</div>
</div>
</div>
}
}
}

View File

@@ -1,5 +1,9 @@
use leptos::*;
use leptos::prelude::*;
use leptos::html;
use leptos_use::storage::use_local_storage;
use ::codee::string::FromToStringCodec;
use shared::GlobalLimitRequest;
use crate::api;
fn format_bytes(bytes: i64) -> String {
const UNITS: [&str; 6] = ["B", "KB", "MB", "GB", "TB", "PB"];
@@ -26,39 +30,30 @@ pub fn StatusBar() -> impl IntoView {
let store = use_context::<crate::store::TorrentStore>().expect("store not provided");
let stats = store.global_stats;
let initial_theme = if let Some(win) = web_sys::window() {
if let Some(doc) = win.document() {
doc.document_element()
.and_then(|el| el.get_attribute("data-theme"))
.unwrap_or_else(|| "dark".to_string())
} else {
"dark".to_string()
}
} else {
"dark".to_string()
};
let (current_theme, set_current_theme, _) = use_local_storage::<String, FromToStringCodec>("vibetorrent_theme");
let (current_theme, set_current_theme) = create_signal(initial_theme);
// Initialize with default if empty
let current_theme_val = current_theme.get();
if current_theme_val.is_empty() {
set_current_theme.set("dark".to_string());
}
create_effect(move |_| {
if let Some(win) = web_sys::window() {
if let Some(storage) = win.local_storage().ok().flatten() {
if let Ok(Some(stored_theme)) = storage.get_item("vibetorrent_theme") {
let theme = stored_theme.to_lowercase();
set_current_theme.set(theme.clone());
if let Some(doc) = win.document() {
let _ = doc
.document_element()
.unwrap()
.set_attribute("data-theme", &theme);
}
}
// Automatically sync theme to document attribute
Effect::new(move |_| {
let theme = current_theme.get().to_lowercase();
if let Some(doc) = document().document_element() {
let _ = doc.set_attribute("data-theme", &theme);
// Also set class for Shadcn dark mode support
if theme == "dark" || theme == "dracula" || theme == "dim" || theme == "abyss" {
let _ = doc.class_list().add_1("dark");
} else {
let _ = doc.class_list().remove_1("dark");
}
}
});
// Preset limits in bytes/s
let limits: Vec<(i64, &str)> = vec![
let limits: Vec<(i64, &str)> = vec!(
(0, "Unlimited"),
(100 * 1024, "100 KB/s"),
(500 * 1024, "500 KB/s"),
@@ -67,297 +62,206 @@ pub fn StatusBar() -> impl IntoView {
(5 * 1024 * 1024, "5 MB/s"),
(10 * 1024 * 1024, "10 MB/s"),
(20 * 1024 * 1024, "20 MB/s"),
];
);
let set_limit = move |limit_type: &str, val: i64| {
let limit_type = limit_type.to_string();
logging::log!("Setting {} limit to {}", limit_type, val);
log::info!("Setting {} limit to {}", limit_type, val);
spawn_local(async move {
let req_body = if limit_type == "down" {
GlobalLimitRequest {
max_download_rate: Some(val),
max_upload_rate: None,
}
let req = if limit_type == "down" {
GlobalLimitRequest {
max_download_rate: Some(val),
max_upload_rate: None,
}
} else {
GlobalLimitRequest {
max_download_rate: None,
max_upload_rate: Some(val),
}
};
leptos::task::spawn_local(async move {
if let Err(e) = api::settings::set_global_limits(&req).await {
log::error!("Failed to set limit: {:?}", e);
} else {
GlobalLimitRequest {
max_download_rate: None,
max_upload_rate: Some(val),
}
};
let client =
gloo_net::http::Request::post("/api/settings/global-limits").json(&req_body);
match client {
Ok(req) => match req.send().await {
Ok(resp) => {
if !resp.ok() {
logging::error!(
"Failed to set limit: {} {}",
resp.status(),
resp.status_text()
);
} else {
logging::log!("Limit set successfully");
}
}
Err(e) => logging::error!("Network error setting limit: {}", e),
},
Err(e) => logging::error!("Failed to create request: {}", e),
log::info!("Limit set successfully");
}
});
};
// Signal-based dropdown state: 0=none, 1=download, 2=upload, 3=theme
let (active_dropdown, set_active_dropdown) = create_signal(0u8);
// Guard to prevent global close from firing right after toggle opens
let skip_next_close = store_value(false);
// Toggle a specific dropdown
let toggle = move |id: u8| {
let current = active_dropdown.get_untracked();
if current == id {
set_active_dropdown.set(0);
} else {
set_active_dropdown.set(id);
// Mark that the next global close should be skipped
skip_next_close.set_value(true);
}
};
// Close all dropdowns
let close_all = move || {
set_active_dropdown.set(0);
};
let down_details_ref = NodeRef::<html::Details>::new();
let up_details_ref = NodeRef::<html::Details>::new();
let theme_details_ref = NodeRef::<html::Details>::new();
// Close dropdowns when tapping outside — uses click (fires after pointerdown)
let _ = window_event_listener(ev::click, move |_| {
if skip_next_close.get_value() {
skip_next_close.set_value(false);
return;
}
set_active_dropdown.set(0);
});
let close_details = move |node_ref: NodeRef<html::Details>| {
if let Some(el) = node_ref.get_untracked() {
el.set_open(false);
}
};
view! {
<div class="fixed bottom-0 left-0 right-0 h-8 min-h-8 bg-base-200 border-t border-base-300 flex items-center px-4 text-xs gap-4 text-base-content/70 z-[99]">
view! {
<div class="fixed bottom-0 left-0 right-0 h-8 min-h-8 bg-muted border-t border-border flex items-center px-4 text-xs gap-4 text-muted-foreground z-[99] cursor-pointer">
// --- DOWNLOAD SPEED DROPDOWN ---
<div class="relative">
<div
class="flex items-center gap-2 cursor-pointer hover:text-primary transition-colors select-none"
title="Global Download Speed - Click to Set Limit"
on:pointerdown=move |e| {
e.stop_propagation();
toggle(1);
}
>
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="w-4 h-4">
<path stroke-linecap="round" stroke-linejoin="round" d="M19.5 13.5L12 21m0 0l-7.5-7.5M12 21V3" />
</svg>
<span class="font-mono">{move || format_speed(stats.get().down_rate)}</span>
<Show when=move || { stats.get().down_limit.unwrap_or(0) > 0 } fallback=|| ()>
<span class="text-[10px] opacity-60">
{move || format!("(Limit: {})", format_speed(stats.get().down_limit.unwrap_or(0)))}
</span>
</Show>
</div>
<ul
class="absolute bottom-full left-0 z-[100] menu p-2 shadow bg-base-200 rounded-box w-40 mb-2 border border-base-300"
style=move || if active_dropdown.get() == 1 { "display: block" } else { "display: none" }
on:pointerdown=move |e| e.stop_propagation()
>
{
limits.clone().into_iter().map(|(val, label)| {
let is_active = move || {
let current = stats.get().down_limit.unwrap_or(0);
(current - val).abs() < 1024
};
view! {
<li>
<button
class=move || if is_active() { "bg-primary/10 text-primary font-bold text-xs flex justify-between" } else { "text-xs flex justify-between" }
on:pointerdown=move |e| {
e.stop_propagation();
set_limit("down", val);
close_all();
}
>
{label}
<Show when=is_active fallback=|| ()>
<span>""</span>
</Show>
</button>
</li>
}
}).collect::<Vec<_>>()
}
</ul>
</div>
// --- UPLOAD SPEED DROPDOWN ---
<div class="relative">
<div
class="flex items-center gap-2 cursor-pointer hover:text-primary transition-colors select-none"
title="Global Upload Speed - Click to Set Limit"
on:pointerdown=move |e| {
e.stop_propagation();
toggle(2);
}
>
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="w-4 h-4">
<path stroke-linecap="round" stroke-linejoin="round" d="M4.5 10.5L12 3m0 0l7.5 7.5M12 3v18" />
</svg>
<span class="font-mono">{move || format_speed(stats.get().up_rate)}</span>
<Show when=move || { stats.get().up_limit.unwrap_or(0) > 0 } fallback=|| ()>
<span class="text-[10px] opacity-60">
{move || format!("(Limit: {})", format_speed(stats.get().up_limit.unwrap_or(0)))}
</span>
</Show>
</div>
<ul
class="absolute bottom-full left-0 z-[100] menu p-2 shadow bg-base-200 rounded-box w-40 mb-2 border border-base-300"
style=move || if active_dropdown.get() == 2 { "display: block" } else { "display: none" }
on:pointerdown=move |e| e.stop_propagation()
>
{
limits.clone().into_iter().map(|(val, label)| {
let is_active = move || {
let current = stats.get().up_limit.unwrap_or(0);
(current - val).abs() < 1024
};
view! {
<li>
<button
class=move || if is_active() { "bg-primary/10 text-primary font-bold text-xs flex justify-between" } else { "text-xs flex justify-between" }
on:pointerdown=move |e| {
e.stop_propagation();
set_limit("up", val);
close_all();
}
>
{label}
<Show when=is_active fallback=|| ()>
<span>""</span>
</Show>
</button>
</li>
}
}).collect::<Vec<_>>()
}
</ul>
</div>
<div class="ml-auto flex items-center gap-4">
<div class="relative">
<div
class="btn btn-ghost btn-xs btn-square"
title="Change Theme"
on:pointerdown=move |e| {
e.stop_propagation();
toggle(3);
}
>
// --- DOWNLOAD SPEED DROPDOWN ---
<details class="group relative" node_ref=down_details_ref>
<summary class="flex items-center gap-2 cursor-pointer hover:text-foreground transition-colors select-none list-none [&::-webkit-details-marker]:hidden outline-none">
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="w-4 h-4">
<path stroke-linecap="round" stroke-linejoin="round" d="M9.53 16.122a3 3 0 0 0-5.78 1.128 2.25 2.25 0 0 1-2.4 2.245 4.5 4.5 0 0 0 8.4-2.245c0-.399-.078-.78-.22-1.128Zm0 0a15.998 15.998 0 0 0 3.388-1.62m-5.043-.025a15.994 15.994 0 0 1 1.622-3.395m3.42 3.42a15.995 15.995 0 0 0 4.764-4.648l3.876-5.814a1.151 1.151 0 0 0-1.597-1.597L14.146 6.32a15.996 15.996 0 0 0-4.649 4.763m3.42 3.42a6.776 6.776 0 0 0-3.42-3.42" />
<path stroke-linecap="round" stroke-linejoin="round" d="M19.5 13.5L12 21m0 0l-7.5-7.5M12 21V3" />
</svg>
</div>
<span class="font-mono">{move || format_speed(stats.get().down_rate)}</span>
<Show when=move || { stats.get().down_limit.unwrap_or(0) > 0 } fallback=|| ()>
<span class="text-[10px] opacity-60">
{move || format!("(Limit: {})", format_speed(stats.get().down_limit.unwrap_or(0)))}
</span>
</Show>
</summary>
<ul
class="absolute bottom-full right-0 z-[100] menu p-2 shadow bg-base-200 rounded-box w-52 mb-2 border border-base-300 max-h-96 overflow-y-auto"
style=move || if active_dropdown.get() == 3 { "display: block" } else { "display: none" }
on:pointerdown=move |e| e.stop_propagation()
>
{
let themes = vec![
"light", "dark", "dim", "nord", "cupcake", "dracula", "cyberpunk", "emerald", "sunset", "abyss"
];
themes.into_iter().map(|theme| {
view! {
<li>
<button
class=move || if current_theme.get() == theme { "bg-primary/10 text-primary font-bold text-xs capitalize" } else { "text-xs capitalize" }
on:pointerdown=move |e| {
e.stop_propagation();
set_current_theme.set(theme.to_string());
if let Some(win) = web_sys::window() {
if let Some(doc) = win.document() {
let _ = doc.document_element().unwrap().set_attribute("data-theme", theme);
}
if let Some(storage) = win.local_storage().ok().flatten() {
let _ = storage.set_item("vibetorrent_theme", theme);
}
<div class="absolute bottom-full left-0 mb-2 z-[100] min-w-[8rem] overflow-hidden rounded-md border border-border bg-popover p-1 text-popover-foreground shadow-md hidden group-open:block animate-in fade-in-0 zoom-in-95 slide-in-from-bottom-2">
<ul class="w-full">
{
limits.clone().into_iter().map(|(val, label)| {
let is_active = move || {
let current = stats.get().down_limit.unwrap_or(0);
(current - val).abs() < 1024
};
view! {
<li>
<button
class=move || {
let base = "relative flex w-full cursor-default select-none items-center rounded-sm py-1.5 pl-8 pr-2 text-xs outline-none focus:bg-accent focus:text-accent-foreground data-[disabled]:pointer-events-none data-[disabled]:opacity-50 hover:bg-accent hover:text-accent-foreground";
if is_active() { format!("{} bg-accent text-accent-foreground font-medium", base) } else { base.to_string() }
}
close_all();
}
>
{theme}
</button>
</li>
}
}).collect::<Vec<_>>()
}
</ul>
</div>
on:click=move |_| {
set_limit("down", val);
close_details(down_details_ref);
}
>
<span class="absolute left-2 flex h-3.5 w-3.5 items-center justify-center">
<Show when=is_active fallback=|| ()>
<span>""</span>
</Show>
</span>
{label}
</button>
</li>
}
}).collect::<Vec<_>>()
}
</ul>
</div>
</details>
<button
class="btn btn-ghost btn-xs btn-square"
// --- UPLOAD SPEED DROPDOWN ---
<details class="group relative" node_ref=up_details_ref>
<summary class="flex items-center gap-2 cursor-pointer hover:text-foreground transition-colors select-none list-none [&::-webkit-details-marker]:hidden outline-none">
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="w-4 h-4">
<path stroke-linecap="round" stroke-linejoin="round" d="M4.5 10.5L12 3m0 0l7.5 7.5M12 3v18" />
</svg>
<span class="font-mono">{move || format_speed(stats.get().up_rate)}</span>
<Show when=move || { stats.get().up_limit.unwrap_or(0) > 0 } fallback=|| ()>
<span class="text-[10px] opacity-60">
{move || format!("(Limit: {})", format_speed(stats.get().up_limit.unwrap_or(0)))}
</span>
</Show>
</summary>
<div class="absolute bottom-full left-0 mb-2 z-[100] min-w-[8rem] overflow-hidden rounded-md border border-border bg-popover p-1 text-popover-foreground shadow-md hidden group-open:block animate-in fade-in-0 zoom-in-95 slide-in-from-bottom-2">
<ul class="w-full">
{
limits.clone().into_iter().map(|(val, label)| {
let is_active = move || {
let current = stats.get().up_limit.unwrap_or(0);
(current - val).abs() < 1024
};
view! {
<li>
<button
class=move || {
let base = "relative flex w-full cursor-default select-none items-center rounded-sm py-1.5 pl-8 pr-2 text-xs outline-none focus:bg-accent focus:text-accent-foreground data-[disabled]:pointer-events-none data-[disabled]:opacity-50 hover:bg-accent hover:text-accent-foreground";
if is_active() { format!("{} bg-accent text-accent-foreground font-medium", base) } else { base.to_string() }
}
on:click=move |_| {
set_limit("up", val);
close_details(up_details_ref);
}
>
<span class="absolute left-2 flex h-3.5 w-3.5 items-center justify-center">
<Show when=is_active fallback=|| ()>
<span>""</span>
</Show>
</span>
{label}
</button>
</li>
}
}).collect::<Vec<_>>()
}
</ul>
</div>
</details>
<div class="ml-auto flex items-center gap-4">
<details class="group relative" node_ref=theme_details_ref>
<summary class="inline-flex items-center justify-center rounded-md text-sm font-medium ring-offset-background transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:pointer-events-none disabled:opacity-50 hover:bg-accent hover:text-accent-foreground h-7 w-7 cursor-pointer outline-none list-none [&::-webkit-details-marker]:hidden">
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="w-4 h-4">
<path stroke-linecap="round" stroke-linejoin="round" d="M9.53 16.122a3 3 0 0 0-5.78 1.128 2.25 2.25 0 0 1-2.4 2.245 4.5 4.5 0 0 0 8.4-2.245c0-.399-.078-.78-.22-1.128Zm0 0a15.998 15.998 0 0 0 3.388-1.62m-5.043-.025a15.994 15.994 0 0 1 1.622-3.395m3.42 3.42a15.995 15.995 0 0 0 4.764-4.648l3.876-5.814a1.151 1.151 0 0 0-1.597-1.597L14.146 6.32a15.996 15.996 0 0 0-4.649 4.763m3.42 3.42a6.776 6.776 0 0 0-3.42-3.42" />
</svg>
</summary>
<div class="absolute bottom-full right-0 mb-2 z-[100] min-w-[8rem] overflow-hidden rounded-md border border-border bg-popover p-1 text-popover-foreground shadow-md hidden group-open:block animate-in fade-in-0 zoom-in-95 slide-in-from-bottom-2 max-h-96 overflow-y-auto">
<ul class="w-full">
{
let themes = vec![
"light", "dark", "dim", "nord", "cupcake", "dracula", "cyberpunk", "emerald", "sunset", "abyss"
];
themes.into_iter().map(|theme| {
let theme_name = theme.to_string();
let theme_name_for_class = theme_name.clone();
let theme_name_for_onclick = theme_name.clone();
let is_active = move || current_theme.get() == theme_name_for_class;
view! {
<li>
<button
class=move || {
let base = "relative flex w-full cursor-default select-none items-center rounded-sm py-1.5 pl-8 pr-2 text-xs outline-none focus:bg-accent focus:text-accent-foreground data-[disabled]:pointer-events-none data-[disabled]:opacity-50 hover:bg-accent hover:text-accent-foreground capitalize";
if is_active() { format!("{} bg-accent text-accent-foreground font-medium", base) } else { base.to_string() }
}
on:click=move |_| {
set_current_theme.set(theme_name_for_onclick.clone());
close_details(theme_details_ref);
}
>
<span class="absolute left-2 flex h-3.5 w-3.5 items-center justify-center">
<Show when=is_active.clone() fallback=|| ()>
<span>""</span>
</Show>
</span>
{theme_name}
</button> </li>
}
}).collect::<Vec<_>>()
}
</ul>
</div>
</details>
<button
class="inline-flex items-center justify-center rounded-md text-sm font-medium ring-offset-background transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:pointer-events-none disabled:opacity-50 hover:bg-accent hover:text-accent-foreground h-7 w-7"
title="Settings & Notification Permissions"
on:click=move |_| {
// Request push notification permission when settings button is clicked
spawn_local(async {
log::info!("Settings button clicked - requesting push notification permission");
// Check current permission state before requesting
let window = web_sys::window().expect("window should exist");
let _current_perm = js_sys::Reflect::get(&window, &"Notification".into())
.ok()
.and_then(|n| js_sys::Reflect::get(&n, &"permission".into()).ok())
.and_then(|p| p.as_string())
.unwrap_or_default();
// Request push notification permission
leptos::task::spawn_local(async {
// ... existing logic ...
crate::store::subscribe_to_push_notifications().await;
// Check permission after request
let new_perm = js_sys::Reflect::get(&window, &"Notification".into())
.ok()
.and_then(|n| js_sys::Reflect::get(&n, &"permission".into()).ok())
.and_then(|p| p.as_string())
.unwrap_or_default();
if let Some(store) = use_context::<crate::store::TorrentStore>() {
if new_perm == "granted" {
crate::store::show_toast_with_signal(
store.notifications,
shared::NotificationLevel::Success,
"Bildirimler etkinleştirildi! Torrent tamamlandığında bildirim alacaksınız.".to_string(),
);
} else if new_perm == "denied" {
crate::store::show_toast_with_signal(
store.notifications,
shared::NotificationLevel::Error,
"Bildirim izni reddedildi. Tarayıcı ayarlarından izin verebilirsiniz.".to_string(),
);
} else {
crate::store::show_toast_with_signal(
store.notifications,
shared::NotificationLevel::Warning,
"Bildirim izni verilemedi. Açılan izin penceresinde 'İzin Ver' seçeneğini seçin.".to_string(),
);
}
}
// ... existing logic ...
});
}
>
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="w-4 h-4">
<path stroke-linecap="round" stroke-linejoin="round" d="M9.594 3.94c.09-.542.56-.94 1.11-.94h2.593c.55 0 1.02.398 1.11.94l.213 1.281c.063.374.313.686.645.87.074.04.147.083.22.127.324.196.72.257 1.075.124l1.217-.456a1.125 1.125 0 011.37.49l1.296 2.247a1.125 1.125 0 01-.26 1.431l-1.003.827c-.293.24-.438.613-.431.992a6.759 6.759 0 010 .255c-.007.378.138.75.43.99l1.005.828c.424.35.534.954.26 1.43l-1.298 2.247a1.125 1.125 0 01-1.369.491l-1.217-.456c-.355-.133-.75-.072-1.076.124a6.57 6.57 0 01-.22.128c-.331.183-.581.495-.644.869l-.212 1.28c-.09.543-.56.941-1.11.941h-2.594c-.55 0-1.02-.398-1.11-.94l-.213-1.281c-.062-.374-.312-.686-.644-.87a6.52 6.52 0 01-.22-.127c-.325-.196-.72-.257-1.076-.124l-1.217.456a1.125 1.125 0 01-1.369-.49l-1.297-2.247a1.125 1.125 0 01.26-1.431l1.004-.827c.292-.24.437-.613.43-.992a6.932 6.932 0 010-.255c.007-.378-.138-.75-.43-.99l-1.004-.828a1.125 1.125 0 01-.26-1.43l1.297-2.247a1.125 1.125 0 011.37-.491l1.216.456c.356.133.751.072 1.076-.124.072-.044.146-.087.22-.128.332-.183.582-.495.644-.869l.214-1.281z" />
<path stroke-linecap="round" stroke-linejoin="round" d="M9.594 3.94c.09-.542.56-.94 1.11-.94h2.593c.55 0 1.02.398 1.11.94l.213 1.281c.063.374.313.686.645.87.074.04.147.083.22.127.324.196.72.257 1.075.124l1.217-.456a1.125 1.125 0 011.37.49l1.296 2.247a1.125 1.125 0 01-.26 1.431l-1.003.827c-.293.24-.438.613-.431.992a6.759 6.759 0 010 .255c-.007.378.138.75.43.99l1.005.828c.424.35.534.954.26 1.43l-1.298 2.247a1.125 1.125 0 01-1.369.491l-1.217-.456c-.355-.133-.75-.072-1.076.124a6.57 6.57 0 01-.22.128c-.331.183-.581.495-.644.869l-.212 1.28c-.09.543-.56.941-1.11.941h-2.594c-.55 0-1.02-.398-1.11-.94l-.213-1.281c-.062-.374-.312-.686-.644-.87a6.52 6.52 0 01-.22-.127c-.325-.196-.72-.257-1.076-.124l-1.217.456a1.125 1.125 0 01-1.369-.49l-1.297-2.247a1.125 1.125 0 012.6-1.431l1.004-.827c.292-.24.437-.613.43-.992a6.932 6.932 0 010-.255c.007-.378-.138-.75-.43-.99l-1.004-.828a1.125 1.125 0 01-.26-1.43l1.297-2.247a1.125 1.125 0 011.37-.491l1.216.456c.356.133.751.072 1.076-.124.072-.044.146-.087.22-.128.332-.183.582-.495.644-.869l.214-1.281z" />
<path stroke-linecap="round" stroke-linejoin="round" d="M15 12a3 3 0 11-6 0 3 3 0 016 0z" />
</svg>
</button>
</div>
</div>
}
}
}

View File

@@ -1,64 +1,58 @@
use leptos::*;
use leptos::prelude::*;
use crate::components::torrent::add_torrent::AddTorrentDialog;
#[component]
pub fn Toolbar() -> impl IntoView {
let (show_add_modal, set_show_add_modal) = create_signal(false);
let show_add_modal = signal(false);
let store = use_context::<crate::store::TorrentStore>().expect("store not provided");
view! {
<div class="navbar min-h-14 h-auto bg-base-100 p-0" style="padding-top: env(safe-area-inset-top);">
<div class="navbar-start gap-4 px-4">
<label for="my-drawer" class="btn btn-square btn-ghost lg:hidden drawer-button">
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" class="inline-block w-5 h-5 stroke-current"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 6h16M4 12h16M4 18h16"></path></svg>
</label>
<div class="flex gap-2">
<button
class="btn btn-sm btn-primary gap-2 font-normal"
title="Add Magnet Link"
on:click=move |_| set_show_add_modal.set(true)
<div class="flex min-h-14 h-auto items-center border-b border-border bg-background px-4" style="padding-top: env(safe-area-inset-top);">
<div class="flex flex-1 items-center gap-4">
// Mobile Menu Trigger (Sheet Trigger in full impl)
<button id="mobile-sheet-trigger" class="lg:hidden inline-flex items-center justify-center rounded-md text-sm font-medium ring-offset-background transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:pointer-events-none disabled:opacity-50 hover:bg-accent hover:text-accent-foreground h-10 w-10">
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" class="w-5 h-5 stroke-current"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 6h16M4 12h16M4 18h16"></path></svg>
</button>
<div class="flex items-center gap-3">
<button
class="inline-flex items-center justify-center rounded-md text-sm font-medium ring-offset-background transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:pointer-events-none disabled:opacity-50 bg-primary text-primary-foreground hover:bg-primary/90 h-9 px-4 py-2 shadow gap-2"
on:click=move |_| show_add_modal.1.set(true)
>
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="w-4 h-4">
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" class="w-4 h-4 md:w-5 md:h-5">
<path stroke-linecap="round" stroke-linejoin="round" d="M12 4.5v15m7.5-7.5h-15" />
</svg>
"Add Torrent"
<span class="hidden sm:inline">"Add Torrent"</span>
<span class="sm:hidden">"Add"</span>
</button>
</div>
</div>
<div class="navbar-end gap-2 px-4">
<div class="join">
<input
type="text"
placeholder="Search..."
class="input input-sm input-bordered join-item w-full max-w-xs focus:outline-none"
<div class="hidden md:flex items-center justify-center flex-1">
<div class="relative w-full max-w-sm">
<input
type="text"
placeholder="Search..."
class="flex h-9 w-full rounded-md border border-input bg-transparent px-3 py-1 text-sm shadow-sm transition-colors file:border-0 file:bg-transparent file:text-sm file:font-medium placeholder:text-muted-foreground focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring disabled:cursor-not-allowed disabled:opacity-50"
prop:value=move || store.search_query.get()
on:input=move |ev| store.search_query.set(event_target_value(&ev))
on:keydown=move |ev: web_sys::KeyboardEvent| {
if ev.key() == "Escape" {
store.search_query.set(String::new());
}
}
/>
<Show when=move || !store.search_query.get().is_empty()>
<button
class="btn btn-sm btn-ghost join-item border-base-content/20 border-l-0 px-2"
title="Clear Search"
<button
class="absolute right-2 top-1/2 -translate-y-1/2 inline-flex items-center justify-center rounded-full text-xs font-medium hover:bg-muted h-5 w-5 opacity-50 hover:opacity-100 transition-opacity"
on:click=move |_| store.search_query.set(String::new())
>
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="w-4 h-4 opacity-70">
<path stroke-linecap="round" stroke-linejoin="round" d="M6 18L18 6M6 6l12 12" />
</svg>
"×"
</button>
</Show>
</div>
</div>
</div>
<Show when=move || show_add_modal.get()>
<crate::components::torrent::add_torrent::AddTorrentModal on_close=move |_| set_show_add_modal.set(false) />
</Show>
<div class="flex flex-1 justify-end px-4 gap-2">
<Show when=move || show_add_modal.0.get()>
<AddTorrentDialog on_close=Callback::new(move |()| show_add_modal.1.set(false)) />
</Show>
</div>
</div>
}
}
}

View File

@@ -1,5 +1,5 @@
pub mod context_menu;
pub mod layout;
pub mod modal;
pub mod toast;
pub mod torrent;
pub mod auth;

View File

@@ -1,56 +0,0 @@
use leptos::*;
#[component]
pub fn Modal(
#[prop(into)] title: String,
children: Children,
#[prop(into)] on_confirm: Callback<()>,
#[prop(into)] on_cancel: Callback<()>,
#[prop(into)] visible: Signal<bool>,
#[prop(into, default = "Confirm".to_string())] confirm_text: String,
#[prop(into, default = "Cancel".to_string())] cancel_text: String,
#[prop(into, default = false)] is_danger: bool,
) -> impl IntoView {
let title = store_value(title);
// Eagerly render children to a Fragment, which is Clone
let child_view = store_value(children());
let on_confirm = store_value(on_confirm);
let on_cancel = store_value(on_cancel);
let confirm_text = store_value(confirm_text);
let cancel_text = store_value(cancel_text);
view! {
<Show when=move || visible.get() fallback=|| ()>
<div class="fixed inset-0 bg-background/80 backdrop-blur-sm flex items-end md:items-center justify-center z-[200] animate-in fade-in duration-200 sm:p-4">
<div class="bg-card p-6 rounded-t-2xl md:rounded-lg w-full max-w-sm shadow-xl border border-border ring-0 transform transition-all animate-in slide-in-from-bottom-10 md:slide-in-from-bottom-0 md:zoom-in-95">
<h3 class="text-lg font-semibold text-card-foreground mb-4">{title.get_value()}</h3>
<div class="text-muted-foreground mb-6 text-sm">
{child_view.with_value(|c| c.clone())}
</div>
<div class="flex justify-end gap-3">
<button
class="inline-flex items-center justify-center rounded-md text-sm font-medium ring-offset-background transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:pointer-events-none disabled:opacity-50 border border-input bg-background hover:bg-accent hover:text-accent-foreground h-10 px-4 py-2"
on:click=move |_| on_cancel.with_value(|cb| cb.call(()))
>
{cancel_text.get_value()}
</button>
<button
class=move || crate::utils::cn(format!("inline-flex items-center justify-center rounded-md text-sm font-medium ring-offset-background transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:pointer-events-none disabled:opacity-50 h-10 px-4 py-2 {}",
if is_danger { "bg-destructive text-destructive-foreground hover:bg-destructive/90" }
else { "bg-primary text-primary-foreground hover:bg-primary/90" }
))
on:click=move |_| {
logging::log!("Modal: Confirm clicked");
on_confirm.with_value(|cb| cb.call(()))
}
>
{confirm_text.get_value()}
</button>
</div>
</div>
</div>
</Show>
}
}

View File

@@ -1,17 +1,17 @@
use leptos::*;
use leptos::prelude::*;
use shared::NotificationLevel;
// ============================================================================
// Toast Components - DaisyUI Alert Style
// Toast Components - Shadcn Style
// ============================================================================
/// Returns the DaisyUI alert class for the notification level
fn get_alert_class(level: &NotificationLevel) -> &'static str {
/// Returns the Shadcn class for the notification level
fn get_toast_class(level: &NotificationLevel) -> &'static str {
match level {
NotificationLevel::Info => "alert alert-info",
NotificationLevel::Success => "alert alert-success",
NotificationLevel::Warning => "alert alert-warning",
NotificationLevel::Error => "alert alert-error",
NotificationLevel::Info => "group pointer-events-auto relative flex w-full items-center justify-between space-x-4 overflow-hidden rounded-md border p-4 shadow-lg transition-all bg-background text-foreground border-border",
NotificationLevel::Success => "group pointer-events-auto relative flex w-full items-center justify-between space-x-4 overflow-hidden rounded-md border p-4 shadow-lg transition-all bg-background text-foreground border-primary/50 text-primary",
NotificationLevel::Warning => "group pointer-events-auto relative flex w-full items-center justify-between space-x-4 overflow-hidden rounded-md border p-4 shadow-lg transition-all bg-yellow-50 dark:bg-yellow-900/20 text-yellow-600 dark:text-yellow-500 border-yellow-200 dark:border-yellow-900",
NotificationLevel::Error => "group pointer-events-auto relative flex w-full items-center justify-between space-x-4 overflow-hidden rounded-md border p-4 shadow-lg transition-all destructive group border-destructive bg-destructive text-destructive-foreground",
}
}
@@ -21,36 +21,38 @@ fn ToastItem(
level: NotificationLevel,
message: String,
) -> impl IntoView {
let alert_class = get_alert_class(&level);
let toast_class = get_toast_class(&level);
// DaisyUI SVG icons
// Icons
let icon_svg = match level {
NotificationLevel::Info => view! {
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" class="stroke-current shrink-0 w-6 h-6">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M13 16h-1v-4h-1m1-4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z"></path>
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="w-5 h-5 opacity-90">
<path stroke-linecap="round" stroke-linejoin="round" d="M11.25 11.25l.041-.02a.75.75 0 011.063.852l-.708 2.836a.75.75 0 001.063.853l.041-.021M21 12a9 9 0 11-18 0 9 9 0 0118 0zm-9-3.75h.008v.008H12V8.25z" />
</svg>
}.into_view(),
}.into_any(),
NotificationLevel::Success => view! {
<svg xmlns="http://www.w3.org/2000/svg" class="stroke-current shrink-0 h-6 w-6" fill="none" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 12l2 2 4-4m6 2a9 9 0 11-18 0 9 9 0 0118 0z"></path>
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="w-5 h-5 opacity-90">
<path stroke-linecap="round" stroke-linejoin="round" d="M9 12.75L11.25 15 15 9.75M21 12a9 9 0 11-18 0 9 9 0 0118 0z" />
</svg>
}.into_view(),
}.into_any(),
NotificationLevel::Warning => view! {
<svg xmlns="http://www.w3.org/2000/svg" class="stroke-current shrink-0 h-6 w-6" fill="none" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 9v2m0 4h.01m-6.938 4h13.856c1.54 0 2.502-1.667 1.732-3L13.732 4c-.77-1.333-2.694-1.333-3.464 0L3.34 16c-.77 1.333.192 3 1.732 3z"></path>
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="w-5 h-5 opacity-90">
<path stroke-linecap="round" stroke-linejoin="round" d="M12 9v3.75m-9.303 3.376c-.866 1.5.217 3.374 1.948 3.374h14.71c1.73 0 2.813-1.874 1.948-3.374L13.949 3.378c-.866-1.5-3.032-1.5-3.898 0L2.697 16.126zM12 15.75h.007v.008H12v-.008z" />
</svg>
}.into_view(),
}.into_any(),
NotificationLevel::Error => view! {
<svg xmlns="http://www.w3.org/2000/svg" class="stroke-current shrink-0 h-6 w-6" fill="none" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M10 14l2-2m0 0l2-2m-2 2l-2-2m2 2l2 2m7-2a9 9 0 11-18 0 9 9 0 0118 0z"></path>
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="w-5 h-5 opacity-90">
<path stroke-linecap="round" stroke-linejoin="round" d="M12 9v3.75m9-.75a9 9 0 11-18 0 9 9 0 0118 0zm-9 3.75h.008v.008H12v-.008z" />
</svg>
}.into_view(),
}.into_any(),
};
view! {
<div class=alert_class>
{icon_svg}
<span>{message}</span>
<div class=toast_class>
<div class="flex items-center gap-3">
{icon_svg}
<div class="text-sm font-medium">{message}</div>
</div>
</div>
}
}
@@ -63,8 +65,7 @@ pub fn ToastContainer() -> impl IntoView {
view! {
<div
class="toast toast-end toast-bottom"
style="position: fixed; bottom: 20px; right: 20px; z-index: 99999;"
class="fixed bottom-0 right-0 z-[100] flex max-h-screen w-full flex-col-reverse p-4 sm:bottom-0 sm:right-0 sm:top-auto sm:flex-col md:max-w-[420px] gap-2"
>
<For
each=move || notifications.get()

View File

@@ -1,128 +1,108 @@
use leptos::*;
use leptos::html::Dialog;
use crate::store::{show_toast_with_signal, TorrentStore};
use shared::NotificationLevel;
use leptos::prelude::*;
use leptos::html;
use leptos::task::spawn_local;
use crate::store::TorrentStore;
use crate::api;
#[component]
pub fn AddTorrentModal(
#[prop(into)]
pub fn AddTorrentDialog(
on_close: Callback<()>,
) -> impl IntoView {
let store = use_context::<TorrentStore>().expect("TorrentStore not provided");
let notifications = store.notifications;
let dialog_ref = create_node_ref::<Dialog>();
let (uri, set_uri) = create_signal(String::new());
let (is_loading, set_loading) = create_signal(false);
let (error_msg, set_error_msg) = create_signal(Option::<String>::None);
// Effect to open the dialog when the component mounts/renders
create_effect(move |_| {
let dialog_ref = NodeRef::<html::Dialog>::new();
let uri = signal(String::new());
let is_loading = signal(false);
let error_msg = signal(Option::<String>::None);
Effect::new(move |_| {
if let Some(dialog) = dialog_ref.get() {
let _ = dialog.show_modal();
}
});
let handle_submit = move |_| {
let uri_val = uri.get();
let handle_submit = move |ev: web_sys::SubmitEvent| {
ev.prevent_default();
let uri_val = uri.0.get();
if uri_val.is_empty() {
show_toast_with_signal(notifications, NotificationLevel::Warning, "Lütfen bir Magnet URI veya URL girin");
set_error_msg.set(Some("Please enter a Magnet URI or URL".to_string()));
error_msg.1.set(Some("Please enter a Magnet URI or URL".to_string()));
return;
}
set_loading.set(true);
set_error_msg.set(None);
is_loading.1.set(true);
error_msg.1.set(None);
let on_close = on_close.clone();
spawn_local(async move {
let req_body = serde_json::json!({
"uri": uri_val
});
match gloo_net::http::Request::post("/api/torrents/add")
.json(&req_body)
{
Ok(req) => {
match req.send().await {
Ok(resp) => {
if resp.ok() {
logging::log!("Torrent added successfully");
show_toast_with_signal(notifications, NotificationLevel::Success, "Torrent eklendi");
set_loading.set(false);
if let Some(dialog) = dialog_ref.get() {
dialog.close();
}
on_close.call(());
} else {
let status = resp.status();
let text = resp.text().await.unwrap_or_default();
logging::error!("Failed to add torrent: {} - {}", status, text);
show_toast_with_signal(notifications, NotificationLevel::Error, "Torrent eklenemedi");
set_error_msg.set(Some(format!("Error {}: {}", status, text)));
set_loading.set(false);
}
}
Err(e) => {
logging::error!("Network error: {}", e);
show_toast_with_signal(notifications, NotificationLevel::Error, "Bağlantı hatası");
set_error_msg.set(Some(format!("Network Error: {}", e)));
set_loading.set(false);
}
match api::torrent::add(&uri_val).await {
Ok(_) => {
log::info!("Torrent added successfully");
crate::store::show_toast_with_signal(
notifications,
shared::NotificationLevel::Success,
"Torrent başarıyla eklendi"
);
if let Some(dialog) = dialog_ref.get() {
dialog.close();
}
on_close.run(());
}
Err(e) => {
logging::error!("Serialization error: {}", e);
show_toast_with_signal(notifications, NotificationLevel::Error, "İstek hatası");
set_error_msg.set(Some(format!("Request Error: {}", e)));
set_loading.set(false);
log::error!("Failed to add torrent: {:?}", e);
error_msg.1.set(Some(format!("Hata: {:?}", e)));
is_loading.1.set(false);
}
}
});
};
let handle_close = move |_| {
let handle_cancel = move |_| {
if let Some(dialog) = dialog_ref.get() {
dialog.close();
}
on_close.call(());
on_close.run(());
};
view! {
<dialog node_ref=dialog_ref class="modal modal-bottom sm:modal-middle">
<div class="modal-box">
<h3 class="font-bold text-lg">"Add Torrent"</h3>
<p class="py-4">"Enter a Magnet URI or direct URL to a .torrent file."</p>
<p class="py-4 text-sm opacity-70">"Enter a Magnet link or a .torrent file URL."</p>
<div class="form-control w-full">
<input
type="text"
placeholder="magnet:?xt=urn:btih:..."
class="input input-bordered w-full"
prop:value=uri
on:input=move |ev| set_uri.set(event_target_value(&ev))
disabled=is_loading
/>
</div>
<form on:submit=handle_submit>
<div class="form-control w-full">
<input
type="text"
placeholder="magnet:?xt=urn:btih:..."
class="input input-bordered w-full"
prop:value=move || uri.0.get()
on:input=move |ev| uri.1.set(event_target_value(&ev))
disabled=move || is_loading.0.get()
autofocus
/>
</div>
<div class="modal-action">
<button type="button" class="btn btn-ghost" on:click=handle_cancel>"Cancel"</button>
<button type="submit" class="btn btn-primary" disabled=move || is_loading.0.get()>
{move || if is_loading.0.get() {
leptos::either::Either::Left(view! { <span class="loading loading-spinner"></span> "Adding..." })
} else {
leptos::either::Either::Right(view! { "Add" })
}}
</button>
</div>
</form>
<div class="modal-action">
<button class="btn" on:click=handle_close disabled=is_loading>"Cancel"</button>
<button class="btn btn-primary" on:click=handle_submit disabled=is_loading>
{move || if is_loading.get() {
view! { <span class="loading loading-spinner"></span> "Adding..." }.into_view()
} else {
view! { "Add" }.into_view()
}}
</button>
</div>
{move || error_msg.get().map(|msg| view! {
{move || error_msg.0.get().map(|msg| view! {
<div class="text-error text-sm mt-2">{msg}</div>
})}
</div>
<form method="dialog" class="modal-backdrop">
<button type="button" on:click=handle_close>"close"</button>
<button on:click=handle_cancel>"close"</button>
</form>
</dialog>
}
}
}

View File

@@ -1,124 +1,82 @@
use leptos::*;
use wasm_bindgen::closure::Closure;
use wasm_bindgen::JsCast;
use leptos::prelude::*;
use leptos::html;
use leptos::task::spawn_local;
use leptos_use::{use_timeout_fn, UseTimeoutFnReturn};
use crate::store::{get_action_messages, show_toast_with_signal};
use crate::api;
use shared::NotificationLevel;
fn format_bytes(bytes: i64) -> String {
const UNITS: [&str; 6] = ["B", "KB", "MB", "GB", "TB", "PB"];
if bytes < 1024 {
return format!("{} B", bytes);
}
if bytes < 1024 { return format!("{} B", bytes); }
let i = (bytes as f64).log2().div_euclid(10.0) as usize;
format!(
"{:.1} {}",
(bytes as f64) / 1024_f64.powi(i as i32),
UNITS[i]
)
format!("{:.1} {}", (bytes as f64) / 1024_f64.powi(i as i32), UNITS[i])
}
fn format_speed(bytes_per_sec: i64) -> String {
if bytes_per_sec == 0 {
return "0 B/s".to_string();
}
if bytes_per_sec == 0 { return "0 B/s".to_string(); }
format!("{}/s", format_bytes(bytes_per_sec))
}
fn format_duration(seconds: i64) -> String {
if seconds <= 0 {
return "".to_string();
}
if seconds <= 0 { return "".to_string(); }
let days = seconds / 86400;
let hours = (seconds % 86400) / 3600;
let minutes = (seconds % 3600) / 60;
let secs = seconds % 60;
if days > 0 { format!("{}d {}h", days, hours) }
else if hours > 0 { format!("{}h {}m", hours, minutes) }
else if minutes > 0 { format!("{}m {}s", minutes, secs) }
else { format!("{}s", secs) }
}
if days > 0 {
format!("{}d {}h", days, hours)
} else if hours > 0 {
format!("{}h {}m", hours, minutes)
} else if minutes > 0 {
format!("{}m {}s", minutes, secs)
} else {
format!("{}s", secs)
}
fn format_date(timestamp: i64) -> String {
if timestamp <= 0 { return "N/A".to_string(); }
let dt = chrono::DateTime::from_timestamp(timestamp, 0);
match dt { Some(dt) => dt.format("%d/%m/%Y %H:%M").to_string(), None => "N/A".to_string() }
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum SortColumn {
Name,
Size,
Progress,
Status,
DownSpeed,
UpSpeed,
ETA,
Name, Size, Progress, Status, DownSpeed, UpSpeed, ETA, AddedDate,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum SortDirection {
Ascending,
Descending,
}
enum SortDirection { Ascending, Descending }
#[component]
pub fn TorrentTable() -> impl IntoView {
let store = use_context::<crate::store::TorrentStore>().expect("store not provided");
let sort_col = signal(SortColumn::AddedDate);
let sort_dir = signal(SortDirection::Descending);
let sort_col = create_rw_signal(SortColumn::Name);
let sort_dir = create_rw_signal(SortDirection::Ascending);
let filtered_torrents = move || {
let mut torrents = store
.torrents
.get()
.into_iter()
.filter(|t| {
let filter = store.filter.get();
let search = store.search_query.get().to_lowercase();
let matches_filter = match filter {
crate::store::FilterStatus::All => true,
crate::store::FilterStatus::Downloading => {
t.status == shared::TorrentStatus::Downloading
}
crate::store::FilterStatus::Seeding => {
t.status == shared::TorrentStatus::Seeding
}
crate::store::FilterStatus::Completed => {
t.status == shared::TorrentStatus::Seeding
|| (t.status == shared::TorrentStatus::Paused
&& t.percent_complete >= 100.0)
} // Approximate
crate::store::FilterStatus::Paused => t.status == shared::TorrentStatus::Paused,
crate::store::FilterStatus::Inactive => {
t.status == shared::TorrentStatus::Paused
|| t.status == shared::TorrentStatus::Error
}
_ => true,
};
let matches_search = if search.is_empty() {
true
} else {
t.name.to_lowercase().contains(&search)
};
matches_filter && matches_search
})
.collect::<Vec<_>>();
let filtered_hashes = move || {
let torrents_map = store.torrents.get();
let filter = store.filter.get();
let search = store.search_query.get();
let search_lower = search.to_lowercase();
let mut torrents: Vec<shared::Torrent> = torrents_map.values().filter(|t| {
let matches_filter = match filter {
crate::store::FilterStatus::All => true,
crate::store::FilterStatus::Downloading => t.status == shared::TorrentStatus::Downloading,
crate::store::FilterStatus::Seeding => t.status == shared::TorrentStatus::Seeding,
crate::store::FilterStatus::Completed => t.status == shared::TorrentStatus::Seeding || (t.status == shared::TorrentStatus::Paused && t.percent_complete >= 100.0),
crate::store::FilterStatus::Paused => t.status == shared::TorrentStatus::Paused,
crate::store::FilterStatus::Inactive => t.status == shared::TorrentStatus::Paused || t.status == shared::TorrentStatus::Error,
_ => true,
};
let matches_search = if search_lower.is_empty() { true } else { t.name.to_lowercase().contains(&search_lower) };
matches_filter && matches_search
}).cloned().collect();
torrents.sort_by(|a, b| {
let col = sort_col.get();
let dir = sort_dir.get();
let col = sort_col.0.get();
let dir = sort_dir.0.get();
let cmp = match col {
SortColumn::Name => a.name.to_lowercase().cmp(&b.name.to_lowercase()),
SortColumn::Size => a.size.cmp(&b.size),
SortColumn::Progress => a
.percent_complete
.partial_cmp(&b.percent_complete)
.unwrap_or(std::cmp::Ordering::Equal),
SortColumn::Progress => a.percent_complete.partial_cmp(&b.percent_complete).unwrap_or(std::cmp::Ordering::Equal),
SortColumn::Status => format!("{:?}", a.status).cmp(&format!("{:?}", b.status)),
SortColumn::DownSpeed => a.down_rate.cmp(&b.down_rate),
SortColumn::UpSpeed => a.up_rate.cmp(&b.up_rate),
@@ -127,415 +85,329 @@ pub fn TorrentTable() -> impl IntoView {
let b_eta = if b.eta <= 0 { i64::MAX } else { b.eta };
a_eta.cmp(&b_eta)
}
SortColumn::AddedDate => a.added_date.cmp(&b.added_date),
};
if dir == SortDirection::Descending {
cmp.reverse()
} else {
cmp
}
if dir == SortDirection::Descending { cmp.reverse() } else { cmp }
});
torrents
torrents.into_iter().map(|t| t.hash.clone()).collect::<Vec<String>>()
};
let handle_sort = move |col: SortColumn| {
if sort_col.get() == col {
sort_dir.update(|d| {
*d = match d {
SortDirection::Ascending => SortDirection::Descending,
SortDirection::Descending => SortDirection::Ascending,
}
if sort_col.0.get() == col {
sort_dir.1.update(|d| {
*d = match d { SortDirection::Ascending => SortDirection::Descending, SortDirection::Descending => SortDirection::Ascending };
});
} else {
sort_col.set(col);
sort_dir.set(SortDirection::Ascending);
sort_col.1.set(col);
sort_dir.1.set(SortDirection::Ascending);
}
};
// Signal-based sort dropdown for mobile
let (sort_open, set_sort_open) = create_signal(false);
let sort_skip_close = store_value(false);
let _ = window_event_listener(ev::click, move |_| {
if sort_skip_close.get_value() {
sort_skip_close.set_value(false);
return;
}
set_sort_open.set(false);
});
let sort_details_ref = NodeRef::<html::Details>::new();
let sort_arrow = move |col: SortColumn| {
if sort_col.get() == col {
match sort_dir.get() {
SortDirection::Ascending => {
view! { <span class="ml-1 text-xs">""</span> }.into_view()
}
SortDirection::Descending => {
view! { <span class="ml-1 text-xs">""</span> }.into_view()
}
if sort_col.0.get() == col {
match sort_dir.0.get() {
SortDirection::Ascending => view! { <span class="ml-1 text-xs">""</span> }.into_any(),
SortDirection::Descending => view! { <span class="ml-1 text-xs">""</span> }.into_any(),
}
} else {
view! { <span class="ml-1 text-xs opacity-0 group-hover:opacity-50">""</span> }
.into_view()
}
} else { view! { <span class="ml-1 text-xs opacity-0 group-hover:opacity-50">""</span> }.into_any() }
};
let (selected_hash, set_selected_hash) = create_signal(Option::<String>::None);
let (menu_visible, set_menu_visible) = create_signal(false);
let (menu_position, set_menu_position) = create_signal((0, 0));
let selected_hash = signal(Option::<String>::None);
let menu_visible = signal(false);
let menu_position = signal((0, 0));
let handle_context_menu = move |e: web_sys::MouseEvent, hash: String| {
e.prevent_default();
set_menu_position.set((e.client_x(), e.client_y()));
set_selected_hash.set(Some(hash)); // Select on right click too
set_menu_visible.set(true);
menu_position.1.set((e.client_x(), e.client_y()));
selected_hash.1.set(Some(hash));
menu_visible.1.set(true);
};
let on_action = move |(action, hash): (String, String)| {
logging::log!("TorrentTable Action: {} on {}", action, hash);
// Note: Don't close menu here - ContextMenu's on_close handles it
// Closing here would dispose ContextMenu while still in callback chain
// Get action messages for toast (Clean Code: DRY)
let (success_msg, error_msg) = get_action_messages(&action);
let success_msg = success_msg.to_string();
let error_msg = error_msg.to_string();
// Capture notifications signal before async (use_context unavailable in spawn_local)
let (success_msg_str, error_msg_str): (&'static str, &'static str) = get_action_messages(&action);
let success_msg = success_msg_str.to_string();
let error_msg = error_msg_str.to_string();
let notifications = store.notifications;
spawn_local(async move {
let action_req = if action == "delete_with_data" {
"delete_with_data"
} else {
&action
let result = match action.as_str() {
"delete" => api::torrent::delete(&hash).await,
"delete_with_data" => api::torrent::delete_with_data(&hash).await,
"start" => api::torrent::start(&hash).await,
"stop" => api::torrent::stop(&hash).await,
_ => api::torrent::action(&hash, &action).await,
};
let req_body = shared::TorrentActionRequest {
hash: hash.clone(),
action: action_req.to_string(),
};
let client = gloo_net::http::Request::post("/api/torrents/action").json(&req_body);
match client {
Ok(req) => match req.send().await {
Ok(resp) => {
if !resp.ok() {
logging::error!(
"Failed to execute action: {} {}",
resp.status(),
resp.status_text()
);
show_toast_with_signal(notifications, NotificationLevel::Error, error_msg);
} else {
logging::log!("Action {} executed successfully", action);
show_toast_with_signal(notifications, NotificationLevel::Success, success_msg);
}
}
Err(e) => {
logging::error!("Network error executing action: {}", e);
show_toast_with_signal(notifications, NotificationLevel::Error, format!("{}: Bağlantı hatası", error_msg));
}
},
Err(e) => {
logging::error!("Failed to serialize request: {}", e);
show_toast_with_signal(notifications, NotificationLevel::Error, error_msg);
}
match result {
Ok(_) => show_toast_with_signal(notifications, NotificationLevel::Success, success_msg),
Err(e) => show_toast_with_signal(notifications, NotificationLevel::Error, format!("{}: {:?}", error_msg, e)),
}
});
};
view! {
<div class="overflow-x-auto h-full bg-base-100 relative">
<div class="hidden md:block h-full overflow-x-auto">
<table class="table table-sm table-pin-rows w-full max-w-full whitespace-nowrap">
<thead>
<tr class="text-xs uppercase text-base-content/60 border-b border-base-200">
<th class="cursor-pointer hover:bg-base-300 group select-none" on:click=move |_| handle_sort(SortColumn::Name)>
<div class="flex items-center">"Name" {move || sort_arrow(SortColumn::Name)}</div>
</th>
<th class="w-24 cursor-pointer hover:bg-base-300 group select-none" on:click=move |_| handle_sort(SortColumn::Size)>
<div class="flex items-center">"Size" {move || sort_arrow(SortColumn::Size)}</div>
</th>
<th class="w-48 cursor-pointer hover:bg-base-300 group select-none" on:click=move |_| handle_sort(SortColumn::Progress)>
<div class="flex items-center">"Progress" {move || sort_arrow(SortColumn::Progress)}</div>
</th>
<th class="w-24 cursor-pointer hover:bg-base-300 group select-none" on:click=move |_| handle_sort(SortColumn::Status)>
<div class="flex items-center">"Status" {move || sort_arrow(SortColumn::Status)}</div>
</th>
<th class="w-24 cursor-pointer hover:bg-base-300 group select-none" on:click=move |_| handle_sort(SortColumn::DownSpeed)>
<div class="flex items-center">"Down Speed" {move || sort_arrow(SortColumn::DownSpeed)}</div>
</th>
<th class="w-24 cursor-pointer hover:bg-base-300 group select-none" on:click=move |_| handle_sort(SortColumn::UpSpeed)>
<div class="flex items-center">"Up Speed" {move || sort_arrow(SortColumn::UpSpeed)}</div>
</th>
<th class="w-24 cursor-pointer hover:bg-base-300 group select-none" on:click=move |_| handle_sort(SortColumn::ETA)>
<div class="flex items-center">"ETA" {move || sort_arrow(SortColumn::ETA)}</div>
</th>
</tr>
</thead>
<tbody>
{move || filtered_torrents().into_iter().map(|t| {
let progress_class = if t.percent_complete >= 100.0 { "progress-success" } else { "progress-primary" };
let status_str = format!("{:?}", t.status);
let status_class = match t.status {
shared::TorrentStatus::Seeding => "text-success",
shared::TorrentStatus::Downloading => "text-primary",
shared::TorrentStatus::Paused => "text-warning",
shared::TorrentStatus::Error => "text-error",
_ => "text-base-content/50"
};
let t_hash = t.hash.clone();
let t_hash_click = t.hash.clone();
let is_selected_fn = move || {
selected_hash.get() == Some(t_hash.clone())
};
view! {
<tr
class=move || {
let base = "hover border-b border-base-200 select-none";
if is_selected_fn() {
format!("{} bg-primary/10", base)
} else {
base.to_string()
}
}
on:contextmenu={
let t_hash = t_hash_click.clone();
move |e: web_sys::MouseEvent| handle_context_menu(e, t_hash.clone())
}
on:click={
let t_hash = t_hash_click.clone();
move |_| set_selected_hash.set(Some(t_hash.clone()))
}
>
<td class="font-medium truncate max-w-xs" title={t.name.clone()}>
{t.name}
</td>
<td class="opacity-80 font-mono text-[11px]">{format_bytes(t.size)}</td>
<td>
<div class="flex items-center gap-2">
<progress class={format!("progress w-24 {}", progress_class)} value={t.percent_complete} max="100"></progress>
<span class="text-[10px] opacity-70">{format!("{:.1}%", t.percent_complete)}</span>
</div>
</td>
<td class={format!("text-[11px] font-medium {}", status_class)}>{status_str}</td>
<td class="text-right font-mono text-[11px] opacity-80 text-success">{format_speed(t.down_rate)}</td>
<td class="text-right font-mono text-[11px] opacity-80 text-primary">{format_speed(t.up_rate)}</td>
<td class="text-right font-mono text-[11px] opacity-80">{format_duration(t.eta)}</td>
</tr>
}
}).collect::<Vec<_>>()}
</tbody>
</table>
</div>
<div class="md:hidden flex flex-col h-full bg-base-200">
<div class="px-3 py-2 border-b border-base-200 flex justify-between items-center bg-base-100/95 backdrop-blur z-10 shrink-0">
<span class="text-xs font-bold opacity-50 uppercase tracking-wider">"Torrents"</span>
<div class="relative">
<div
role="button"
class="btn btn-ghost btn-xs gap-1 opacity-70 font-normal"
on:pointerdown=move |e| {
e.stop_propagation();
let cur = sort_open.get_untracked();
if cur {
set_sort_open.set(false);
} else {
set_sort_open.set(true);
sort_skip_close.set_value(true);
}
}
>
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="w-4 h-4">
<path stroke-linecap="round" stroke-linejoin="round" d="M3 7.5L7.5 3m0 0L12 7.5M7.5 3v13.5m13.5 0L16.5 21m0 0L12 16.5m4.5 4.5V7.5" />
</svg>
"Sort"
</div>
<ul
class="absolute top-full right-0 z-[100] menu p-2 shadow bg-base-100 rounded-box w-48 mt-1 border border-base-200 text-xs"
style=move || if sort_open.get() { "display: block" } else { "display: none" }
on:pointerdown=move |e| e.stop_propagation()
>
<li class="menu-title px-2 py-1 opacity-50 text-[10px] uppercase font-bold">"Sort By"</li>
{
let columns = vec![
(SortColumn::Name, "Name"),
(SortColumn::Size, "Size"),
(SortColumn::Progress, "Progress"),
(SortColumn::Status, "Status"),
(SortColumn::DownSpeed, "Down Speed"),
(SortColumn::UpSpeed, "Up Speed"),
(SortColumn::ETA, "ETA"),
];
columns.into_iter().map(|(col, label)| {
let is_active = move || sort_col.get() == col;
let current_dir = move || sort_dir.get();
view! {
<li>
<button
class=move || if is_active() { "bg-primary/10 text-primary font-bold flex justify-between" } else { "flex justify-between" }
on:pointerdown=move |e| {
e.stop_propagation();
handle_sort(col);
set_sort_open.set(false);
}
>
{label}
<Show when=is_active fallback=|| ()>
<span class="opacity-70 text-[10px]">
{move || match current_dir() {
SortDirection::Ascending => "",
SortDirection::Descending => "",
}}
</span>
</Show>
</button>
</li>
}
}).collect::<Vec<_>>()
}
</ul>
<div class="h-full bg-background relative flex flex-col">
// --- DESKTOP VIEW ---
<div class="hidden md:flex flex-col h-full overflow-hidden">
// Header
<div class="flex items-center text-xs uppercase text-muted-foreground border-b border-border bg-muted/50 h-9 shrink-0 px-2 font-medium">
<div class="flex-1 px-2 cursor-pointer hover:text-foreground group select-none flex items-center" on:click=move |_| handle_sort(SortColumn::Name)>
"Name" {move || sort_arrow(SortColumn::Name)}
</div>
<div class="w-24 px-2 cursor-pointer hover:text-foreground group select-none flex items-center" on:click=move |_| handle_sort(SortColumn::Size)>
"Size" {move || sort_arrow(SortColumn::Size)}
</div>
<div class="w-48 px-2 cursor-pointer hover:text-foreground group select-none flex items-center" on:click=move |_| handle_sort(SortColumn::Progress)>
"Progress" {move || sort_arrow(SortColumn::Progress)}
</div>
<div class="w-24 px-2 cursor-pointer hover:text-foreground group select-none flex items-center" on:click=move |_| handle_sort(SortColumn::Status)>
"Status" {move || sort_arrow(SortColumn::Status)}
</div>
<div class="w-24 px-2 cursor-pointer hover:text-foreground group select-none flex items-center" on:click=move |_| handle_sort(SortColumn::DownSpeed)>
"DL Speed" {move || sort_arrow(SortColumn::DownSpeed)}
</div>
<div class="w-24 px-2 cursor-pointer hover:text-foreground group select-none flex items-center" on:click=move |_| handle_sort(SortColumn::UpSpeed)>
"Up Speed" {move || sort_arrow(SortColumn::UpSpeed)}
</div>
<div class="w-24 px-2 cursor-pointer hover:text-foreground group select-none flex items-center" on:click=move |_| handle_sort(SortColumn::ETA)>
"ETA" {move || sort_arrow(SortColumn::ETA)}
</div>
<div class="w-32 px-2 cursor-pointer hover:text-foreground group select-none flex items-center" on:click=move |_| handle_sort(SortColumn::AddedDate)>
"Date" {move || sort_arrow(SortColumn::AddedDate)}
</div>
</div>
<div class="overflow-y-auto p-3 pb-20 flex-1 grid grid-cols-1 content-start gap-3">
{move || filtered_torrents().into_iter().map(|t| {
let progress_class = if t.percent_complete >= 100.0 { "progress-success" } else { "progress-primary" };
let status_str = format!("{:?}", t.status);
let status_badge_class = match t.status {
shared::TorrentStatus::Seeding => "badge-success badge-soft",
shared::TorrentStatus::Downloading => "badge-primary badge-soft",
shared::TorrentStatus::Paused => "badge-warning badge-soft",
shared::TorrentStatus::Error => "badge-error badge-soft",
_ => "badge-ghost"
};
let _t_hash = t.hash.clone();
let t_hash_click = t.hash.clone();
let (timer_id, set_timer_id) = create_signal(Option::<i32>::None);
let t_hash_long = t.hash.clone();
let clear_timer = move || {
if let Some(id) = timer_id.get_untracked() {
window().clear_timeout_with_handle(id);
set_timer_id.set(None);
}
};
let handle_touchstart = {
let t_hash = t_hash_long.clone();
move |e: web_sys::TouchEvent| {
clear_timer();
if let Some(touch) = e.touches().get(0) {
let x = touch.client_x();
let y = touch.client_y();
let hash = t_hash.clone();
let closure = Closure::wrap(Box::new(move || {
set_menu_position.set((x, y));
set_selected_hash.set(Some(hash.clone()));
set_menu_visible.set(true);
// Haptic feedback (iOS Safari doesn't support vibrate)
let navigator = window().navigator();
if js_sys::Reflect::has(&navigator, &wasm_bindgen::JsValue::from_str("vibrate")).unwrap_or(false) {
let _ = navigator.vibrate_with_duration(50);
}
}) as Box<dyn Fn()>);
let id = window()
.set_timeout_with_callback_and_timeout_and_arguments_0(
closure.as_ref().unchecked_ref(),
600
)
.unwrap_or(0);
closure.forget();
set_timer_id.set(Some(id));
// Regular List (Standard For loop)
<div class="flex-1 overflow-y-auto min-h-0">
<For each=filtered_hashes key=|hash| hash.clone() children={
let handle_context_menu = handle_context_menu.clone();
move |hash| {
view! {
<TorrentRow
hash=hash.clone()
selected_hash=selected_hash.0
set_selected_hash=selected_hash.1
on_context_menu=handle_context_menu.clone()
/>
}
}
};
let handle_touchmove = move |_| {
clear_timer();
};
let handle_touchend = move |_| {
clear_timer();
};
view! {
<div
class=move || {
"card card-compact bg-base-100 shadow-sm border border-base-200 transition-transform active:scale-[0.99] select-none"
}
style="user-select: none; -webkit-user-select: none; -webkit-touch-callout: none;"
on:contextmenu={
let t_hash = t.hash.clone();
move |e: web_sys::MouseEvent| handle_context_menu(e, t_hash.clone())
}
on:click={
let t_hash = t_hash_click.clone();
move |_| set_selected_hash.set(Some(t_hash.clone()))
}
on:touchstart=handle_touchstart
on:touchmove=handle_touchmove
on:touchend=handle_touchend
on:touchcancel=handle_touchend
>
<div class="card-body gap-3">
<div class="flex justify-between items-start gap-2">
<h3 class="font-medium text-sm line-clamp-2 leading-tight">{t.name}</h3>
<div class={format!("badge badge-xs text-[10px] whitespace-nowrap {}", status_badge_class)}>
{status_str}
</div>
</div>
<div class="flex flex-col gap-1">
<div class="flex justify-between text-[10px] opacity-70">
<span>{format_bytes(t.size)}</span>
<span>{format!("{:.1}%", t.percent_complete)}</span>
</div>
<progress class={format!("progress w-full h-1.5 {}", progress_class)} value={t.percent_complete} max="100"></progress>
</div>
<div class="grid grid-cols-3 gap-2 text-[10px] font-mono opacity-80 pt-1 border-t border-base-200/50">
<div class="flex flex-col">
<span class="text-[9px] opacity-60 uppercase">"Down"</span>
<span class="text-success">{format_speed(t.down_rate)}</span>
</div>
<div class="flex flex-col text-center border-l border-r border-base-200/50">
<span class="text-[9px] opacity-60 uppercase">"Up"</span>
<span class="text-primary">{format_speed(t.up_rate)}</span>
</div>
<div class="flex flex-col text-right">
<span class="text-[9px] opacity-60 uppercase">"ETA"</span>
<span>{format_duration(t.eta)}</span>
</div>
</div>
</div>
</div>
}
}).collect::<Vec<_>>()}
} />
</div>
</div>
<Show when=move || menu_visible.get() fallback=|| ()>
<crate::components::context_menu::ContextMenu
visible=true
position=menu_position.get()
torrent_hash=selected_hash.get().unwrap_or_default()
on_close=Callback::from(move |_| set_menu_visible.set(false))
on_action=Callback::from(on_action)
/>
// --- MOBILE VIEW ---
<div class="md:hidden flex flex-col h-full bg-muted/10 relative overflow-hidden">
<div class="px-3 py-2 border-b border-border flex justify-between items-center bg-background/95 backdrop-blur z-10 shrink-0">
<span class="text-xs font-bold opacity-50 uppercase tracking-wider text-muted-foreground">"Torrents"</span>
<details class="dropdown dropdown-end" node_ref=sort_details_ref>
<summary class="btn btn-ghost btn-xs gap-1 opacity-70 font-normal list-none [&::-webkit-details-marker]:hidden cursor-pointer hover:bg-accent hover:text-accent-foreground rounded-sm px-2 py-1 flex items-center">
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="w-4 h-4 pointer-events-none"><path stroke-linecap="round" stroke-linejoin="round" d="M3 7.5L7.5 3m0 0L12 7.5M7.5 3v13.5m13.5 0L16.5 21m0 0L12 16.5m4.5 4.5V7.5" /></svg>
<span class="pointer-events-none">"Sort"</span>
</summary>
<div class="dropdown-content z-[100] absolute right-0 top-full mt-1 min-w-[10rem] overflow-hidden rounded-md border border-border bg-popover p-1 text-popover-foreground shadow-md">
<div class="px-2 py-1.5 text-xs font-semibold text-muted-foreground">"Sort By"</div>
<ul class="w-full">
{
let columns = vec![(SortColumn::Name, "Name"), (SortColumn::Size, "Size"), (SortColumn::Progress, "Progress"), (SortColumn::Status, "Status"), (SortColumn::DownSpeed, "DL Speed"), (SortColumn::UpSpeed, "Up Speed"), (SortColumn::ETA, "ETA"), (SortColumn::AddedDate, "Date")];
columns.into_iter().map(|(col, label)| {
let is_active = move || sort_col.0.get() == col;
view! {
<li>
<button type="button" class=move || if is_active() { "bg-accent text-accent-foreground font-medium flex w-full cursor-default select-none items-center rounded-sm py-1.5 px-2 text-xs outline-none" } else { "flex w-full cursor-default select-none items-center rounded-sm py-1.5 px-2 text-xs outline-none hover:bg-accent hover:text-accent-foreground" } on:click=move |_| { handle_sort(col); if let Some(el) = sort_details_ref.get() { el.set_open(false); } }>
{label}
<Show when=is_active fallback=|| ()><span class="ml-auto opacity-70 text-[10px]">{move || match sort_dir.0.get() { SortDirection::Ascending => "", SortDirection::Descending => "" }}</span></Show>
</button>
</li>
}
}).collect::<Vec<_>>()
}
</ul>
</div>
</details>
</div>
<div class="flex-1 overflow-y-auto p-3 min-h-0">
<For each=filtered_hashes key=|hash| hash.clone() children={
let handle_context_menu = handle_context_menu.clone();
let menu_pos_setter = menu_position.1.clone();
let menu_vis_setter = menu_visible.1.clone();
move |hash| {
view! {
<div class="pb-3">
<TorrentCard
hash=hash.clone()
selected_hash=selected_hash.0
set_selected_hash=selected_hash.1
set_menu_position=menu_pos_setter
set_menu_visible=menu_vis_setter
on_context_menu=handle_context_menu.clone()
/>
</div>
}
}
} />
</div>
</div>
<Show when=move || menu_visible.0.get() fallback=|| ()>
<crate::components::context_menu::ContextMenu position=menu_position.0.get() torrent_hash=selected_hash.0.get().unwrap_or_default() on_close=Callback::new(move |()| menu_visible.1.set(false)) on_action=Callback::new(move |args| on_action(args)) />
</Show>
</div>
}
}
#[component]
fn TorrentRow(
hash: String,
selected_hash: ReadSignal<Option<String>>,
set_selected_hash: WriteSignal<Option<String>>,
on_context_menu: impl Fn(web_sys::MouseEvent, String) + 'static + Clone + Send + Sync,
) -> impl IntoView {
let store = use_context::<crate::store::TorrentStore>().expect("store not provided");
let h = hash.clone();
let torrent = Memo::new(move |_| store.torrents.with(|map| map.get(&h).cloned()));
view! {
<Show when=move || torrent.get().is_some() fallback=|| ()>
{
let on_context_menu = on_context_menu.clone();
let hash = hash.clone();
move || {
let t = torrent.get().unwrap();
let t_hash = hash.clone();
let t_name = t.name.clone();
let status_color = match t.status { shared::TorrentStatus::Seeding => "text-green-500", shared::TorrentStatus::Downloading => "text-blue-500", shared::TorrentStatus::Paused => "text-yellow-500", shared::TorrentStatus::Error => "text-red-500", _ => "text-muted-foreground" };
let selected_hash_clone = selected_hash.clone();
let t_hash_row = t_hash.clone();
view! {
<div
class=move || {
let base = "flex items-center text-sm hover:bg-muted/50 border-b border-border h-[48px] px-2 select-none cursor-pointer";
if selected_hash_clone.get() == Some(t_hash_row.clone()) { format!("{} bg-muted", base) } else { base.to_string() }
}
on:contextmenu={
let t_hash = t_hash.clone();
let on_context_menu = on_context_menu.clone();
move |e: web_sys::MouseEvent| on_context_menu(e, t_hash.clone())
}
on:click={
let t_hash = t_hash.clone();
let set_selected_hash = set_selected_hash.clone();
move |_| set_selected_hash.set(Some(t_hash.clone()))
}
>
<div class="flex-1 min-w-0 px-2 font-medium truncate" title=t_name.clone()>{t_name.clone()}</div>
<div class="w-24 px-2 font-mono text-xs text-muted-foreground">{format_bytes(t.size)}</div>
<div class="w-48 px-2">
<div class="flex items-center gap-2">
<div class="h-2 w-full bg-secondary rounded-full overflow-hidden">
<div class="h-full bg-primary transition-all duration-500" style=format!("width: {}%", t.percent_complete)></div>
</div>
<span class="text-[10px] text-muted-foreground w-10 text-right">{format!("{:.1}%", t.percent_complete)}</span>
</div>
</div>
<div class={format!("w-24 px-2 text-xs font-medium {}", status_color)}>{format!("{:?}", t.status)}</div>
<div class="w-24 px-2 text-right font-mono text-xs text-green-600 dark:text-green-500">{format_speed(t.down_rate)}</div>
<div class="w-24 px-2 text-right font-mono text-xs text-blue-600 dark:text-blue-500">{format_speed(t.up_rate)}</div>
<div class="w-24 px-2 text-right font-mono text-xs text-muted-foreground">{format_duration(t.eta)}</div>
<div class="w-32 px-2 text-right font-mono text-xs text-muted-foreground">{format_date(t.added_date)}</div>
</div>
}
}
}
</Show>
}
}
#[component]
fn TorrentCard(
hash: String,
selected_hash: ReadSignal<Option<String>>,
set_selected_hash: WriteSignal<Option<String>>,
set_menu_position: WriteSignal<(i32, i32)>,
set_menu_visible: WriteSignal<bool>,
on_context_menu: impl Fn(web_sys::MouseEvent, String) + 'static + Clone + Send + Sync,
) -> impl IntoView {
let store = use_context::<crate::store::TorrentStore>().expect("store not provided");
let h = hash.clone();
let torrent = Memo::new(move |_| store.torrents.with(|map| map.get(&h).cloned()));
view! {
<Show when=move || torrent.get().is_some() fallback=|| ()>
{
let hash = hash.clone();
let on_context_menu = on_context_menu.clone();
move || {
let t = torrent.get().unwrap();
let t_hash = hash.clone();
let t_name = t.name.clone();
let status_badge_class = match t.status { shared::TorrentStatus::Seeding => "bg-green-100 text-green-700 dark:bg-green-900/30 dark:text-green-400 border-green-200 dark:border-green-800", shared::TorrentStatus::Downloading => "bg-blue-100 text-blue-700 dark:bg-blue-900/30 dark:text-blue-400 border-blue-200 dark:border-blue-800", shared::TorrentStatus::Paused => "bg-yellow-100 text-yellow-700 dark:bg-yellow-900/30 dark:text-yellow-400 border-yellow-200 dark:border-yellow-800", shared::TorrentStatus::Error => "bg-red-100 text-red-700 dark:bg-red-900/30 dark:text-red-400 border-red-200 dark:border-red-800", _ => "bg-muted text-muted-foreground" };
let t_hash_long = t_hash.clone();
let set_menu_position = set_menu_position.clone();
let set_selected_hash = set_selected_hash.clone();
let set_menu_visible = set_menu_visible.clone();
let UseTimeoutFnReturn { start, .. } = use_timeout_fn(
move |pos: (i32, i32)| {
set_menu_position.set(pos);
set_selected_hash.set(Some(t_hash_long.clone()));
set_menu_visible.set(true);
let _ = window().navigator().vibrate_with_duration(50);
},
600.0,
);
let selected_hash_clone = selected_hash.clone();
let t_hash_card = t_hash.clone();
view! {
<div
class=move || {
let base = "bg-card text-card-foreground rounded-lg border border-border shadow-sm select-none cursor-pointer h-full";
if selected_hash_clone.get() == Some(t_hash_card.clone()) { format!("{} ring-2 ring-primary ring-inset", base) } else { base.to_string() }
}
on:contextmenu={
let t_hash = t_hash.clone();
let on_context_menu = on_context_menu.clone();
move |e: web_sys::MouseEvent| on_context_menu(e, t_hash.clone())
}
on:click={
let t_hash = t_hash.clone();
let set_selected_hash = set_selected_hash.clone();
move |_| set_selected_hash.set(Some(t_hash.clone()))
}
on:touchstart={
let start = start.clone();
move |e: web_sys::TouchEvent| if let Some(touch) = e.touches().get(0) { start((touch.client_x(), touch.client_y())); }
}
>
<div class="p-3 gap-3 flex flex-col h-full justify-between">
<div class="flex justify-between items-start gap-2">
<h3 class="font-medium text-sm line-clamp-2 leading-tight">{t_name.clone()}</h3>
<div class={format!("inline-flex items-center rounded-full border px-2.5 py-0.5 text-xs font-semibold transition-colors focus:outline-none focus:ring-2 focus:ring-ring focus:ring-offset-2 {}", status_badge_class)}>{format!("{:?}", t.status)}</div>
</div>
<div class="flex flex-col gap-1">
<div class="flex justify-between text-[10px] text-muted-foreground">
<span>{format_bytes(t.size)}</span>
<span>{format!("{:.1}%", t.percent_complete)}</span>
</div>
<div class="h-1.5 w-full bg-secondary rounded-full overflow-hidden">
<div class="h-full bg-primary transition-all duration-500" style=format!("width: {}%", t.percent_complete)></div>
</div>
</div>
<div class="grid grid-cols-4 gap-2 text-[10px] font-mono text-muted-foreground pt-1 border-t border-border/50">
<div class="flex flex-col text-blue-600 dark:text-blue-500"><span>"DL"</span><span>{format_speed(t.down_rate)}</span></div>
<div class="flex flex-col text-green-600 dark:text-green-500"><span>"UP"</span><span>{format_speed(t.up_rate)}</span></div>
<div class="flex flex-col"><span>"ETA"</span><span>{format_duration(t.eta)}</span></div>
<div class="flex flex-col text-right"><span>"DATE"</span><span>{format_date(t.added_date)}</span></div>
</div>
</div>
</div>
}
}
}
</Show>
}
}

View File

@@ -1,21 +1,27 @@
#![recursion_limit = "256"]
mod app;
// mod models; // Removed
mod components;
pub mod utils;
pub mod store;
pub mod api;
use leptos::*;
use leptos::prelude::*;
use leptos::mount::mount_to_body;
use wasm_bindgen::prelude::*;
use app::App;
#[wasm_bindgen(start)]
pub fn main() {
console_error_panic_hook::set_once();
console_log::init_with_level(log::Level::Debug).unwrap();
console_log::init_with_level(log::Level::Debug)
.expect("Failed to initialize logging");
let window = web_sys::window().unwrap();
let document = window.document().unwrap();
let body = document.body().unwrap();
let window = web_sys::window()
.expect("Failed to access window - browser may not be fully loaded");
let document = window.document()
.expect("Failed to access document");
let body = document.body()
.expect("Failed to access document body");
// Add app-loaded class to body to hide spinner via CSS
let _ = body.class_list().add_1("app-loaded");

View File

@@ -1,7 +1,11 @@
use futures::StreamExt;
use gloo_net::eventsource::futures::EventSource;
use leptos::*;
use leptos::prelude::*;
use leptos::task::spawn_local;
use shared::{AppEvent, GlobalStats, NotificationLevel, SystemNotification, Torrent};
use std::collections::HashMap;
use struct_patch::traits::Patch;
use base64::{Engine as _, engine::general_purpose::STANDARD as BASE64};
#[derive(Clone, Debug, PartialEq)]
pub struct NotificationItem {
@@ -9,13 +13,6 @@ pub struct NotificationItem {
pub notification: SystemNotification,
}
// ============================================================================
// Toast Helper Functions (Clean Code: Single Responsibility)
// ============================================================================
/// Shows a toast notification using a direct signal reference.
/// Use this version inside async blocks (spawn_local) where use_context is unavailable.
/// Auto-removes after 5 seconds.
pub fn show_toast_with_signal(
notifications: RwSignal<Vec<NotificationItem>>,
level: NotificationLevel,
@@ -27,11 +24,10 @@ pub fn show_toast_with_signal(
message: message.into(),
};
let item = NotificationItem { id, notification };
notifications.update(|list| list.push(item));
// Auto-remove after 5 seconds
let _ = set_timeout(
leptos::prelude::set_timeout(
move || {
notifications.update(|list| list.retain(|i| i.id != id));
},
@@ -39,41 +35,15 @@ pub fn show_toast_with_signal(
);
}
/// Shows a toast notification with the given level and message.
/// Only works within reactive scope (components, effects). For async, use show_toast_with_signal.
/// Auto-removes after 5 seconds.
pub fn show_toast(level: NotificationLevel, message: impl Into<String>) {
if let Some(store) = use_context::<TorrentStore>() {
show_toast_with_signal(store.notifications, level, message);
}
}
/// Convenience function for success toasts (reactive scope only)
pub fn toast_success(message: impl Into<String>) {
show_toast(NotificationLevel::Success, message);
}
pub fn toast_success(message: impl Into<String>) { show_toast(NotificationLevel::Success, message); }
pub fn toast_error(message: impl Into<String>) { show_toast(NotificationLevel::Error, message); }
/// Convenience function for error toasts (reactive scope only)
pub fn toast_error(message: impl Into<String>) {
show_toast(NotificationLevel::Error, message);
}
/// Convenience function for info toasts (reactive scope only)
pub fn toast_info(message: impl Into<String>) {
show_toast(NotificationLevel::Info, message);
}
/// Convenience function for warning toasts (reactive scope only)
pub fn toast_warning(message: impl Into<String>) {
show_toast(NotificationLevel::Warning, message);
}
// ============================================================================
// Action Message Mapping (Clean Code: DRY Principle)
// ============================================================================
/// Maps torrent action strings to user-friendly Turkish messages.
/// Returns (success_message, error_message)
pub fn get_action_messages(action: &str) -> (&'static str, &'static str) {
match action {
"start" => ("Torrent başlatıldı", "Torrent başlatılamadı"),
@@ -88,520 +58,128 @@ pub fn get_action_messages(action: &str) -> (&'static str, &'static str) {
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum FilterStatus {
All,
Downloading,
Seeding,
Completed,
Paused,
Inactive,
Active,
Error,
}
impl FilterStatus {
pub fn as_str(&self) -> &'static str {
match self {
FilterStatus::All => "All",
FilterStatus::Downloading => "Downloading",
FilterStatus::Seeding => "Seeding",
FilterStatus::Completed => "Completed",
FilterStatus::Paused => "Paused",
FilterStatus::Inactive => "Inactive",
FilterStatus::Active => "Active",
FilterStatus::Error => "Error",
}
}
All, Downloading, Seeding, Completed, Paused, Inactive, Active, Error,
}
#[derive(Clone, Copy, Debug)]
pub struct TorrentStore {
pub torrents: RwSignal<Vec<Torrent>>,
pub torrents: RwSignal<HashMap<String, Torrent>>,
pub filter: RwSignal<FilterStatus>,
pub search_query: RwSignal<String>,
pub global_stats: RwSignal<GlobalStats>,
pub notifications: RwSignal<Vec<NotificationItem>>,
pub user: RwSignal<Option<String>>,
}
pub fn provide_torrent_store() {
let torrents = create_rw_signal(Vec::<Torrent>::new());
let filter = create_rw_signal(FilterStatus::All);
let search_query = create_rw_signal(String::new());
let global_stats = create_rw_signal(GlobalStats::default());
let notifications = create_rw_signal(Vec::<NotificationItem>::new());
let torrents = RwSignal::new(HashMap::new());
let filter = RwSignal::new(FilterStatus::All);
let search_query = RwSignal::new(String::new());
let global_stats = RwSignal::new(GlobalStats::default());
let notifications = RwSignal::new(Vec::<NotificationItem>::new());
let user = RwSignal::new(Option::<String>::None);
let store = TorrentStore {
torrents,
filter,
search_query,
global_stats,
notifications,
};
let show_browser_notification = crate::utils::notification::use_app_notification();
let store = TorrentStore { torrents, filter, search_query, global_stats, notifications, user };
provide_context(store);
// Initialize SSE connection with auto-reconnect
create_effect(move |_| {
spawn_local(async move {
let mut backoff_ms: u32 = 1000; // Start with 1 second
let max_backoff_ms: u32 = 30000; // Max 30 seconds
let mut was_connected = false;
let mut disconnect_notified = false; // Track if we already showed disconnect toast
let mut got_first_message = false; // Only count as "connected" after receiving data
let notifications_for_sse = notifications;
let global_stats_for_sse = global_stats;
let torrents_for_sse = torrents;
let show_browser_notification = show_browser_notification.clone();
loop {
let es_result = EventSource::new("/api/events");
match es_result {
Ok(mut es) => {
match es.subscribe("message") {
Ok(mut stream) => {
// Don't show "connected" toast yet - wait for first real message
got_first_message = false;
spawn_local(async move {
let mut backoff_ms: u32 = 1000;
let mut was_connected = false;
let mut disconnect_notified = false;
// Process messages
while let Some(Ok((_, msg))) = stream.next().await {
// First successful message = truly connected
if !got_first_message {
got_first_message = true;
backoff_ms = 1000; // Reset backoff on real data
if was_connected && disconnect_notified {
// We were previously connected, lost connection, and now truly reconnected
show_toast_with_signal(
notifications,
NotificationLevel::Success,
"Sunucu bağlantısı yeniden kuruldu",
);
disconnect_notified = false;
}
was_connected = true;
}
loop {
if let Some(data_str) = msg.data().as_string() {
if let Ok(event) = serde_json::from_str::<AppEvent>(&data_str) {
match event {
AppEvent::FullList { torrents: list, .. } => {
torrents.set(list);
}
AppEvent::Update(update) => {
torrents.update(|list| {
if let Some(t) = list.iter_mut().find(|t| t.hash == update.hash)
{
if let Some(name) = update.name {
t.name = name;
log::debug!("SSE: Creating EventSource...");
let es_result = EventSource::new("/api/events");
match es_result {
Ok(mut es) => {
log::debug!("SSE: EventSource created, subscribing...");
if let Ok(mut stream) = es.subscribe("message") {
log::debug!("SSE: Subscribed to message channel");
let mut got_first_message = false;
while let Some(Ok((_, msg))) = stream.next().await {
log::debug!("SSE: Received message");
if !got_first_message {
got_first_message = true;
backoff_ms = 1000;
if was_connected && disconnect_notified {
show_toast_with_signal(notifications_for_sse, NotificationLevel::Success, "Sunucu bağlantısı yeniden kuruldu");
disconnect_notified = false;
}
was_connected = true;
}
if let Some(data_str) = msg.data().as_string() {
// Decode Base64
match BASE64.decode(&data_str) {
Ok(bytes) => {
// Deserialize MessagePack
match rmp_serde::from_slice::<AppEvent>(&bytes) {
Ok(event) => {
match event {
AppEvent::FullList { torrents: list, .. } => {
log::info!("SSE: Received FullList with {} torrents", list.len());
torrents_for_sse.update(|map| {
let new_hashes: std::collections::HashSet<String> = list.iter().map(|t| t.hash.clone()).collect();
map.retain(|hash, _| new_hashes.contains(hash));
for new_torrent in list {
map.insert(new_torrent.hash.clone(), new_torrent);
}
if let Some(size) = update.size {
t.size = size;
}
if let Some(down_rate) = update.down_rate {
t.down_rate = down_rate;
}
if let Some(up_rate) = update.up_rate {
t.up_rate = up_rate;
}
if let Some(percent_complete) = update.percent_complete {
t.percent_complete = percent_complete;
}
if let Some(completed) = update.completed {
t.completed = completed;
}
if let Some(eta) = update.eta {
t.eta = eta;
}
if let Some(status) = update.status {
t.status = status;
}
if let Some(error_message) = update.error_message {
t.error_message = error_message;
}
if let Some(label) = update.label {
t.label = Some(label);
});
log::debug!("SSE: torrents map now has {} entries", torrents_for_sse.with(|m| m.len()));
}
AppEvent::Update(patch) => {
torrents_for_sse.update(|map| {
if let Some(hash) = patch.hash.as_ref() {
if let Some(t) = map.get_mut(hash) {
t.apply(patch);
}
}
});
}
AppEvent::Stats(stats) => { global_stats_for_sse.set(stats); }
AppEvent::Notification(n) => {
show_toast_with_signal(notifications_for_sse, n.level.clone(), n.message.clone());
if n.message.contains("tamamlandı") || n.level == shared::NotificationLevel::Error {
show_browser_notification("VibeTorrent", &n.message);
}
});
}
AppEvent::Stats(stats) => {
global_stats.set(stats);
}
AppEvent::Notification(n) => {
// Show toast notification
show_toast_with_signal(notifications, n.level.clone(), n.message.clone());
// Show browser notification for critical events
let is_critical = n.message.contains("tamamlandı")
|| n.level == shared::NotificationLevel::Error;
if is_critical {
let title = match n.level {
shared::NotificationLevel::Success => "✅ VibeTorrent",
shared::NotificationLevel::Error => "❌ VibeTorrent",
shared::NotificationLevel::Warning => "⚠️ VibeTorrent",
shared::NotificationLevel::Info => " VibeTorrent",
};
crate::utils::notification::show_notification_if_enabled(
title,
&n.message
);
}
}
}
Err(e) => log::error!("SSE: Failed to deserialize MessagePack: {}", e),
}
}
}
// Stream ended - connection lost
if was_connected && !disconnect_notified {
show_toast_with_signal(
notifications,
NotificationLevel::Warning,
"Sunucu bağlantısı kesildi, yeniden bağlanılıyor...",
);
disconnect_notified = true;
}
}
Err(_) => {
// Failed to subscribe - only notify once
if was_connected && !disconnect_notified {
show_toast_with_signal(
notifications,
NotificationLevel::Warning,
"Sunucu bağlantısı kesildi, yeniden bağlanılıyor...",
);
disconnect_notified = true;
Err(e) => log::error!("SSE: Failed to decode Base64: {}", e),
}
}
}
}
Err(_) => {
// Failed to create EventSource - only notify once
if was_connected && !disconnect_notified {
show_toast_with_signal(
notifications,
NotificationLevel::Warning,
"Sunucu bağlantısı kesildi, yeniden bağlanılıyor...",
);
show_toast_with_signal(notifications_for_sse, NotificationLevel::Warning, "Sunucu bağlantısı kesildi, yeniden bağlanılıyor...");
disconnect_notified = true;
}
}
}
// Wait before reconnecting (exponential backoff)
gloo_timers::future::TimeoutFuture::new(backoff_ms).await;
backoff_ms = std::cmp::min(backoff_ms * 2, max_backoff_ms);
Err(_) => {
if was_connected && !disconnect_notified {
show_toast_with_signal(notifications_for_sse, NotificationLevel::Warning, "Sunucu bağlantısı kurulamıyor...");
disconnect_notified = true;
}
}
}
});
log::debug!("SSE: Reconnecting in {}ms...", backoff_ms);
gloo_timers::future::TimeoutFuture::new(backoff_ms).await;
backoff_ms = std::cmp::min(backoff_ms * 2, 30000);
}
});
}
// ============================================================================
// Push Notification Subscription
// ============================================================================
use serde::{Deserialize, Serialize};
use wasm_bindgen::prelude::*;
#[derive(Serialize, Deserialize, Debug)]
struct PushSubscriptionData {
endpoint: String,
keys: PushKeys,
}
#[derive(Serialize, Deserialize, Debug)]
struct PushKeys {
p256dh: String,
auth: String,
}
/// Subscribe user to push notifications
/// Requests notification permission if needed, then subscribes to push
pub async fn subscribe_to_push_notifications() {
use gloo_net::http::Request;
// First, request notification permission if not already granted
let window = web_sys::window().expect("window should exist");
let permission_granted = if let Ok(notification_class) = js_sys::Reflect::get(&window, &"Notification".into()) {
if notification_class.is_undefined() {
log::error!("Notification API not available");
return;
}
// Check current permission
let current_permission = js_sys::Reflect::get(&notification_class, &"permission".into())
.ok()
.and_then(|p| p.as_string())
.unwrap_or_default();
if current_permission == "granted" {
log::info!("Notification permission already granted");
true
} else if current_permission == "denied" {
log::warn!("Notification permission was denied");
return;
} else {
// Permission is "default" - need to request
log::info!("Requesting notification permission...");
if let Ok(request_fn) = js_sys::Reflect::get(&notification_class, &"requestPermission".into()) {
if request_fn.is_function() {
let request_fn_typed = js_sys::Function::from(request_fn);
match request_fn_typed.call0(&notification_class) {
Ok(promise_val) => {
let request_future = wasm_bindgen_futures::JsFuture::from(
js_sys::Promise::from(promise_val)
);
match request_future.await {
Ok(result) => {
let result_str = result.as_string().unwrap_or_default();
log::info!("Permission request result: {}", result_str);
result_str == "granted"
}
Err(e) => {
log::error!("Failed to request notification permission: {:?}", e);
false
}
}
}
Err(e) => {
log::error!("Failed to call requestPermission: {:?}", e);
false
}
}
} else {
false
}
} else {
false
}
}
} else {
log::error!("Cannot access Notification class");
return;
};
if !permission_granted {
log::warn!("Notification permission not granted, cannot subscribe to push");
return;
}
log::info!("Notification permission granted! Proceeding with push subscription...");
// Get VAPID public key from backend
let public_key_response = match Request::get("/api/push/public-key").send().await {
Ok(resp) => resp,
Err(e) => {
log::error!("Failed to get VAPID public key: {:?}", e);
return;
}
};
let public_key_data: serde_json::Value = match public_key_response.json().await {
Ok(data) => data,
Err(e) => {
log::error!("Failed to parse VAPID public key: {:?}", e);
return;
}
};
let public_key = match public_key_data.get("publicKey").and_then(|v| v.as_str()) {
Some(key) => key,
None => {
log::error!("Missing publicKey in response");
return;
}
};
log::info!("VAPID public key from backend: {} (len: {})", public_key, public_key.len());
// Convert VAPID public key to Uint8Array
let public_key_array = match url_base64_to_uint8array(public_key) {
Ok(arr) => {
log::info!("VAPID key converted to Uint8Array (length: {})", arr.length());
arr
}
Err(e) => {
log::error!("Failed to convert VAPID key: {:?}", e);
return;
}
};
// Get service worker registration
let window = web_sys::window().expect("window should exist");
let navigator = window.navigator();
let service_worker = navigator.service_worker();
let registration_promise = match service_worker.ready() {
Ok(promise) => promise,
Err(e) => {
log::error!("Failed to get ready promise: {:?}", e);
return;
}
};
let registration_future = wasm_bindgen_futures::JsFuture::from(registration_promise);
let registration = match registration_future.await {
Ok(reg) => reg,
Err(e) => {
log::error!("Failed to get service worker registration: {:?}", e);
return;
}
};
let service_worker_registration = registration
.dyn_into::<web_sys::ServiceWorkerRegistration>()
.expect("should be ServiceWorkerRegistration");
// Subscribe to push
let push_manager = match service_worker_registration.push_manager() {
Ok(pm) => pm,
Err(e) => {
log::error!("Failed to get push manager: {:?}", e);
return;
}
};
let subscribe_options = web_sys::PushSubscriptionOptionsInit::new();
subscribe_options.set_user_visible_only(true);
subscribe_options.set_application_server_key(&public_key_array);
let subscribe_promise = match push_manager.subscribe_with_options(&subscribe_options) {
Ok(promise) => promise,
Err(e) => {
log::error!("Failed to subscribe to push: {:?}", e);
return;
}
};
let subscription_future = wasm_bindgen_futures::JsFuture::from(subscribe_promise);
let subscription = match subscription_future.await {
Ok(sub) => sub,
Err(e) => {
log::error!("Failed to get push subscription: {:?}", e);
return;
}
};
let push_subscription = subscription
.dyn_into::<web_sys::PushSubscription>()
.expect("should be PushSubscription");
// Get subscription JSON using toJSON() method
let json_result = match js_sys::Reflect::get(&push_subscription, &"toJSON".into()) {
Ok(func) if func.is_function() => {
let json_func = js_sys::Function::from(func);
match json_func.call0(&push_subscription) {
Ok(result) => result,
Err(e) => {
log::error!("Failed to call toJSON: {:?}", e);
return;
}
}
}
_ => {
log::error!("toJSON method not found on PushSubscription");
return;
}
};
let json_value = match js_sys::JSON::stringify(&json_result) {
Ok(val) => val,
Err(e) => {
log::error!("Failed to stringify subscription: {:?}", e);
return;
}
};
let subscription_json_str = json_value.as_string().expect("should be string");
log::info!("Push subscription: {}", subscription_json_str);
// Parse and send to backend
let subscription_data: serde_json::Value = match serde_json::from_str(&subscription_json_str) {
Ok(data) => data,
Err(e) => {
log::error!("Failed to parse subscription JSON: {:?}", e);
return;
}
};
// Extract endpoint and keys
let endpoint = subscription_data
.get("endpoint")
.and_then(|v| v.as_str())
.expect("endpoint should exist")
.to_string();
let keys_obj = subscription_data
.get("keys")
.expect("keys should exist");
let p256dh = keys_obj
.get("p256dh")
.and_then(|v| v.as_str())
.expect("p256dh should exist")
.to_string();
let auth = keys_obj
.get("auth")
.and_then(|v| v.as_str())
.expect("auth should exist")
.to_string();
let push_data = PushSubscriptionData {
endpoint,
keys: PushKeys { p256dh, auth },
};
// Send to backend
let response = match Request::post("/api/push/subscribe")
.json(&push_data)
.expect("serialization should succeed")
.send()
.await
{
Ok(resp) => resp,
Err(e) => {
log::error!("Failed to send subscription to backend: {:?}", e);
return;
}
};
if response.ok() {
log::info!("Successfully subscribed to push notifications");
} else {
log::error!("Backend rejected push subscription: {:?}", response.status());
}
}
/// Helper to convert URL-safe base64 string to Uint8Array
/// Uses JavaScript to properly decode binary data (avoids UTF-8 encoding issues)
fn url_base64_to_uint8array(base64_string: &str) -> Result<js_sys::Uint8Array, JsValue> {
// Add padding
let padding = (4 - (base64_string.len() % 4)) % 4;
let mut padded = base64_string.to_string();
padded.push_str(&"=".repeat(padding));
// Replace URL-safe characters
let standard_base64 = padded.replace('-', "+").replace('_', "/");
// Decode using JavaScript to avoid UTF-8 encoding issues
// Create a JavaScript function to decode the base64 and convert to Uint8Array
let js_code = format!(
r#"
(function() {{
const binaryString = atob('{}');
const bytes = new Uint8Array(binaryString.length);
for (let i = 0; i < binaryString.length; i++) {{
bytes[i] = binaryString.charCodeAt(i);
}}
return bytes;
}})()
"#,
standard_base64
);
let result = js_sys::eval(&js_code)?;
let array = result.dyn_into::<js_sys::Uint8Array>()?;
Ok(array)
// ...
}

View File

@@ -1,37 +1,19 @@
use wasm_bindgen::prelude::*;
use web_sys::{Notification, NotificationOptions};
use leptos::prelude::*;
use leptos_use::{use_web_notification, UseWebNotificationReturn, NotificationPermission};
/// Request browser notification permission from user
pub async fn request_notification_permission() -> bool {
let window = web_sys::window().expect("no global window");
// Check if Notification API is available
if js_sys::Reflect::has(&window, &JsValue::from_str("Notification")).unwrap_or(false) {
let notification = js_sys::Reflect::get(&window, &JsValue::from_str("Notification"))
.expect("Notification should exist");
// Request permission
let promise = js_sys::Reflect::get(&notification, &JsValue::from_str("requestPermission"))
.expect("requestPermission should exist");
if let Ok(function) = promise.dyn_into::<js_sys::Function>() {
if let Ok(promise) = function.call0(&notification) {
if let Ok(promise) = promise.dyn_into::<js_sys::Promise>() {
let result = wasm_bindgen_futures::JsFuture::from(promise).await;
if let Ok(permission) = result {
let permission_str = permission.as_string().unwrap_or_default();
return permission_str == "granted";
}
}
}
if let Ok(promise) = Notification::request_permission() {
if let Ok(result) = wasm_bindgen_futures::JsFuture::from(promise).await {
return result.as_string().unwrap_or_default() == "granted";
}
}
false
}
/// Check if browser notifications are supported and permitted
/// Check if browser notifications are supported
pub fn is_notification_supported() -> bool {
let window = web_sys::window().expect("no global window");
js_sys::Reflect::has(&window, &JsValue::from_str("Notification")).unwrap_or(false)
@@ -39,67 +21,61 @@ pub fn is_notification_supported() -> bool {
/// Get current notification permission status
pub fn get_notification_permission() -> String {
if !is_notification_supported() {
return "unsupported".to_string();
match Notification::permission() {
web_sys::NotificationPermission::Granted => "granted".to_string(),
web_sys::NotificationPermission::Denied => "denied".to_string(),
web_sys::NotificationPermission::Default => "default".to_string(),
_ => "default".to_string(),
}
let window = web_sys::window().expect("no global window");
let notification = js_sys::Reflect::get(&window, &JsValue::from_str("Notification"))
.expect("Notification should exist");
let permission = js_sys::Reflect::get(&notification, &JsValue::from_str("permission"))
.unwrap_or(JsValue::from_str("default"));
permission.as_string().unwrap_or("default".to_string())
}
/// Show a browser notification
/// Returns true if notification was shown successfully
pub fn show_browser_notification(title: &str, body: &str, icon: Option<&str>) -> bool {
// Check permission first
let permission = get_notification_permission();
if permission != "granted" {
log::warn!("Notification permission not granted: {}", permission);
/// Hook for using browser notifications within Leptos components or effects.
/// This uses leptos-use for reactive permission tracking.
pub fn use_app_notification() -> impl Fn(&str, &str) + Clone {
let UseWebNotificationReturn { permission, .. } = use_web_notification();
move |title: &str, body: &str| {
// Check user preference from localStorage
let window = web_sys::window().expect("no global window");
let storage = window.local_storage().ok().flatten();
let enabled = storage
.and_then(|s| s.get_item("vibetorrent_browser_notifications").ok().flatten())
.unwrap_or_else(|| "true".to_string());
// Use the reactive permission signal from leptos-use
if enabled == "true" && permission.get() == NotificationPermission::Granted {
show_browser_notification(title, body);
}
}
}
/// Show a browser notification (non-reactive version)
pub fn show_browser_notification(title: &str, body: &str) -> bool {
if get_notification_permission() != "granted" {
return false;
}
// Create notification options
let opts = NotificationOptions::new();
opts.set_body(body);
opts.set_icon(icon.unwrap_or("/icon-192.png"));
opts.set_badge("/icon-192.png");
opts.set_icon("/icon-192.png");
opts.set_tag("vibetorrent");
opts.set_require_interaction(false);
opts.set_silent(Some(false));
// Create and show notification
match Notification::new_with_options(title, &opts) {
Ok(_notification) => {
true
}
Err(e) => {
log::error!("Failed to create notification: {:?}", e);
false
}
Ok(_) => true,
Err(_) => false,
}
}
/// Show notification only if enabled in settings and permission granted
/// Legacy helper for showing notification if enabled in settings
pub fn show_notification_if_enabled(title: &str, body: &str) -> bool {
// Check localStorage for user preference
let window = web_sys::window().expect("no global window");
let storage = window.local_storage().ok().flatten();
if let Some(storage) = storage {
let enabled = storage
.get_item("vibetorrent_browser_notifications")
.ok()
.flatten()
.unwrap_or("true".to_string());
let enabled = storage
.and_then(|s| s.get_item("vibetorrent_browser_notifications").ok().flatten())
.unwrap_or_else(|| "true".to_string());
if enabled == "true" {
return show_browser_notification(title, body, None);
}
if enabled == "true" {
return show_browser_notification(title, body);
}
false

View File

@@ -1,127 +1,158 @@
const CACHE_NAME = 'vibetorrent-v1';
const CACHE_NAME = "vibetorrent-v2";
const ASSETS_TO_CACHE = [
'/',
'/index.html',
'/manifest.json',
'/icon-192.png',
'/icon-512.png'
"/",
"/index.html",
"/manifest.json",
"/icon-192.png",
"/icon-512.png",
];
// Install event - cache assets
self.addEventListener('install', (event) => {
console.log('[Service Worker] Installing...');
event.waitUntil(
caches.open(CACHE_NAME).then((cache) => {
console.log('[Service Worker] Caching static assets');
return cache.addAll(ASSETS_TO_CACHE);
}).then(() => {
console.log('[Service Worker] Skip waiting');
return self.skipWaiting();
})
);
self.addEventListener("install", (event) => {
console.log("[Service Worker] Installing...");
event.waitUntil(
caches
.open(CACHE_NAME)
.then((cache) => {
console.log("[Service Worker] Caching static assets");
return cache.addAll(ASSETS_TO_CACHE);
})
.then(() => {
console.log("[Service Worker] Skip waiting");
return self.skipWaiting();
}),
);
});
// Activate event - clean old caches
self.addEventListener('activate', (event) => {
console.log('[Service Worker] Activating...');
event.waitUntil(
caches.keys().then((cacheNames) => {
return Promise.all(
cacheNames.map((key) => {
if (key !== CACHE_NAME) {
console.log('[Service Worker] Deleting old cache:', key);
return caches.delete(key);
}
})
);
}).then(() => {
console.log('[Service Worker] Claiming clients');
return self.clients.claim();
})
);
self.addEventListener("activate", (event) => {
console.log("[Service Worker] Activating...");
event.waitUntil(
caches
.keys()
.then((cacheNames) => {
return Promise.all(
cacheNames.map((key) => {
if (key !== CACHE_NAME) {
console.log("[Service Worker] Deleting old cache:", key);
return caches.delete(key);
}
}),
);
})
.then(() => {
console.log("[Service Worker] Claiming clients");
return self.clients.claim();
}),
);
});
// Fetch event - network first, cache fallback for API calls
self.addEventListener('fetch', (event) => {
const url = new URL(event.request.url);
// Network-first strategy for API calls
if (url.pathname.startsWith('/api/')) {
event.respondWith(
fetch(event.request)
.catch(() => {
// Could return cached API response or offline page
return new Response(
JSON.stringify({ error: 'Offline' }),
{ headers: { 'Content-Type': 'application/json' } }
);
})
);
return;
}
// Cache-first strategy for static assets
// Fetch event - network first for HTML, cache fallback for API calls
self.addEventListener("fetch", (event) => {
const url = new URL(event.request.url);
// Network-first strategy for API calls
if (url.pathname.startsWith("/api/")) {
event.respondWith(
caches.match(event.request).then((response) => {
return response || fetch(event.request).then((fetchResponse) => {
// Optionally cache new requests
if (fetchResponse && fetchResponse.status === 200) {
const responseToCache = fetchResponse.clone();
caches.open(CACHE_NAME).then((cache) => {
cache.put(event.request, responseToCache);
});
}
return fetchResponse;
});
})
fetch(event.request).catch(() => {
// Could return cached API response or offline page
return new Response(JSON.stringify({ error: "Offline" }), {
headers: { "Content-Type": "application/json" },
});
}),
);
return;
}
// Network-first strategy for HTML pages (entry points)
// This ensures users always get the latest version of the app
if (
event.request.mode === "navigate" ||
url.pathname.endsWith("index.html") ||
url.pathname === "/"
) {
event.respondWith(
fetch(event.request)
.then((response) => {
// Cache the latest version of the HTML
const responseToCache = response.clone();
caches.open(CACHE_NAME).then((cache) => {
cache.put(event.request, responseToCache);
});
return response;
})
.catch(() => {
return caches.match(event.request);
}),
);
return;
}
// Cache-first strategy for static assets (JS, CSS, Images)
event.respondWith(
caches.match(event.request).then((response) => {
return (
response ||
fetch(event.request).then((fetchResponse) => {
// Optionally cache new requests
if (fetchResponse && fetchResponse.status === 200) {
const responseToCache = fetchResponse.clone();
caches.open(CACHE_NAME).then((cache) => {
cache.put(event.request, responseToCache);
});
}
return fetchResponse;
})
);
}),
);
});
// Notification click event - focus or open app
self.addEventListener('notificationclick', (event) => {
console.log('[Service Worker] Notification clicked:', event.notification.tag);
event.notification.close();
event.waitUntil(
clients.matchAll({ type: 'window', includeUncontrolled: true }).then((clientList) => {
// If app is already open, focus it
for (let client of clientList) {
if (client.url === '/' && 'focus' in client) {
return client.focus();
}
}
// Otherwise open new window
if (clients.openWindow) {
return clients.openWindow('/');
}
})
);
self.addEventListener("notificationclick", (event) => {
console.log("[Service Worker] Notification clicked:", event.notification.tag);
event.notification.close();
event.waitUntil(
clients
.matchAll({ type: "window", includeUncontrolled: true })
.then((clientList) => {
// If app is already open, focus it
for (let client of clientList) {
if (client.url === "/" && "focus" in client) {
return client.focus();
}
}
// Otherwise open new window
if (clients.openWindow) {
return clients.openWindow("/");
}
}),
);
});
// Push notification event
self.addEventListener('push', (event) => {
console.log('[Service Worker] Push notification received');
const data = event.data ? event.data.json() : {};
const title = data.title || 'VibeTorrent';
const options = {
body: data.body || 'New notification',
icon: data.icon || '/icon-192.png',
badge: data.badge || '/icon-192.png',
tag: data.tag || 'vibetorrent-notification',
requireInteraction: false,
// iOS-specific: vibrate pattern (if supported)
vibrate: [200, 100, 200],
// Add data for notification click handling
data: {
url: data.url || '/',
timestamp: Date.now()
}
};
console.log('[Service Worker] Showing notification:', title, options);
event.waitUntil(
self.registration.showNotification(title, options)
);
self.addEventListener("push", (event) => {
console.log("[Service Worker] Push notification received");
const data = event.data ? event.data.json() : {};
const title = data.title || "VibeTorrent";
const options = {
body: data.body || "New notification",
icon: data.icon || "/icon-192.png",
badge: data.badge || "/icon-192.png",
tag: data.tag || "vibetorrent-notification",
requireInteraction: false,
// iOS-specific: vibrate pattern (if supported)
vibrate: [200, 100, 200],
// Add data for notification click handling
data: {
url: data.url || "/",
timestamp: Date.now(),
},
};
console.log("[Service Worker] Showing notification:", title, options);
event.waitUntil(self.registration.showNotification(title, options));
});

View File

@@ -0,0 +1,30 @@
[package]
edition = "2018"
name = "coarsetime"
version = "0.1.37"
description = "Time and duration crate optimized for speed (patched for MIPS)"
license = "BSD-2-Clause"
[features]
wasi-abi2 = ["dep:wasi-abi2"]
[lib]
name = "coarsetime"
path = "src/lib.rs"
[dependencies]
portable-atomic = { version = "1", default-features = false, features = ["fallback"] }
[target.'cfg(all(any(target_arch = "wasm32", target_arch = "wasm64"), target_os = "unknown"))'.dependencies.wasm-bindgen]
version = "0.2"
[target.'cfg(any(target_os = "wasix", target_os = "wasi"))'.dependencies.wasix]
version = "0.13"
[target.'cfg(not(any(target_os = "wasix", target_os = "wasi")))'.dependencies.libc]
version = "0.2"
[target.'cfg(target_os = "wasi")'.dependencies.wasi-abi2]
version = "0.14.7"
optional = true
package = "wasi"

View File

@@ -4,9 +4,6 @@
)))]
use std::time;
#[cfg(target_has_atomic = "64")]
use std::sync::atomic::{AtomicU64, Ordering};
#[cfg(not(target_has_atomic = "64"))]
use portable_atomic::{AtomicU64, Ordering};
use super::Duration;

View File

@@ -3,9 +3,6 @@ use std::mem::MaybeUninit;
use std::ops::*;
#[allow(unused_imports)]
use std::ptr::*;
#[cfg(target_has_atomic = "64")]
use std::sync::atomic::{AtomicU64, Ordering};
#[cfg(not(target_has_atomic = "64"))]
use portable_atomic::{AtomicU64, Ordering};
use super::duration::*;

View File

@@ -1,8 +1,50 @@
[package]
name = "shared"
version = "0.1.0"
edition = "2024"
edition = "2021"
[dependencies]
serde = { version = "1.0.228", features = ["derive"] }
serde = { version = "1.0", features = ["derive"] }
utoipa = { version = "5.4.0", features = ["axum_extras"] }
struct-patch = "0.5"
rmp-serde = "1.3"
# Leptos 0.8.7
leptos = { version = "0.8.7", features = ["nightly"] }
leptos_router = { version = "0.8.7", features = ["nightly"] }
leptos_axum = { version = "0.8.7", optional = true }
axum = { version = "0.8", features = ["macros"], optional = true }
# SSR Dependencies (XML-RPC & SCGI)
tokio = { version = "1", features = ["full"], optional = true }
bytes = { version = "1", optional = true }
thiserror = { version = "2", optional = true }
quick-xml = { version = "0.31", features = ["serde", "serialize"], optional = true }
# Database
sqlx = { version = "0.8", features = ["runtime-tokio", "sqlite"], optional = true }
anyhow = { version = "1.0", optional = true }
# Auth (SSR)
jsonwebtoken = { version = "9", optional = true }
cookie = { version = "0.18", features = ["percent-encode"], optional = true }
bcrypt = { version = "0.17", optional = true }
[features]
default = []
ssr = [
"dep:tokio",
"dep:bytes",
"dep:thiserror",
"dep:quick-xml",
"dep:leptos_axum",
"dep:sqlx",
"dep:anyhow",
"dep:jsonwebtoken",
"dep:cookie",
"dep:bcrypt",
"dep:axum",
"leptos/ssr",
"leptos_router/ssr",
]
hydrate = ["leptos/hydrate"]

View File

@@ -0,0 +1,16 @@
-- 001_init.sql
-- Initial schema for users and sessions
CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY,
username TEXT NOT NULL UNIQUE,
password_hash TEXT NOT NULL,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
);
CREATE TABLE IF NOT EXISTS sessions (
token TEXT PRIMARY KEY,
user_id INTEGER NOT NULL,
expires_at DATETIME NOT NULL,
FOREIGN KEY(user_id) REFERENCES users(id)
);

View File

@@ -0,0 +1,13 @@
-- 002_push_subscriptions.sql
-- Push notification subscriptions storage
CREATE TABLE IF NOT EXISTS push_subscriptions (
id INTEGER PRIMARY KEY AUTOINCREMENT,
endpoint TEXT NOT NULL UNIQUE,
p256dh TEXT NOT NULL,
auth TEXT NOT NULL,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
);
-- Index for faster lookups by endpoint
CREATE INDEX IF NOT EXISTS idx_push_subscriptions_endpoint ON push_subscriptions(endpoint);

148
shared/src/db.rs Normal file
View File

@@ -0,0 +1,148 @@
use sqlx::{sqlite::SqlitePoolOptions, Pool, Sqlite, Row, sqlite::SqliteConnectOptions};
use std::time::Duration;
use anyhow::Result;
use std::str::FromStr;
#[derive(Clone)]
pub struct Db {
pool: Pool<Sqlite>,
}
impl Db {
pub async fn new(db_url: &str) -> Result<Self> {
let options = SqliteConnectOptions::from_str(db_url)?
.create_if_missing(true)
.busy_timeout(Duration::from_secs(10)) // Bekleme süresini 10 saniyeye çıkardık
.journal_mode(sqlx::sqlite::SqliteJournalMode::Wal)
.synchronous(sqlx::sqlite::SqliteSynchronous::Normal);
let pool = SqlitePoolOptions::new()
.max_connections(5)
.acquire_timeout(Duration::from_secs(10))
.connect_with(options)
.await?;
let db = Self { pool };
db.run_migrations().await?;
Ok(db)
}
async fn run_migrations(&self) -> Result<()> {
sqlx::migrate!("./migrations").run(&self.pool).await?;
Ok(())
}
// --- User Operations ---
pub async fn create_user(&self, username: &str, password_hash: &str) -> Result<()> {
sqlx::query("INSERT INTO users (username, password_hash) VALUES (?, ?)")
.bind(username)
.bind(password_hash)
.execute(&self.pool)
.await?;
Ok(())
}
pub async fn get_user_by_username(&self, username: &str) -> Result<Option<(i64, String)>> {
let row = sqlx::query("SELECT id, password_hash FROM users WHERE username = ?")
.bind(username)
.fetch_optional(&self.pool)
.await?;
Ok(row.map(|r| (r.get(0), r.get(1))))
}
pub async fn get_username_by_id(&self, id: i64) -> Result<Option<String>> {
let row = sqlx::query("SELECT username FROM users WHERE id = ?")
.bind(id)
.fetch_optional(&self.pool)
.await?;
Ok(row.map(|r| r.get(0)))
}
pub async fn has_users(&self) -> Result<bool> {
let row: (i64,) = sqlx::query_as("SELECT COUNT(*) FROM users")
.fetch_one(&self.pool)
.await?;
Ok(row.0 > 0)
}
// --- Session Operations ---
pub async fn create_session(&self, user_id: i64, token: &str, expires_at: i64) -> Result<()> {
sqlx::query("INSERT INTO sessions (token, user_id, expires_at) VALUES (?, ?, datetime(?, 'unixepoch'))")
.bind(token)
.bind(user_id)
.bind(expires_at)
.execute(&self.pool)
.await?;
Ok(())
}
pub async fn get_session_user(&self, token: &str) -> Result<Option<i64>> {
let row = sqlx::query("SELECT user_id FROM sessions WHERE token = ? AND expires_at > datetime('now')")
.bind(token)
.fetch_optional(&self.pool)
.await?;
Ok(row.map(|r| r.get(0)))
}
pub async fn delete_session(&self, token: &str) -> Result<()> {
sqlx::query("DELETE FROM sessions WHERE token = ?")
.bind(token)
.execute(&self.pool)
.await?;
Ok(())
}
pub async fn update_password(&self, user_id: i64, password_hash: &str) -> Result<()> {
sqlx::query("UPDATE users SET password_hash = ? WHERE id = ?")
.bind(password_hash)
.bind(user_id)
.execute(&self.pool)
.await?;
Ok(())
}
pub async fn delete_all_sessions_for_user(&self, user_id: i64) -> Result<()> {
sqlx::query("DELETE FROM sessions WHERE user_id = ?")
.bind(user_id)
.execute(&self.pool)
.await?;
Ok(())
}
// --- Push Subscription Operations ---
pub async fn save_push_subscription(&self, endpoint: &str, p256dh: &str, auth: &str) -> Result<()> {
sqlx::query(
"INSERT INTO push_subscriptions (endpoint, p256dh, auth) VALUES (?, ?, ?)
ON CONFLICT(endpoint) DO UPDATE SET p256dh = EXCLUDED.p256dh, auth = EXCLUDED.auth"
)
.bind(endpoint)
.bind(p256dh)
.bind(auth)
.execute(&self.pool)
.await?;
Ok(())
}
pub async fn remove_push_subscription(&self, endpoint: &str) -> Result<()> {
sqlx::query("DELETE FROM push_subscriptions WHERE endpoint = ?")
.bind(endpoint)
.execute(&self.pool)
.await?;
Ok(())
}
pub async fn get_all_push_subscriptions(&self) -> Result<Vec<(String, String, String)>> {
let rows = sqlx::query_as::<_, (String, String, String)>(
"SELECT endpoint, p256dh, auth FROM push_subscriptions"
)
.fetch_all(&self.pool)
.await?;
Ok(rows)
}
}

View File

@@ -1,7 +1,32 @@
use serde::{Deserialize, Serialize};
use struct_patch::Patch;
use utoipa::ToSchema;
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, ToSchema)]
#[cfg(feature = "ssr")]
pub mod scgi;
#[cfg(feature = "ssr")]
pub mod xmlrpc;
#[cfg(feature = "ssr")]
pub mod db;
pub mod server_fns;
#[derive(Clone, Debug)]
pub struct ServerContext {
pub scgi_socket_path: String,
}
#[cfg(feature = "ssr")]
#[derive(Clone)]
pub struct DbContext {
pub db: db::Db,
}
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, ToSchema, Patch)]
#[patch_derive(Debug, Serialize, Deserialize, Clone, PartialEq, ToSchema, Default)]
#[patch_name = "TorrentUpdate"]
pub struct Torrent {
pub hash: String,
pub name: String,
@@ -28,14 +53,20 @@ pub enum TorrentStatus {
}
#[derive(Debug, Serialize, Deserialize, Clone, ToSchema)]
#[serde(tag = "type", content = "data")]
#[serde(tag = "t", content = "d")]
pub enum AppEvent {
#[serde(rename = "f")]
FullList {
#[serde(rename = "t")]
torrents: Vec<Torrent>,
#[serde(rename = "ts")]
timestamp: u64,
},
#[serde(rename = "u")]
Update(TorrentUpdate),
#[serde(rename = "s")]
Stats(GlobalStats),
#[serde(rename = "n")]
Notification(SystemNotification),
}
@@ -62,20 +93,8 @@ pub struct GlobalStats {
pub free_space: Option<i64>,
}
#[derive(Debug, Serialize, Deserialize, Clone, ToSchema)]
pub struct TorrentUpdate {
pub hash: String,
pub name: Option<String>,
pub size: Option<i64>,
pub down_rate: Option<i64>,
pub up_rate: Option<i64>,
pub percent_complete: Option<f64>,
pub completed: Option<i64>,
pub eta: Option<i64>,
pub status: Option<TorrentStatus>,
pub error_message: Option<String>,
pub label: Option<String>,
}
// REMOVED: Manual TorrentUpdate struct definition as it's now generated by Patch macro
#[derive(Debug, Serialize, Deserialize, Clone, ToSchema)]
pub struct TorrentActionRequest {
@@ -130,3 +149,9 @@ pub struct SetLabelRequest {
pub hash: String,
pub label: String,
}
#[derive(Debug, Serialize, Deserialize, Clone, ToSchema)]
pub struct AddTorrentRequest {
#[schema(example = "magnet:?xt=urn:btih:...")]
pub uri: String,
}

View File

@@ -1,3 +1,5 @@
#![cfg(feature = "ssr")]
use bytes::Bytes;
use std::collections::HashMap;
use thiserror::Error;
@@ -11,6 +13,8 @@ pub enum ScgiError {
#[allow(dead_code)]
#[error("Protocol Error: {0}")]
Protocol(String),
#[error("Timeout: SCGI request took too long")]
Timeout,
}
pub struct ScgiRequest {
@@ -78,20 +82,48 @@ impl ScgiRequest {
}
pub async fn send_request(socket_path: &str, request: ScgiRequest) -> Result<Bytes, ScgiError> {
let mut stream = UnixStream::connect(socket_path).await?;
let data = request.encode();
stream.write_all(&data).await?;
let perform_request = async {
let mut stream = UnixStream::connect(socket_path).await?;
let data = request.encode();
stream.write_all(&data).await?;
let mut response = Vec::new();
stream.read_to_end(&mut response).await?;
let mut response = Vec::new();
stream.read_to_end(&mut response).await?;
Ok::<Vec<u8>, std::io::Error>(response)
};
let double_newline = b"\r\n\r\n";
if let Some(pos) = response
.windows(double_newline.len())
.position(|window| window == double_newline)
{
Ok(Bytes::from(response.split_off(pos + double_newline.len())))
} else {
Ok(Bytes::from(response))
let response = tokio::time::timeout(std::time::Duration::from_secs(10), perform_request)
.await
.map_err(|_| ScgiError::Timeout)??;
let mut response_vec = response;
// Improved header stripping: find the first occurrence of "<?xml" OR double newline
let patterns = [
&b"\r\n\r\n"[..],
&b"\n\n"[..],
&b"<?xml"[..] // If headers are missing or weird, find start of XML
];
let mut found_pos = None;
for (i, pattern) in patterns.iter().enumerate() {
if let Some(pos) = response_vec
.windows(pattern.len())
.position(|window| window == *pattern)
{
// For XML pattern, we keep it. For newlines, we skip them.
if i == 2 {
found_pos = Some(pos);
} else {
found_pos = Some(pos + pattern.len());
}
break;
}
}
}
if let Some(pos) = found_pos {
Ok(Bytes::from(response_vec.split_off(pos)))
} else {
Ok(Bytes::from(response_vec))
}
}

View File

@@ -0,0 +1,168 @@
use leptos::prelude::*;
use serde::{Deserialize, Serialize};
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct UserResponse {
pub id: i64,
pub username: String,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Claims {
pub sub: String, // username
pub uid: i64, // user id
pub exp: usize,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct SetupStatus {
pub completed: bool,
}
#[server(GetSetupStatus, "/api/server_fns/GetSetupStatus")]
pub async fn get_setup_status() -> Result<SetupStatus, ServerFnError> {
use crate::DbContext;
let db_context = use_context::<DbContext>().ok_or_else(|| ServerFnError::new("DB Context missing"))?;
let has_users = db_context.db.has_users().await
.map_err(|e| ServerFnError::new(format!("DB error: {}", e)))?;
Ok(SetupStatus {
completed: has_users,
})
}
#[server(Setup, "/api/server_fns/Setup")]
pub async fn setup(username: String, password: String) -> Result<(), ServerFnError> {
use crate::DbContext;
let db_context = use_context::<DbContext>().ok_or_else(|| ServerFnError::new("DB Context missing"))?;
// Check if setup is already done
let has_users = db_context.db.has_users().await.unwrap_or(false);
if has_users {
return Err(ServerFnError::new("Setup already completed"));
}
// Hash password (low cost for MIPS)
let password_hash = bcrypt::hash(&password, 6)
.map_err(|_| ServerFnError::new("Hashing error"))?;
db_context.db.create_user(&username, &password_hash).await
.map_err(|e| ServerFnError::new(format!("DB error: {}", e)))?;
Ok(())
}
#[server(Login, "/api/server_fns/Login")]
pub async fn login(username: String, password: String) -> Result<UserResponse, ServerFnError> {
use crate::DbContext;
use leptos_axum::ResponseOptions;
use jsonwebtoken::{encode, Header, EncodingKey};
use cookie::{Cookie, SameSite};
use std::time::{SystemTime, UNIX_EPOCH};
let db_context = use_context::<DbContext>().ok_or_else(|| ServerFnError::new("DB Context missing"))?;
let user_opt = db_context.db.get_user_by_username(&username).await
.map_err(|e| ServerFnError::new(format!("DB error: {}", e)))?;
if let Some((uid, password_hash)) = user_opt {
let valid = bcrypt::verify(&password, &password_hash).unwrap_or(false);
if !valid {
return Err(ServerFnError::new("Invalid credentials"));
}
let expiration = SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap()
.as_secs() as usize + 24 * 3600; // 24 hours
let claims = Claims {
sub: username.clone(),
uid,
exp: expiration,
};
let secret = std::env::var("JWT_SECRET").unwrap_or_else(|_| "secret".to_string());
let token = encode(&Header::default(), &claims, &EncodingKey::from_secret(secret.as_bytes()))
.map_err(|e| ServerFnError::new(format!("Token error: {}", e)))?;
let cookie = Cookie::build(("auth_token", token))
.path("/")
.http_only(true)
.same_site(SameSite::Strict)
.build();
if let Some(options) = use_context::<ResponseOptions>() {
options.insert_header(
axum::http::header::SET_COOKIE,
axum::http::HeaderValue::from_str(&cookie.to_string()).unwrap(),
);
}
Ok(UserResponse {
id: uid,
username,
})
} else {
Err(ServerFnError::new("Invalid credentials"))
}
}
#[server(Logout, "/api/server_fns/Logout")]
pub async fn logout() -> Result<(), ServerFnError> {
use leptos_axum::ResponseOptions;
use cookie::{Cookie, SameSite};
let cookie = Cookie::build(("auth_token", ""))
.path("/")
.http_only(true)
.same_site(SameSite::Strict)
.max_age(cookie::time::Duration::seconds(0))
.build();
if let Some(options) = use_context::<ResponseOptions>() {
options.insert_header(
axum::http::header::SET_COOKIE,
axum::http::HeaderValue::from_str(&cookie.to_string()).unwrap(),
);
}
Ok(())
}
#[server(GetUser, "/api/server_fns/GetUser")]
pub async fn get_user() -> Result<Option<UserResponse>, ServerFnError> {
use axum::http::HeaderMap;
use leptos_axum::extract;
use jsonwebtoken::{decode, Validation, DecodingKey};
let headers: HeaderMap = extract().await.map_err(|e| ServerFnError::new(format!("Extract error: {}", e)))?;
let cookie_header = headers.get(axum::http::header::COOKIE)
.and_then(|h| h.to_str().ok());
if let Some(cookie_str) = cookie_header {
for c_str in cookie_str.split(';') {
if let Ok(c) = cookie::Cookie::parse(c_str.trim()) {
if c.name() == "auth_token" {
let token = c.value();
let secret = std::env::var("JWT_SECRET").unwrap_or_else(|_| "secret".to_string());
let token_data = decode::<Claims>(
token,
&DecodingKey::from_secret(secret.as_bytes()),
&Validation::default(),
);
if let Ok(data) = token_data {
return Ok(Some(UserResponse {
id: data.claims.uid,
username: data.claims.sub,
}));
}
}
}
}
}
Ok(None)
}

View File

@@ -0,0 +1,4 @@
pub mod torrent;
pub mod settings;
pub mod push;
pub mod auth;

View File

@@ -0,0 +1,22 @@
use leptos::prelude::*;
#[server(GetPushPublicKey, "/api/server_fns")]
pub async fn get_public_key() -> Result<String, ServerFnError> {
let key = std::env::var("VAPID_PUBLIC_KEY")
.map_err(|_| ServerFnError::new("VAPID_PUBLIC_KEY not configured"))?;
Ok(key)
}
#[server(SubscribePush, "/api/server_fns")]
pub async fn subscribe_push(
endpoint: String,
p256dh: String,
auth: String,
) -> Result<(), ServerFnError> {
let db_ctx = expect_context::<crate::DbContext>();
db_ctx
.db
.save_push_subscription(&endpoint, &p256dh, &auth)
.await
.map_err(|e| ServerFnError::new(format!("Failed to save subscription: {}", e)))
}

View File

@@ -0,0 +1,58 @@
use leptos::prelude::*;
use crate::GlobalLimitRequest;
#[server(GetGlobalLimits, "/api/server_fns")]
pub async fn get_global_limits() -> Result<GlobalLimitRequest, ServerFnError> {
use crate::xmlrpc::{self, RtorrentClient};
let ctx = expect_context::<crate::ServerContext>();
let client = RtorrentClient::new(&ctx.scgi_socket_path);
let down = match client.call("throttle.global_down.max_rate", &[]).await {
Ok(xml) => xmlrpc::parse_i64_response(&xml).unwrap_or(0),
Err(_) => -1,
};
let up = match client.call("throttle.global_up.max_rate", &[]).await {
Ok(xml) => xmlrpc::parse_i64_response(&xml).unwrap_or(0),
Err(_) => -1,
};
Ok(GlobalLimitRequest {
max_download_rate: Some(down),
max_upload_rate: Some(up),
})
}
#[server(SetGlobalLimits, "/api/server_fns")]
pub async fn set_global_limits(
max_download_rate: Option<i64>,
max_upload_rate: Option<i64>,
) -> Result<(), ServerFnError> {
use crate::xmlrpc::{RpcParam, RtorrentClient};
let ctx = expect_context::<crate::ServerContext>();
let client = RtorrentClient::new(&ctx.scgi_socket_path);
if let Some(down) = max_download_rate {
let down_kb = down / 1024;
client
.call(
"throttle.global_down.max_rate.set_kb",
&[RpcParam::from(""), RpcParam::Int(down_kb)],
)
.await
.map_err(|e| ServerFnError::new(format!("Failed to set down limit: {}", e)))?;
}
if let Some(up) = max_upload_rate {
let up_kb = up / 1024;
client
.call(
"throttle.global_up.max_rate.set_kb",
&[RpcParam::from(""), RpcParam::Int(up_kb)],
)
.await
.map_err(|e| ServerFnError::new(format!("Failed to set up limit: {}", e)))?;
}
Ok(())
}

View File

@@ -0,0 +1,274 @@
use leptos::prelude::*;
use crate::{TorrentFile, TorrentPeer, TorrentTracker};
#[server(AddTorrent, "/api/server_fns")]
pub async fn add_torrent(uri: String) -> Result<(), ServerFnError> {
use crate::xmlrpc::{RpcParam, RtorrentClient};
let ctx = expect_context::<crate::ServerContext>();
let client = RtorrentClient::new(&ctx.scgi_socket_path);
let params = vec![RpcParam::from(""), RpcParam::from(uri.as_str())];
match client.call("load.start", &params).await {
Ok(response) => {
if response.contains("faultCode") {
return Err(ServerFnError::new("rTorrent returned fault"));
}
Ok(())
}
Err(e) => Err(ServerFnError::new(format!("Failed to add torrent: {}", e))),
}
}
#[server(TorrentAction, "/api/server_fns")]
pub async fn torrent_action(hash: String, action: String) -> Result<String, ServerFnError> {
use crate::xmlrpc::{RpcParam, RtorrentClient};
let ctx = expect_context::<crate::ServerContext>();
let client = RtorrentClient::new(&ctx.scgi_socket_path);
if action == "delete_with_data" {
return delete_torrent_with_data_inner(&client, &hash).await;
}
let method = match action.as_str() {
"start" => "d.start",
"stop" => "d.stop",
"delete" => "d.erase",
_ => return Err(ServerFnError::new("Invalid action")),
};
let params = vec![RpcParam::from(hash.as_str())];
match client.call(method, &params).await {
Ok(_) => Ok("Action executed".to_string()),
Err(e) => Err(ServerFnError::new(format!("RPC error: {}", e))),
}
}
#[cfg(feature = "ssr")]
async fn delete_torrent_with_data_inner(
client: &crate::xmlrpc::RtorrentClient,
hash: &str,
) -> Result<String, ServerFnError> {
use crate::xmlrpc::{parse_string_response, RpcParam};
let params_hash = vec![RpcParam::from(hash)];
let path_xml = client
.call("d.base_path", &params_hash)
.await
.map_err(|e| ServerFnError::new(format!("Failed to call rTorrent: {}", e)))?;
let path = parse_string_response(&path_xml)
.map_err(|e| ServerFnError::new(format!("Failed to parse path: {}", e)))?;
let root_xml = client
.call("directory.default", &[])
.await
.map_err(|e| ServerFnError::new(format!("Failed to get download root: {}", e)))?;
let root_path_str = parse_string_response(&root_xml)
.map_err(|e| ServerFnError::new(format!("Failed to parse root path: {}", e)))?;
let root_path = tokio::fs::canonicalize(std::path::Path::new(&root_path_str))
.await
.map_err(|e| ServerFnError::new(format!("Invalid download root: {}", e)))?;
let target_path_raw = std::path::Path::new(&path);
if !tokio::fs::try_exists(target_path_raw).await.unwrap_or(false) {
client
.call("d.erase", &params_hash)
.await
.map_err(|e| ServerFnError::new(format!("Failed to erase torrent: {}", e)))?;
return Ok("Torrent removed (Data not found)".to_string());
}
let target_path = tokio::fs::canonicalize(target_path_raw)
.await
.map_err(|e| ServerFnError::new(format!("Invalid data path: {}", e)))?;
if !target_path.starts_with(&root_path) {
return Err(ServerFnError::new(
"Security Error: Cannot delete files outside download directory",
));
}
if target_path == root_path {
return Err(ServerFnError::new(
"Security Error: Cannot delete the download root directory",
));
}
client
.call("d.erase", &params_hash)
.await
.map_err(|e| ServerFnError::new(format!("Failed to erase torrent: {}", e)))?;
let delete_result = if target_path.is_dir() {
tokio::fs::remove_dir_all(&target_path).await
} else {
tokio::fs::remove_file(&target_path).await
};
match delete_result {
Ok(_) => Ok("Torrent and data deleted".to_string()),
Err(e) => Err(ServerFnError::new(format!("Failed to delete data: {}", e))),
}
}
#[server(GetFiles, "/api/server_fns")]
pub async fn get_files(hash: String) -> Result<Vec<TorrentFile>, ServerFnError> {
use crate::xmlrpc::{parse_multicall_response, RpcParam, RtorrentClient};
let ctx = expect_context::<crate::ServerContext>();
let client = RtorrentClient::new(&ctx.scgi_socket_path);
let params = vec![
RpcParam::from(hash.as_str()),
RpcParam::from(""),
RpcParam::from("f.path="),
RpcParam::from("f.size_bytes="),
RpcParam::from("f.completed_chunks="),
RpcParam::from("f.priority="),
];
let xml = client
.call("f.multicall", &params)
.await
.map_err(|e| ServerFnError::new(format!("RPC error: {}", e)))?;
let rows = parse_multicall_response(&xml)
.map_err(|e| ServerFnError::new(format!("Parse error: {}", e)))?;
Ok(rows
.into_iter()
.enumerate()
.map(|(idx, row)| TorrentFile {
index: idx as u32,
path: row.get(0).cloned().unwrap_or_default(),
size: row.get(1).and_then(|s| s.parse().ok()).unwrap_or(0),
completed_chunks: row.get(2).and_then(|s| s.parse().ok()).unwrap_or(0),
priority: row.get(3).and_then(|s| s.parse().ok()).unwrap_or(0),
})
.collect())
}
#[server(GetPeers, "/api/server_fns")]
pub async fn get_peers(hash: String) -> Result<Vec<TorrentPeer>, ServerFnError> {
use crate::xmlrpc::{parse_multicall_response, RpcParam, RtorrentClient};
let ctx = expect_context::<crate::ServerContext>();
let client = RtorrentClient::new(&ctx.scgi_socket_path);
let params = vec![
RpcParam::from(hash.as_str()),
RpcParam::from(""),
RpcParam::from("p.address="),
RpcParam::from("p.client_version="),
RpcParam::from("p.down_rate="),
RpcParam::from("p.up_rate="),
RpcParam::from("p.completed_percent="),
];
let xml = client
.call("p.multicall", &params)
.await
.map_err(|e| ServerFnError::new(format!("RPC error: {}", e)))?;
let rows = parse_multicall_response(&xml)
.map_err(|e| ServerFnError::new(format!("Parse error: {}", e)))?;
Ok(rows
.into_iter()
.map(|row| TorrentPeer {
ip: row.get(0).cloned().unwrap_or_default(),
client: row.get(1).cloned().unwrap_or_default(),
down_rate: row.get(2).and_then(|s| s.parse().ok()).unwrap_or(0),
up_rate: row.get(3).and_then(|s| s.parse().ok()).unwrap_or(0),
progress: row.get(4).and_then(|s| s.parse().ok()).unwrap_or(0.0),
})
.collect())
}
#[server(GetTrackers, "/api/server_fns")]
pub async fn get_trackers(hash: String) -> Result<Vec<TorrentTracker>, ServerFnError> {
use crate::xmlrpc::{parse_multicall_response, RpcParam, RtorrentClient};
let ctx = expect_context::<crate::ServerContext>();
let client = RtorrentClient::new(&ctx.scgi_socket_path);
let params = vec![
RpcParam::from(hash.as_str()),
RpcParam::from(""),
RpcParam::from("t.url="),
RpcParam::from("t.activity_date_last="),
RpcParam::from("t.message="),
];
let xml = client
.call("t.multicall", &params)
.await
.map_err(|e| ServerFnError::new(format!("RPC error: {}", e)))?;
let rows = parse_multicall_response(&xml)
.map_err(|e| ServerFnError::new(format!("Parse error: {}", e)))?;
Ok(rows
.into_iter()
.map(|row| TorrentTracker {
url: row.get(0).cloned().unwrap_or_default(),
status: "Unknown".to_string(),
message: row.get(2).cloned().unwrap_or_default(),
})
.collect())
}
#[server(SetFilePriority, "/api/server_fns")]
pub async fn set_file_priority(
hash: String,
file_index: u32,
priority: u8,
) -> Result<(), ServerFnError> {
use crate::xmlrpc::{RpcParam, RtorrentClient};
let ctx = expect_context::<crate::ServerContext>();
let client = RtorrentClient::new(&ctx.scgi_socket_path);
let target = format!("{}:f{}", hash, file_index);
let params = vec![
RpcParam::from(target.as_str()),
RpcParam::from(priority as i64),
];
client
.call("f.set_priority", &params)
.await
.map_err(|e| ServerFnError::new(format!("RPC error: {}", e)))?;
let _ = client
.call("d.update_priorities", &[RpcParam::from(hash.as_str())])
.await;
Ok(())
}
#[server(SetLabel, "/api/server_fns")]
pub async fn set_label(hash: String, label: String) -> Result<(), ServerFnError> {
use crate::xmlrpc::{RpcParam, RtorrentClient};
let ctx = expect_context::<crate::ServerContext>();
let client = RtorrentClient::new(&ctx.scgi_socket_path);
let params = vec![RpcParam::from(hash.as_str()), RpcParam::from(label)];
client
.call("d.custom1.set", &params)
.await
.map_err(|e| ServerFnError::new(format!("RPC error: {}", e)))?;
Ok(())
}
#[server(GetVersion, "/api/server_fns")]
pub async fn get_version() -> Result<String, ServerFnError> {
use crate::xmlrpc::{parse_string_response, RtorrentClient};
let ctx = expect_context::<crate::ServerContext>();
let client = RtorrentClient::new(&ctx.scgi_socket_path);
match client.call("system.client_version", &[]).await {
Ok(xml) => {
let version = parse_string_response(&xml).unwrap_or(xml);
Ok(version)
}
Err(e) => Err(ServerFnError::new(format!("Failed to get version: {}", e))),
}
}

View File

@@ -1,3 +1,5 @@
#![cfg(feature = "ssr")]
use crate::scgi::{send_request, ScgiError, ScgiRequest};
use quick_xml::de::from_str;
use quick_xml::se::to_string;

View File

@@ -1 +0,0 @@
{"v":1}

View File

@@ -1,6 +0,0 @@
{
"git": {
"sha1": "831c97016aa3d8f7851999aa1deea8407e7cbd42"
},
"path_in_vcs": ""
}

View File

@@ -1,8 +0,0 @@
version: 2
updates:
- package-ecosystem: cargo
directory: "/"
schedule:
interval: daily
time: "04:00"
open-pull-requests-limit: 10

View File

@@ -1,17 +0,0 @@
name: Close inactive issues
on:
schedule:
- cron: "30 1 * * *"
jobs:
close-issues:
runs-on: ubuntu-latest
permissions:
issues: write
pull-requests: write
steps:
- uses: actions/stale@v9
with:
stale-issue-message: "This issue is stale because it has been open for 30 days with no activity."
close-issue-message: "This issue was closed because it has been inactive for 14 days since being marked as stale."
repo-token: ${{ secrets.GITHUB_TOKEN }}

View File

@@ -1,4 +0,0 @@
target
Cargo.lock
.vscode
zig-cache

View File

@@ -1,82 +0,0 @@
# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO
#
# When uploading crates to the registry Cargo will automatically
# "normalize" Cargo.toml files for maximal compatibility
# with all versions of Cargo and also rewrite `path` dependencies
# to registry (e.g., crates.io) dependencies.
#
# If you are reading this file be aware that the original Cargo.toml
# will likely look very different (and much more reasonable).
# See Cargo.toml.orig for the original contents.
[package]
edition = "2018"
name = "coarsetime"
version = "0.1.37"
authors = ["Frank Denis <github@pureftpd.org>"]
build = false
autolib = false
autobins = false
autoexamples = false
autotests = false
autobenches = false
description = "Time and duration crate optimized for speed"
homepage = "https://github.com/jedisct1/rust-coarsetime"
readme = "README.md"
keywords = [
"time",
"date",
"duration",
]
categories = [
"concurrency",
"date-and-time",
"os",
]
license = "BSD-2-Clause"
repository = "https://github.com/jedisct1/rust-coarsetime"
[features]
wasi-abi2 = ["dep:wasi-abi2"]
[lib]
name = "coarsetime"
path = "src/lib.rs"
[[bench]]
name = "benchmark"
path = "benches/benchmark.rs"
harness = false
[dev-dependencies.benchmark-simple]
version = "0.1.10"
[dependencies.portable-atomic]
version = "1.6"
[target.'cfg(all(any(target_arch = "wasm32", target_arch = "wasm64"), target_os = "unknown"))'.dependencies.wasm-bindgen]
version = "0.2"
[target.'cfg(any(target_os = "wasix", target_os = "wasi"))'.dependencies.wasix]
version = "0.13"
[target.'cfg(not(any(target_os = "wasix", target_os = "wasi")))'.dependencies.libc]
version = "0.2"
[target.'cfg(target_os = "wasi")'.dependencies.wasi-abi2]
version = "0.14.7"
optional = true
package = "wasi"
[profile.bench]
codegen-units = 1
[profile.dev]
overflow-checks = true
[profile.release]
opt-level = 3
lto = true
codegen-units = 1
panic = "abort"
incremental = false

View File

@@ -1,47 +0,0 @@
[package]
name = "coarsetime"
version = "0.1.37"
description = "Time and duration crate optimized for speed"
authors = ["Frank Denis <github@pureftpd.org>"]
keywords = ["time", "date", "duration"]
readme = "README.md"
license = "BSD-2-Clause"
homepage = "https://github.com/jedisct1/rust-coarsetime"
repository = "https://github.com/jedisct1/rust-coarsetime"
categories = ["concurrency", "date-and-time", "os"]
edition = "2018"
[features]
wasi-abi2 = ["dep:wasi-abi2"]
[target.'cfg(not(any(target_os = "wasix", target_os = "wasi")))'.dependencies]
libc = "0.2"
[target.'cfg(target_os = "wasi")'.dependencies]
wasi-abi2 = { package = "wasi", version = "0.14.7", optional = true }
[target.'cfg(any(target_os = "wasix", target_os = "wasi"))'.dependencies]
wasix = "0.13"
[target.'cfg(all(any(target_arch = "wasm32", target_arch = "wasm64"), target_os = "unknown"))'.dependencies]
wasm-bindgen = "0.2"
[dev-dependencies]
benchmark-simple = "0.1.10"
[profile.bench]
codegen-units = 1
[[bench]]
name = "benchmark"
harness = false
[profile.release]
lto = true
panic = "abort"
opt-level = 3
codegen-units = 1
incremental = false
[profile.dev]
overflow-checks=true

View File

@@ -1,25 +0,0 @@
BSD 2-Clause License
Copyright (c) 2016-2026, Frank Denis
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

View File

@@ -1,90 +0,0 @@
[![Documentation](https://docs.rs/coarsetime/badge.svg)](https://docs.rs/coarsetime)
[![Windows build status](https://ci.appveyor.com/api/projects/status/xlbhk9850dvl5ylh?svg=true)](https://ci.appveyor.com/project/jedisct1/rust-coarsetime)
# coarsetime
A Rust crate to make time measurements, that focuses on speed, API stability and portability.
This crate is a partial replacement for the `Time` and `Duration` structures
from the standard library, with the following differences:
* Speed is privileged over accuracy. In particular, `CLOCK_MONOTONIC_COARSE` is
used to retrieve the clock value on Linux systems, and transformations avoid
operations that can be slow on non-Intel systems.
* The number of system calls can be kept to a minimum. The "most recent
timestamp" is always kept in memory. It can be read with just a load operation,
and can be updated only as frequently as necessary.
* The API is stable, and the same for all platforms. Unlike the standard library, it doesn't silently compile functions that do nothing but panic at runtime on some platforms.
# Installation
`coarsetime` is available on [crates.io](https://crates.io/crates/coarsetime)
and works on Rust stable, beta, and nightly.
Windows and Unix-like systems are supported.
Available feature:
* `wasi-abi2`: when targeting WASI, use the second preview of the ABI. Default is to use the regular WASI-core ABI.
# Documentation
[API documentation](https://docs.rs/coarsetime)
# Example
```rust
extern crate coarsetime;
use coarsetime::{Duration, Instant, Updater};
// Get the current instant. This may require a system call, but it may also
// be faster than the stdlib equivalent.
let now = Instant::now();
// Get the latest known instant. This operation is super fast.
// In this case, the value will be identical to `now`, because we haven't
// updated the latest known instant yet.
let ts1 = Instant::recent();
// Update the latest known instant. This may require a system call.
// Note that a call to `Instant::now()` also updates the stored instant.
Instant::update();
// Now, we may get a different instant. This call is also super fast.
let ts2 = Instant::recent();
// Compute the time elapsed between ts2 and ts1.
let elapsed_ts2_ts1 = ts2.duration_since(ts1);
// Operations such as `+` and `-` between `Instant` and `Duration` are also
// available.
let elapsed_ts2_ts1 = ts2 - ts1;
// Returns the time elapsed since ts1.
// This retrieves the actual current time, and may require a system call.
let elapsed_since_ts1 = ts1.elapsed();
// Returns the approximate time elapsed since ts1.
// This uses the latest known instant, and is super fast.
let elapsed_since_recent = ts1.elapsed_since_recent();
// Instant::update() should be called periodically, for example using an
// event loop. Alternatively, the crate provides an easy way to spawn a
// background task that will periodically update the latest known instant.
// Here, the update will happen every 250ms.
let updater = Updater::new(250).start().unwrap();
// From now on, Instant::recent() will always return an approximation of the
// current instant.
let ts3 = Instant::recent();
// Stop the task.
updater.stop().unwrap();
// Returns the elapsed time since the UNIX epoch
let unix_timestamp = Clock::now_since_epoch();
// Returns an approximation of the elapsed time since the UNIX epoch, based on
// the latest time update
let unix_timestamp_approx = Clock::recent_since_epoch();
```

View File

@@ -1,61 +0,0 @@
use benchmark_simple::*;
use coarsetime::*;
use std::time;
fn main() {
let options = &Options {
iterations: 250_000,
warmup_iterations: 25_000,
min_samples: 5,
max_samples: 10,
max_rsd: 1.0,
..Default::default()
};
bench_coarsetime_now(options);
bench_coarsetime_recent(options);
bench_coarsetime_elapsed(options);
bench_coarsetime_elapsed_since_recent(options);
bench_stdlib_now(options);
bench_stdlib_elapsed(options);
}
fn bench_coarsetime_now(options: &Options) {
let b = Bench::new();
Instant::update();
let res = b.run(options, Instant::now);
println!("coarsetime_now(): {}", res.throughput(1));
}
fn bench_coarsetime_recent(options: &Options) {
let b = Bench::new();
Instant::update();
let res = b.run(options, Instant::recent);
println!("coarsetime_recent(): {}", res.throughput(1));
}
fn bench_coarsetime_elapsed(options: &Options) {
let b = Bench::new();
let ts = Instant::now();
let res = b.run(options, || ts.elapsed());
println!("coarsetime_elapsed(): {}", res.throughput(1));
}
fn bench_coarsetime_elapsed_since_recent(options: &Options) {
let b = Bench::new();
let ts = Instant::now();
let res = b.run(options, || ts.elapsed_since_recent());
println!("coarsetime_since_recent(): {}", res.throughput(1));
}
fn bench_stdlib_now(options: &Options) {
let b = Bench::new();
let res = b.run(options, time::Instant::now);
println!("stdlib_now(): {}", res.throughput(1));
}
fn bench_stdlib_elapsed(options: &Options) {
let b = Bench::new();
let ts = time::Instant::now();
let res = b.run(options, || ts.elapsed());
println!("stdlib_elapsed(): {}", res.throughput(1));
}

View File

@@ -1,55 +0,0 @@
use std::thread::sleep;
use std::time;
#[cfg(not(any(target_arch = "wasm32", target_arch = "wasm64")))]
use super::Updater;
use super::{Clock, Duration, Instant};
#[test]
fn tests() {
let ts = Instant::now();
let d = Duration::from_secs(2);
sleep(time::Duration::new(3, 0));
let elapsed = ts.elapsed().as_secs();
println!("Elapsed: {elapsed} secs");
assert!(elapsed >= 2);
assert!(elapsed < 100);
assert!(ts.elapsed_since_recent() > d);
let ts = Instant::now();
sleep(time::Duration::new(1, 0));
assert_eq!(Instant::recent(), ts);
Instant::update();
assert!(Instant::recent() > ts);
let clock_now = Clock::recent_since_epoch();
sleep(time::Duration::new(1, 0));
assert_eq!(Clock::recent_since_epoch(), clock_now);
assert!(Clock::now_since_epoch() > clock_now);
#[cfg(not(any(target_arch = "wasm32", target_arch = "wasm64")))]
tests_updater();
}
#[cfg(not(any(target_arch = "wasm32", target_arch = "wasm64")))]
#[test]
fn tests_updater() {
let updater = Updater::new(250)
.start()
.expect("Unable to start a background updater");
let ts = Instant::recent();
let clock_recent = Clock::recent_since_epoch();
sleep(time::Duration::new(2, 0));
assert!(Clock::recent_since_epoch() > clock_recent);
assert!(Instant::recent() != ts);
updater.stop().unwrap();
let clock_recent = Clock::recent_since_epoch();
sleep(time::Duration::new(1, 0));
assert_eq!(Clock::recent_since_epoch(), clock_recent);
}
#[test]
fn tests_duration() {
let duration = Duration::from_days(1000);
assert_eq!(duration.as_days(), 1000);
}

BIN
vibetorrent.db Normal file

Binary file not shown.

BIN
vibetorrent.db-shm Normal file

Binary file not shown.

BIN
vibetorrent.db-wal Normal file

Binary file not shown.