From b51a552ebd6216704c6ab9e974f9d84891ced44b Mon Sep 17 00:00:00 2001 From: Phil Denhoff Date: Thu, 8 Feb 2024 23:57:28 -0800 Subject: [PATCH] feat: support text file imports --- src-tauri/libcalibre/src/mime_type.rs | 8 ++++++- src-tauri/src/book.rs | 6 +++-- src-tauri/src/libs/file_formats/mod.rs | 9 +++++++ src-tauri/src/libs/file_formats/pdf.rs | 6 +++-- src-tauri/src/libs/file_formats/txt.rs | 33 ++++++++++++++++++++++++++ src/bindings.ts | 2 +- 6 files changed, 58 insertions(+), 6 deletions(-) create mode 100644 src-tauri/src/libs/file_formats/txt.rs diff --git a/src-tauri/libcalibre/src/mime_type.rs b/src-tauri/libcalibre/src/mime_type.rs index 7f08565..8431b10 100644 --- a/src-tauri/libcalibre/src/mime_type.rs +++ b/src-tauri/libcalibre/src/mime_type.rs @@ -4,6 +4,7 @@ pub enum MIMETYPE { PDF, KF7, // Kindle Format 7 — AZW files KF8, // Kindle Format 8 — AZW3 files + TXT, UNKNOWN, } @@ -16,6 +17,7 @@ impl MIMETYPE { MIMETYPE::PDF => "application/pdf", MIMETYPE::KF7 => "application/vnd.amazon.ebook", MIMETYPE::KF8 => "application/vnd.amazon.ebook-kf8", // Not a real MIME type, Amazon hasn't registered it + MIMETYPE::TXT => "text/plain", MIMETYPE::UNKNOWN => "application/octet-stream", } } @@ -28,6 +30,7 @@ impl MIMETYPE { "application/vnd.amazon.ebook" => Some(MIMETYPE::KF7), "application/pdf" => Some(MIMETYPE::PDF), "application/octet-stream" => Some(MIMETYPE::UNKNOWN), + "text/plain" => Some(MIMETYPE::TXT), _ => None, } } @@ -39,6 +42,7 @@ impl MIMETYPE { MIMETYPE::PDF => "pdf", MIMETYPE::KF7 => "azw", MIMETYPE::KF8 => "azw3", + MIMETYPE::TXT => "txt", MIMETYPE::UNKNOWN => "", } } @@ -50,6 +54,7 @@ impl MIMETYPE { "pdf" => Some(MIMETYPE::PDF), "azw" => Some(MIMETYPE::KF7), "azw3" => Some(MIMETYPE::KF8), + "txt" => Some(MIMETYPE::TXT), _ => None, } } @@ -63,8 +68,9 @@ impl PartialEq for MIMETYPE { (MIMETYPE::PDF, MIMETYPE::PDF) => true, (MIMETYPE::KF7, MIMETYPE::KF7) => true, (MIMETYPE::KF8, MIMETYPE::KF8) => true, + (MIMETYPE::TXT, MIMETYPE::TXT) => true, (MIMETYPE::UNKNOWN, MIMETYPE::UNKNOWN) => true, _ => false, } } -} \ No newline at end of file +} diff --git a/src-tauri/src/book.rs b/src-tauri/src/book.rs index 97f196a..218128d 100644 --- a/src-tauri/src/book.rs +++ b/src-tauri/src/book.rs @@ -1,4 +1,4 @@ -use std::{path::PathBuf, collections::HashMap}; +use std::{collections::HashMap, path::PathBuf}; use chrono::NaiveDate; use serde::{Deserialize, Serialize}; @@ -6,7 +6,7 @@ use serde::{Deserialize, Serialize}; #[derive(Serialize, specta::Type, Deserialize, Clone)] pub enum LocalOrRemote { Local, - Remote + Remote, } #[derive(Serialize, specta::Type, Deserialize, Clone)] @@ -61,6 +61,7 @@ pub enum ImportableBookType { EPUB = 0, PDF = 1, MOBI = 2, + TEXT = 3, } impl std::fmt::Display for ImportableBookType { @@ -69,6 +70,7 @@ impl std::fmt::Display for ImportableBookType { ImportableBookType::EPUB => write!(f, "EPUB"), ImportableBookType::PDF => write!(f, "PDF"), ImportableBookType::MOBI => write!(f, "MOBI"), + ImportableBookType::TEXT => write!(f, "TXT"), } } } diff --git a/src-tauri/src/libs/file_formats/mod.rs b/src-tauri/src/libs/file_formats/mod.rs index b8f22b4..2a385ea 100644 --- a/src-tauri/src/libs/file_formats/mod.rs +++ b/src-tauri/src/libs/file_formats/mod.rs @@ -9,6 +9,7 @@ use crate::book::{ImportableBookMetadata, ImportableBookType}; mod epub; mod mobi; mod pdf; +mod txt; #[derive(Serialize, Clone, Copy, specta::Type)] pub enum SupportedFormats { @@ -17,6 +18,7 @@ pub enum SupportedFormats { PDF, KF7, // Kindle Format 7 — AZW files KF8, // Kindle Format 8 — AZW3 files + TXT, UNKNOWN, } @@ -28,6 +30,7 @@ impl SupportedFormats { SupportedFormats::PDF, SupportedFormats::KF7, SupportedFormats::KF8, + SupportedFormats::TXT, SupportedFormats::UNKNOWN, ] } @@ -50,6 +53,7 @@ impl SupportedFormats { Self::PDF => "pdf", Self::KF7 => "azw", Self::KF8 => "azw3", + Self::TXT => "txt", Self::UNKNOWN => "", } } @@ -61,6 +65,7 @@ impl SupportedFormats { "pdf" => Some(Self::PDF), "azw" => Some(Self::KF7), "azw3" => Some(Self::KF8), + "txt" => Some(Self::TXT), _ => None, } } @@ -126,6 +131,10 @@ pub fn get_importable_file_metadata(file: ImportableFile) -> Option Some(metadata.to_importable_book_metadata()), _ => None, }, + Some(SupportedFormats::TXT) => match txt::read_metadata(&file.path) { + Some(metadata) => Some(metadata.to_importable_book_metadata()), + _ => None, + }, _ => None, } } diff --git a/src-tauri/src/libs/file_formats/pdf.rs b/src-tauri/src/libs/file_formats/pdf.rs index 24284b5..701cdeb 100644 --- a/src-tauri/src/libs/file_formats/pdf.rs +++ b/src-tauri/src/libs/file_formats/pdf.rs @@ -1,5 +1,7 @@ use std::path::{Path, PathBuf}; +use crate::book::ImportableBookType; + pub struct PdfMetadata { pub title: String, pub path: PathBuf, @@ -8,7 +10,7 @@ impl PdfMetadata { pub fn to_importable_book_metadata(&self) -> super::ImportableBookMetadata { super::ImportableBookMetadata { title: self.title.clone(), - file_type: crate::book::ImportableBookType::PDF, + file_type: ImportableBookType::PDF, path: self.path.clone(), author_names: None, identifier: None, @@ -28,4 +30,4 @@ pub fn read_metadata(path: &Path) -> Option { title: title.to_string(), path: path.to_path_buf(), }) -} \ No newline at end of file +} diff --git a/src-tauri/src/libs/file_formats/txt.rs b/src-tauri/src/libs/file_formats/txt.rs new file mode 100644 index 0000000..07c45c3 --- /dev/null +++ b/src-tauri/src/libs/file_formats/txt.rs @@ -0,0 +1,33 @@ +use std::path::{Path, PathBuf}; + +use crate::book::ImportableBookType; + +pub struct TxtMetadata { + pub title: String, + pub path: PathBuf, +} +impl TxtMetadata { + pub fn to_importable_book_metadata(&self) -> super::ImportableBookMetadata { + super::ImportableBookMetadata { + title: self.title.clone(), + file_type: ImportableBookType::TEXT, + path: self.path.clone(), + author_names: None, + identifier: None, + publisher: None, + language: None, + tags: vec![], + publication_date: None, + file_contains_cover: false, + } + } +} + +pub fn read_metadata(path: &Path) -> Option { + let title = path.file_stem()?.to_str()?; + + Some(TxtMetadata { + title: title.to_string(), + path: path.to_path_buf(), + }) +} diff --git a/src/bindings.ts b/src/bindings.ts index 88425e3..7627bbf 100644 --- a/src/bindings.ts +++ b/src/bindings.ts @@ -78,7 +78,7 @@ path: string; publication_date: string | null; * True if a cover image can be extracted from the file at `path`. */ file_contains_cover: boolean } -export type ImportableBookType = "EPUB" | "PDF" | "MOBI" +export type ImportableBookType = "EPUB" | "PDF" | "MOBI" | "TEXT" export type ImportableFile = { path: string } export type LibraryAuthor = { id: string; name: string; sortable_name: string } export type LibraryBook = { id: string; uuid: string | null; title: string; author_list: LibraryAuthor[]; sortable_title: string | null; author_sort_lookup: { [key in string]: string } | null; file_list: BookFile[]; cover_image: LocalOrRemoteUrl | null }