diff options
| author | metamuffin <metamuffin@disroot.org> | 2026-03-02 15:53:12 +0100 |
|---|---|---|
| committer | metamuffin <metamuffin@disroot.org> | 2026-03-02 15:53:12 +0100 |
| commit | 516f87ac2c7dcd48d457912ebec1d5b5e6bf7e3a (patch) | |
| tree | 09535e2562c26a24acd43387fc2888e97c0d9694 /server/src/routes/admin/import.rs | |
| parent | f9d5bfc7ba1f9dba714e71c39c63a5f4622ebd85 (diff) | |
| download | jellything-516f87ac2c7dcd48d457912ebec1d5b5e6bf7e3a.tar jellything-516f87ac2c7dcd48d457912ebec1d5b5e6bf7e3a.tar.bz2 jellything-516f87ac2c7dcd48d457912ebec1d5b5e6bf7e3a.tar.zst | |
move files around
Diffstat (limited to 'server/src/routes/admin/import.rs')
| -rw-r--r-- | server/src/routes/admin/import.rs | 78 |
1 files changed, 78 insertions, 0 deletions
diff --git a/server/src/routes/admin/import.rs b/server/src/routes/admin/import.rs new file mode 100644 index 0000000..31e7d70 --- /dev/null +++ b/server/src/routes/admin/import.rs @@ -0,0 +1,78 @@ +/* + This file is part of jellything (https://codeberg.org/metamuffin/jellything) + which is licensed under the GNU Affero General Public License (version 3); see /COPYING. + Copyright (C) 2026 metamuffin <metamuffin.org> +*/ + +use crate::{request_info::RequestInfo, routes::error::MyResult}; +use jellycommon::routes::u_admin_import; +use jellyimport::{ + ImportConfig, import_wrap, is_importing, + reporting::{IMPORT_ERRORS, IMPORT_PROGRESS}, +}; +use jellyui::{components::admin::AdminImport, tr}; +use rocket::{ + get, post, + response::{Flash, Redirect, content::RawHtml}, +}; +use rocket_ws::{Message, Stream, WebSocket}; +use std::time::Duration; +use tokio::{spawn, time::sleep}; + +#[get("/admin/import", rank = 2)] +pub async fn r_admin_import(ri: RequestInfo<'_>) -> MyResult<RawHtml<String>> { + ri.require_admin()?; + + let last_import_err = IMPORT_ERRORS.read().await.clone(); + let last_import_err = last_import_err + .iter() + .map(|e| e.as_str()) + .collect::<Vec<_>>(); + + Ok(ri.respond_ui(&AdminImport { + busy: is_importing(), + errors: &last_import_err, + ri: &ri.render_info(), + })) +} + +#[post("/admin/import?<incremental>")] +pub async fn r_admin_import_post( + ri: RequestInfo<'_>, + incremental: bool, +) -> MyResult<Flash<Redirect>> { + ri.require_admin()?; + spawn(async move { + let _ = import_wrap( + ImportConfig { + config: ri.state.config.import.clone(), + cache: ri.state.cache.clone(), + db: ri.state.database.clone(), + }, + incremental, + ) + .await; + }); + Ok(Flash::success( + Redirect::to(u_admin_import()), + tr(ri.lang, "admin.import_success"), + )) +} + +#[get("/admin/import", rank = 1)] +pub fn r_admin_import_stream(ri: RequestInfo<'_>, ws: WebSocket) -> MyResult<Stream!['static]> { + ri.require_admin()?; + Ok({ + Stream! { ws => + loop { + let Some(p) = IMPORT_PROGRESS.read().await.clone() else { + break; + }; + yield Message::Text(serde_json::to_string(&p).unwrap()); + sleep(Duration::from_secs_f32(0.05)).await; + } + yield Message::Text("done".to_string()); + let _ = ws; + } + }) +} |