Skip to content

Commit

Permalink
Update desktop app to support new persistence config
Browse files Browse the repository at this point in the history
  • Loading branch information
ikatson committed Aug 15, 2024
1 parent d77d96b commit 275b3b0
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 22 deletions.
14 changes: 8 additions & 6 deletions crates/librqbit/src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,13 @@ pub enum SessionPersistenceConfig {
Json { folder: Option<PathBuf> },
}

impl SessionPersistenceConfig {
pub fn default_json_persistence_folder() -> anyhow::Result<PathBuf> {
let dir = get_configuration_directory("session")?;
Ok(dir.data_dir().to_owned())
}
}

#[derive(Default)]
pub struct SessionOptions {
/// Turn on to disable DHT.
Expand Down Expand Up @@ -475,16 +482,11 @@ impl Session {
async fn persistence_factory(
opts: &SessionOptions,
) -> anyhow::Result<Option<BoxSessionPersistenceStore>> {
pub fn default_persistence_folder() -> anyhow::Result<PathBuf> {
let dir = get_configuration_directory("session")?;
Ok(dir.data_dir().to_owned())
}

match &opts.persistence {
Some(SessionPersistenceConfig::Json { folder }) => {
let folder = match folder.as_ref() {
Some(f) => f.clone(),
None => default_persistence_folder()?,
None => SessionPersistenceConfig::default_json_persistence_folder()?,
};

Ok(Some(Box::new(
Expand Down
9 changes: 9 additions & 0 deletions desktop/src-tauri/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 24 additions & 3 deletions desktop/src-tauri/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use std::{
net::{Ipv4Addr, SocketAddr, SocketAddrV4},
path::PathBuf,
path::{Path, PathBuf},
time::Duration,
};

use librqbit::{dht::PersistentDht, Session};
use librqbit::dht::PersistentDht;
use serde::{Deserialize, Serialize};
use serde_with::serde_as;

Expand Down Expand Up @@ -49,14 +49,35 @@ impl Default for RqbitDesktopConfigTcpListen {
#[serde(default)]
pub struct RqbitDesktopConfigPersistence {
pub disable: bool,

#[serde(default)]
pub folder: PathBuf,

/// Deprecated, but keeping for backwards compat for serialized / deserialized config.
#[serde(default)]
pub filename: PathBuf,
}

impl RqbitDesktopConfigPersistence {
pub(crate) fn fix_backwards_compat(&mut self) {
if self.folder != Path::new("") {
return;
}
if self.filename != Path::new("") {
if let Some(parent) = self.filename.parent() {
self.folder = parent.to_owned();
}
}
}
}

impl Default for RqbitDesktopConfigPersistence {
fn default() -> Self {
let folder = librqbit::SessionPersistenceConfig::default_json_persistence_folder().unwrap();
Self {
disable: false,
filename: Session::default_persistence_filename().unwrap(),
folder,
filename: PathBuf::new(),
}
}
}
Expand Down
28 changes: 21 additions & 7 deletions desktop/src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use librqbit::{
dht::PersistentDhtConfig,
tracing_subscriber_config_utils::{init_logging, InitLoggingOptions, InitLoggingResult},
AddTorrent, AddTorrentOptions, Api, ApiError, PeerConnectionOptions, Session, SessionOptions,
SessionPersistenceConfig,
};
use parking_lot::RwLock;
use serde::Serialize;
Expand All @@ -42,7 +43,9 @@ struct State {

fn read_config(path: &str) -> anyhow::Result<RqbitDesktopConfig> {
let rdr = BufReader::new(File::open(path)?);
Ok(serde_json::from_reader(rdr)?)
let mut config: RqbitDesktopConfig = serde_json::from_reader(rdr)?;
config.persistence.fix_backwards_compat();
Ok(config)
}

fn write_config(path: &str, config: &RqbitDesktopConfig) -> anyhow::Result<()> {
Expand All @@ -65,6 +68,17 @@ async fn api_from_config(
init_logging: &InitLoggingResult,
config: &RqbitDesktopConfig,
) -> anyhow::Result<Api> {
let persistence = if config.persistence.disable {
None
} else {
Some(SessionPersistenceConfig::Json {
folder: if config.persistence.folder == Path::new("") {
None
} else {
Some(config.persistence.folder.clone())
},
})
};
let session = Session::new_with_opts(
config.default_download_location.clone(),
SessionOptions {
Expand All @@ -74,8 +88,7 @@ async fn api_from_config(
config_filename: Some(config.dht.persistence_filename.clone()),
..Default::default()
}),
persistence: !config.persistence.disable,
persistence_filename: Some(config.persistence.filename.clone()),
persistence,
peer_opts: Some(PeerConnectionOptions {
connect_timeout: Some(config.peer_opts.connect_timeout),
read_write_timeout: Some(config.peer_opts.read_write_timeout),
Expand Down Expand Up @@ -266,31 +279,31 @@ async fn torrent_action_delete(
state: tauri::State<'_, State>,
id: usize,
) -> Result<EmptyJsonResponse, ApiError> {
state.api()?.api_torrent_action_delete(id)
state.api()?.api_torrent_action_delete(id).await
}

#[tauri::command]
async fn torrent_action_pause(
state: tauri::State<'_, State>,
id: usize,
) -> Result<EmptyJsonResponse, ApiError> {
state.api()?.api_torrent_action_pause(id)
state.api()?.api_torrent_action_pause(id).await
}

#[tauri::command]
async fn torrent_action_forget(
state: tauri::State<'_, State>,
id: usize,
) -> Result<EmptyJsonResponse, ApiError> {
state.api()?.api_torrent_action_forget(id)
state.api()?.api_torrent_action_forget(id).await
}

#[tauri::command]
async fn torrent_action_start(
state: tauri::State<'_, State>,
id: usize,
) -> Result<EmptyJsonResponse, ApiError> {
state.api()?.api_torrent_action_start(id)
state.api()?.api_torrent_action_start(id).await
}

#[tauri::command]
Expand All @@ -302,6 +315,7 @@ async fn torrent_action_configure(
state
.api()?
.api_torrent_action_update_only_files(id, &only_files.into_iter().collect())
.await
}

#[tauri::command]
Expand Down
2 changes: 1 addition & 1 deletion desktop/src/configuration.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ interface RqbitDesktopConfigTcpListen {

interface RqbitDesktopConfigPersistence {
disable: boolean;
filename: PathLike;
folder: PathLike;
}

interface RqbitDesktopConfigPeerOpts {
Expand Down
10 changes: 5 additions & 5 deletions desktop/src/configure.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ export const ConfigModal: React.FC<{
};

const handleToggleChange: React.ChangeEventHandler<HTMLInputElement> = (
e,
e
) => {
const name: string = e.target.name;
const [mainField, subField] = name.split(".", 2);
Expand Down Expand Up @@ -166,7 +166,7 @@ export const ConfigModal: React.FC<{
text: "Error saving configuration",
details: e,
});
},
}
);
};

Expand Down Expand Up @@ -292,10 +292,10 @@ export const ConfigModal: React.FC<{
/>

<FormInput
label="Persistence filename"
name="persistence.filename"
label="Persistence folder"
name="persistence.folder"
inputType="text"
value={config.persistence.filename}
value={config.persistence.folder}
onChange={handleInputChange}
disabled={config.persistence.disable}
/>
Expand Down

0 comments on commit 275b3b0

Please sign in to comment.