Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support text file imports #24

Merged
merged 1 commit into from
Feb 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src-tauri/libcalibre/src/mime_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ pub enum MIMETYPE {
PDF,
KF7, // Kindle Format 7 — AZW files
KF8, // Kindle Format 8 — AZW3 files
TXT,
UNKNOWN,
}

Expand All @@ -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",
}
}
Expand All @@ -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,
}
}
Expand All @@ -39,6 +42,7 @@ impl MIMETYPE {
MIMETYPE::PDF => "pdf",
MIMETYPE::KF7 => "azw",
MIMETYPE::KF8 => "azw3",
MIMETYPE::TXT => "txt",
MIMETYPE::UNKNOWN => "",
}
}
Expand All @@ -50,6 +54,7 @@ impl MIMETYPE {
"pdf" => Some(MIMETYPE::PDF),
"azw" => Some(MIMETYPE::KF7),
"azw3" => Some(MIMETYPE::KF8),
"txt" => Some(MIMETYPE::TXT),
_ => None,
}
}
Expand All @@ -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,
}
}
}
}
6 changes: 4 additions & 2 deletions src-tauri/src/book.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use std::{path::PathBuf, collections::HashMap};
use std::{collections::HashMap, path::PathBuf};

use chrono::NaiveDate;
use serde::{Deserialize, Serialize};

#[derive(Serialize, specta::Type, Deserialize, Clone)]
pub enum LocalOrRemote {
Local,
Remote
Remote,
}

#[derive(Serialize, specta::Type, Deserialize, Clone)]
Expand Down Expand Up @@ -61,6 +61,7 @@ pub enum ImportableBookType {
EPUB = 0,
PDF = 1,
MOBI = 2,
TEXT = 3,
}

impl std::fmt::Display for ImportableBookType {
Expand All @@ -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"),
}
}
}
Expand Down
9 changes: 9 additions & 0 deletions src-tauri/src/libs/file_formats/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -17,6 +18,7 @@ pub enum SupportedFormats {
PDF,
KF7, // Kindle Format 7 — AZW files
KF8, // Kindle Format 8 — AZW3 files
TXT,
UNKNOWN,
}

Expand All @@ -28,6 +30,7 @@ impl SupportedFormats {
SupportedFormats::PDF,
SupportedFormats::KF7,
SupportedFormats::KF8,
SupportedFormats::TXT,
SupportedFormats::UNKNOWN,
]
}
Expand All @@ -50,6 +53,7 @@ impl SupportedFormats {
Self::PDF => "pdf",
Self::KF7 => "azw",
Self::KF8 => "azw3",
Self::TXT => "txt",
Self::UNKNOWN => "",
}
}
Expand All @@ -61,6 +65,7 @@ impl SupportedFormats {
"pdf" => Some(Self::PDF),
"azw" => Some(Self::KF7),
"azw3" => Some(Self::KF8),
"txt" => Some(Self::TXT),
_ => None,
}
}
Expand Down Expand Up @@ -126,6 +131,10 @@ pub fn get_importable_file_metadata(file: ImportableFile) -> Option<ImportableBo
Some(metadata) => 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,
}
}
6 changes: 4 additions & 2 deletions src-tauri/src/libs/file_formats/pdf.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use std::path::{Path, PathBuf};

use crate::book::ImportableBookType;

pub struct PdfMetadata {
pub title: String,
pub path: PathBuf,
Expand All @@ -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,
Expand All @@ -28,4 +30,4 @@ pub fn read_metadata(path: &Path) -> Option<PdfMetadata> {
title: title.to_string(),
path: path.to_path_buf(),
})
}
}
33 changes: 33 additions & 0 deletions src-tauri/src/libs/file_formats/txt.rs
Original file line number Diff line number Diff line change
@@ -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<TxtMetadata> {
let title = path.file_stem()?.to_str()?;

Some(TxtMetadata {
title: title.to_string(),
path: path.to_path_buf(),
})
}
2 changes: 1 addition & 1 deletion src/bindings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down