Skip to content

Commit 6ed07b0

Browse files
committed
feat: hacky way of creating Calibre-compat. library
1 parent 8e8de40 commit 6ed07b0

File tree

11 files changed

+318
-17
lines changed

11 files changed

+318
-17
lines changed

src-tauri/Cargo.lock

Lines changed: 132 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src-tauri/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ tauri-plugin-persisted-scope = { git = "https://github.com/tauri-apps/plugins-wo
3232
tauri-specta = { version = "=2.0.0-rc.4", features = ["javascript", "typescript"] }
3333
urlencoding = "2.1.3"
3434
uuid = { version = "1.6.1", features = [ "v4", "fast-rng", ] }
35+
zip = "0.6"
3536

3637
[features]
3738
# this feature is used for production builds or when `devPath` points to the filesystem and the built-in dev server is disabled.

src-tauri/libcalibre/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ pub mod domain;
33
pub mod infrastructure;
44
pub mod mime_type;
55
mod models;
6-
mod persistence;
6+
pub mod persistence;
77
mod schema;
88
pub mod util;
99

10.3 KB
Binary file not shown.

src-tauri/src/libs/calibre/mod.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use crate::libs::file_formats::read_epub_metadata;
1212

1313
use chrono::NaiveDate;
1414
use chrono::NaiveDateTime;
15+
use diesel::RunQueryDsl;
1516
use libcalibre::application::services::domain::author::dto::NewAuthorDto;
1617
use libcalibre::application::services::domain::author::service::AuthorService;
1718
use libcalibre::application::services::domain::author::service::AuthorServiceTrait;
@@ -246,3 +247,39 @@ pub fn update_book(library_path: String, book_id: String, updates: BookUpdate) -
246247
}
247248
}
248249
}
250+
251+
#[tauri::command]
252+
#[specta::specta]
253+
pub fn create_library(handle: tauri::AppHandle, library_root: String) -> Result<(), String> {
254+
let resource_path = handle
255+
.path_resolver()
256+
.resolve_resource("resources/empty_7_2_calibre_lib.zip")
257+
.expect("Failed to find default empty library");
258+
259+
let file = std::fs::File::open(resource_path).expect("Failed to open default empty library");
260+
261+
// Extract the default library to the specified location
262+
let mut archive = zip::ZipArchive::new(file).expect("Failed to read zip archive");
263+
let result = archive.extract(&library_root).map_err(|e| e.to_string());
264+
265+
// Set a new UUID for the library
266+
match libcalibre::util::get_db_path(&library_root) {
267+
None => return Err("Failed to open database".to_string()),
268+
Some(db_path) => {
269+
let mut conn = libcalibre::persistence::establish_connection(&db_path)
270+
.expect("Failed to open database");
271+
diesel::sql_query("UPDATE library_id SET uuid = uuid4()")
272+
.execute(&mut conn)
273+
.expect("Failed to set new UUID");
274+
}
275+
}
276+
277+
result
278+
}
279+
280+
#[tauri::command]
281+
#[specta::specta]
282+
pub fn is_valid_library(library_root: String) -> bool {
283+
let db_path = libcalibre::util::get_db_path(&library_root);
284+
db_path.is_some()
285+
}

src-tauri/src/main.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ fn run_tauri_backend() -> std::io::Result<()> {
3131
libs::calibre::check_file_importable,
3232
libs::calibre::add_book_to_db_by_metadata,
3333
libs::calibre::update_book,
34+
libs::calibre::is_valid_library,
35+
libs::calibre::create_library,
3436
])
3537
.config(ExportConfig::default().bigint(BigIntExportBehavior::BigInt));
3638

src-tauri/tauri.conf.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
"readDir": true,
2929
"readFile": true,
3030
"writeFile": true,
31-
"scope": ["$APPCONFIG", "$APPCONFIG/*"]
31+
"scope": ["$APPCONFIG", "$APPCONFIG/*", "$RESOURCE/*"]
3232
},
3333
"protocol": {
3434
"asset": true,
@@ -62,7 +62,9 @@
6262
"providerShortName": null,
6363
"signingIdentity": null
6464
},
65-
"resources": [],
65+
"resources": [
66+
"resources/empty_7_2_calibre_lib.zip"
67+
],
6668
"shortDescription": "",
6769
"targets": "all",
6870
"windows": {

src/bindings.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,17 @@ try {
2929
if(e instanceof Error) throw e;
3030
else return { status: "error", error: e as any };
3131
}
32+
},
33+
async isValidLibrary(libraryRoot: string) : Promise<boolean> {
34+
return await TAURI_INVOKE("plugin:tauri-specta|is_valid_library", { libraryRoot });
35+
},
36+
async createLibrary(libraryRoot: string) : Promise<__Result__<null, string>> {
37+
try {
38+
return { status: "ok", data: await TAURI_INVOKE("plugin:tauri-specta|create_library", { libraryRoot }) };
39+
} catch (e) {
40+
if(e instanceof Error) throw e;
41+
else return { status: "error", error: e as any };
42+
}
3243
}
3344
}
3445

0 commit comments

Comments
 (0)