Fix Scan Trigger by providing JWT Auth
All checks were successful
Run Tests on Code / run-tests (push) Successful in 15s

This commit is contained in:
Neshura 2024-08-07 22:50:02 +02:00
parent 3abca4c124
commit 3e27cad6a8
Signed by: Neshura
GPG key ID: 4E2D47B1374C297D

View file

@ -1,7 +1,6 @@
mod logging;
use std::collections::HashMap;
use std::fmt::format;
use std::path::{Path, PathBuf};
use axum::body::BodyDataStream;
use axum::extract::{Request, State};
@ -11,8 +10,7 @@ use axum::routing::post;
use dotenv::{dotenv, var};
use futures::TryStreamExt;
use log::LevelFilter;
use reqwest::{Error, Response};
use serde::Serialize;
use serde::{Deserialize, Serialize};
use systemd_journal_logger::JournalLog;
use tokio::fs::File;
use tokio::{fs, io};
@ -200,6 +198,11 @@ async fn upload_file(state: &mut App, request: Request) {
///$KAVITA_URL/api/Library/scan-folder
let client = reqwest::Client::new();
let jwt_url = format!("{}/api/Plugin/authenticate?apiKey={}&pluginName=kwm-api", var("KAVITA_URL").expect("KAVITA_URL needs to be set in the .env file"), var("KAVITA_API_KEY").expect("KAVITA_API_KEY needs to be set in the .env file"));
match client.post(jwt_url).send().await {
Ok(data) => {
let auth: PluginAuthResponse = serde_json::from_str(&data.text().await.unwrap()).unwrap();
let url = format!("{}/api/Library/scan-folder", var("KAVITA_URL").expect("KAVITA_URL needs to be set in the .env file"));
let request_body = LibraryScanBody {
@ -211,6 +214,7 @@ async fn upload_file(state: &mut App, request: Request) {
let res = client
.post(url)
.bearer_auth(auth.token)
.body(body)
.send()
.await;
@ -223,6 +227,14 @@ async fn upload_file(state: &mut App, request: Request) {
state.log.error(format!("Error Triggering Library Scan: {e}"));
}
}
}
Err(e) => {
}
}
}
async fn stream_to_file(path: &PathBuf, stream: BodyDataStream) -> Result<(), (StatusCode, String)>
@ -252,3 +264,15 @@ struct LibraryScanBody {
folder_path: String,
api_key: String,
}
#[derive(Deserialize)]
#[serde(rename_all="camelCase")]
struct PluginAuthResponse {
username: String,
token: String,
refresh_token: String,
api_key: String,
preferences: serde_json::Value,
age_restriction: serde_json::Value,
kavita_version: String
}