Compare commits
6 commits
Author | SHA1 | Date | |
---|---|---|---|
7905faa411 | |||
838414f9d6 | |||
a9245c1a3c | |||
3e27cad6a8 | |||
3abca4c124 | |||
37ec615cbd |
3 changed files with 47 additions and 23 deletions
2
Cargo.lock
generated
2
Cargo.lock
generated
|
@ -34,7 +34,7 @@ dependencies = [
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "api-backend"
|
name = "api-backend"
|
||||||
version = "0.0.5"
|
version = "0.0.8"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"axum",
|
"axum",
|
||||||
"chrono",
|
"chrono",
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
[package]
|
[package]
|
||||||
authors = ["Neshura"]
|
authors = ["Neshura"]
|
||||||
name = "api-backend"
|
name = "api-backend"
|
||||||
version = "0.0.5"
|
version = "0.0.8"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
description = "API Backend For Easier Uploading To Kavita"
|
description = "API Backend For Easier Uploading To Kavita"
|
||||||
license = "GPL-3.0-or-later"
|
license = "GPL-3.0-or-later"
|
||||||
|
|
66
src/main.rs
66
src/main.rs
|
@ -1,7 +1,6 @@
|
||||||
mod logging;
|
mod logging;
|
||||||
|
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::fmt::format;
|
|
||||||
use std::path::{Path, PathBuf};
|
use std::path::{Path, PathBuf};
|
||||||
use axum::body::BodyDataStream;
|
use axum::body::BodyDataStream;
|
||||||
use axum::extract::{Request, State};
|
use axum::extract::{Request, State};
|
||||||
|
@ -11,8 +10,7 @@ use axum::routing::post;
|
||||||
use dotenv::{dotenv, var};
|
use dotenv::{dotenv, var};
|
||||||
use futures::TryStreamExt;
|
use futures::TryStreamExt;
|
||||||
use log::LevelFilter;
|
use log::LevelFilter;
|
||||||
use reqwest::{Error, Response};
|
use serde::{Deserialize, Serialize};
|
||||||
use serde::Serialize;
|
|
||||||
use systemd_journal_logger::JournalLog;
|
use systemd_journal_logger::JournalLog;
|
||||||
use tokio::fs::File;
|
use tokio::fs::File;
|
||||||
use tokio::{fs, io};
|
use tokio::{fs, io};
|
||||||
|
@ -200,29 +198,43 @@ async fn upload_file(state: &mut App, request: Request) {
|
||||||
|
|
||||||
///$KAVITA_URL/api/Library/scan-folder
|
///$KAVITA_URL/api/Library/scan-folder
|
||||||
let client = reqwest::Client::new();
|
let client = reqwest::Client::new();
|
||||||
let url = format!("{}/api/Library/scan-folder", var("KAVITA_URL").expect("KAVITA_URL needs to be set in the .env file"));
|
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"));
|
||||||
|
|
||||||
let request_body = LibraryScanBody {
|
match client.post(jwt_url).send().await {
|
||||||
directory: file.format,
|
Ok(data) => {
|
||||||
api_key: var("KAVITA_API_KEY").expect("KAVITA_API_KEY needs to be set in the .env file")
|
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 body = serde_json::to_string::<LibraryScanBody>(&request_body).expect("Unable to Parse LibraryScanBody");
|
|
||||||
|
|
||||||
let res = client
|
let request_body = LibraryScanBody {
|
||||||
.post(url)
|
folder_path: file.format,
|
||||||
.body(body)
|
api_key: var("KAVITA_API_KEY").expect("KAVITA_API_KEY needs to be set in the .env file")
|
||||||
.send()
|
};
|
||||||
.await;
|
|
||||||
|
let body = serde_json::to_string::<LibraryScanBody>(&request_body).expect("Unable to Parse LibraryScanBody");
|
||||||
match res {
|
|
||||||
Ok(_) => {
|
let res = client
|
||||||
state.log.info("Scan Triggered".to_string());
|
.post(url)
|
||||||
|
.header(reqwest::header::AUTHORIZATION, auth.token)
|
||||||
|
.body(body)
|
||||||
|
.send()
|
||||||
|
.await;
|
||||||
|
|
||||||
|
match res {
|
||||||
|
Ok(_) => {
|
||||||
|
state.log.info("Scan Triggered".to_string());
|
||||||
|
}
|
||||||
|
Err(e) => {
|
||||||
|
state.log.error(format!("Error Triggering Library Scan: {e}"));
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
state.log.error(format!("Error Triggering Library Scan: {e}"));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn stream_to_file(path: &PathBuf, stream: BodyDataStream) -> Result<(), (StatusCode, String)>
|
async fn stream_to_file(path: &PathBuf, stream: BodyDataStream) -> Result<(), (StatusCode, String)>
|
||||||
|
@ -249,6 +261,18 @@ async fn stream_to_file(path: &PathBuf, stream: BodyDataStream) -> Result<(), (S
|
||||||
#[derive(Serialize)]
|
#[derive(Serialize)]
|
||||||
#[serde(rename_all="camelCase")]
|
#[serde(rename_all="camelCase")]
|
||||||
struct LibraryScanBody {
|
struct LibraryScanBody {
|
||||||
directory: String,
|
folder_path: String,
|
||||||
api_key: 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
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue