Add a desktop app with tauri

This commit is contained in:
Igor Katson 2023-12-02 22:24:36 +00:00
parent 28c2db2a37
commit 30da40555a
No known key found for this signature in database
GPG key ID: B4EC22B66D61A3F5
42 changed files with 15389 additions and 0 deletions

4
desktop/src-tauri/.gitignore vendored Normal file
View file

@ -0,0 +1,4 @@
# Generated by Cargo
# will have compiled files and executables
/target/

4837
desktop/src-tauri/Cargo.lock generated Normal file

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,32 @@
[package]
name = "rqbit"
version = "0.0.0"
description = "A Tauri App"
authors = ["you"]
license = ""
repository = ""
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[build-dependencies]
tauri-build = { version = "1.5", features = [] }
[dependencies]
tauri = { version = "1.5", features = ["shell-open"] }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
librqbit = {path = "../../crates/librqbit", features = ["webui"] }
tokio = { version = "1.34.0", features = ["rt-multi-thread"] }
anyhow = "1.0.75"
base64 = "0.21.5"
http = "1.0.0"
directories = "5.0.1"
tracing-subscriber = "0.3.18"
[features]
# this feature is used for production builds or when `devPath` points to the filesystem
# DO NOT REMOVE!!
custom-protocol = ["tauri/custom-protocol"]
[workspace]

View file

@ -0,0 +1,3 @@
fn main() {
tauri_build::build()
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 17 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 52 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 13 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 20 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 21 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 62 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 73 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.2 KiB

Binary file not shown.

Binary file not shown.

After

Width:  |  Height:  |  Size: 68 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 159 KiB

View file

@ -0,0 +1,150 @@
// Prevents additional console window on Windows in release, DO NOT REMOVE!!
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
use http::StatusCode;
use librqbit::{
api::{
Api, ApiAddTorrentResponse, EmptyJsonResponse, TorrentDetailsResponse, TorrentListResponse,
},
api_error::ApiError,
session::AddTorrentOptions,
torrent_state::stats::TorrentStats,
};
struct State {
api: Api,
}
#[tauri::command]
fn torrents_list(state: tauri::State<State>) -> TorrentListResponse {
state.api.api_torrent_list()
}
#[tauri::command]
async fn torrent_create_from_url(
state: tauri::State<'_, State>,
url: String,
opts: Option<AddTorrentOptions>,
) -> Result<ApiAddTorrentResponse, ApiError> {
state
.api
.api_add_torrent(librqbit::session::AddTorrent::Url(url.into()), opts)
.await
}
#[tauri::command]
async fn torrent_create_from_base64_file(
state: tauri::State<'_, State>,
contents: String,
opts: Option<AddTorrentOptions>,
) -> Result<ApiAddTorrentResponse, ApiError> {
use base64::{engine::general_purpose, Engine as _};
let bytes = general_purpose::STANDARD_NO_PAD
.decode(&contents)
.map_err(|_| ApiError::new_from_string(StatusCode::BAD_REQUEST, "invalid base64".into()))?;
state
.api
.api_add_torrent(
librqbit::session::AddTorrent::TorrentFileBytes(bytes.into()),
opts,
)
.await
}
#[tauri::command]
async fn torrent_details(
state: tauri::State<'_, State>,
id: usize,
) -> Result<TorrentDetailsResponse, ApiError> {
state.api.api_torrent_details(id)
}
#[tauri::command]
async fn torrent_stats(
state: tauri::State<'_, State>,
id: usize,
) -> Result<TorrentStats, ApiError> {
state.api.api_stats_v1(id)
}
#[tauri::command]
async fn torrent_action_delete(
state: tauri::State<'_, State>,
id: usize,
) -> Result<EmptyJsonResponse, ApiError> {
state.api.api_torrent_action_delete(id)
}
#[tauri::command]
async fn torrent_action_pause(
state: tauri::State<'_, State>,
id: usize,
) -> Result<EmptyJsonResponse, ApiError> {
state.api.api_torrent_action_pause(id)
}
#[tauri::command]
async fn torrent_action_forget(
state: tauri::State<'_, State>,
id: usize,
) -> Result<EmptyJsonResponse, ApiError> {
state.api.api_torrent_action_forget(id)
}
#[tauri::command]
async fn torrent_action_start(
state: tauri::State<'_, State>,
id: usize,
) -> Result<EmptyJsonResponse, ApiError> {
state.api.api_torrent_action_start(id)
}
async fn start_session() {
tauri::async_runtime::set(tokio::runtime::Handle::current());
let download_folder = directories::UserDirs::new()
.expect("directories::UserDirs::new()")
.download_dir()
.expect("download_dir()")
.to_path_buf();
let s = librqbit::session::Session::new_with_opts(
download_folder,
Default::default(),
librqbit::session::SessionOptions {
disable_dht: false,
disable_dht_persistence: false,
persistence: true,
..Default::default()
},
)
.await
.expect("couldn't set up librqbit session");
let api = Api::new(s, None);
tauri::Builder::default()
.manage(State { api })
.invoke_handler(tauri::generate_handler![
torrents_list,
torrent_details,
torrent_stats,
torrent_create_from_url,
torrent_action_delete,
torrent_action_pause,
torrent_action_forget,
torrent_action_start,
torrent_create_from_base64_file,
])
.run(tauri::generate_context!())
.expect("error while running tauri application");
}
fn main() {
tracing_subscriber::fmt::init();
tokio::runtime::Builder::new_multi_thread()
.enable_all()
.build()
.expect("couldn't set up tokio runtime")
.block_on(start_session())
}

View file

@ -0,0 +1,45 @@
{
"build": {
"beforeDevCommand": "npm run dev",
"beforeBuildCommand": "npm run build",
"devPath": "http://localhost:1420",
"distDir": "../dist"
},
"package": {
"productName": "rqbit",
"version": "0.0.0"
},
"tauri": {
"allowlist": {
"all": false,
"shell": {
"all": false,
"open": true
}
},
"bundle": {
"active": true,
"targets": "all",
"identifier": "com.rqbit.desktop",
"icon": [
"icons/32x32.png",
"icons/128x128.png",
"icons/128x128@2x.png",
"icons/icon.icns",
"icons/icon.ico"
]
},
"security": {
"csp": null
},
"windows": [
{
"fullscreen": false,
"resizable": true,
"title": "rqbit",
"width": 800,
"height": 600
}
]
}
}