Skip to content
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
19 changes: 17 additions & 2 deletions lib/src/epub/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -605,6 +605,11 @@ impl Display for EpubBook {
)
}
}
impl Drop for EpubBook {
fn drop(&mut self) {
self.release_memory();
}
}

impl EpubBook {
iepub_derive::option_string_method!(info, creator);
Expand Down Expand Up @@ -744,7 +749,7 @@ impl EpubBook {
self.assets.iter_mut()
}

pub fn remove_assets(&mut self,index: usize){
pub fn remove_assets(&mut self, index: usize) {
self.assets.remove(index);
}

Expand All @@ -763,7 +768,7 @@ impl EpubBook {
self.chapters.iter()
}

pub fn remove_chapter(&mut self, index: usize){
pub fn remove_chapter(&mut self, index: usize) {
self.chapters.remove(index);
}

Expand Down Expand Up @@ -860,6 +865,16 @@ impl EpubBook {
}
}

pub fn release_memory(&mut self) {
self.reader = None;
self.chapters.clear();
self.assets.clear();
self.nav.clear();
self.meta.clear();
self.cover = None;
self.version = String::new();
}

#[cfg(feature = "cache")]
pub fn cache<T: AsRef<std::path::Path>>(&self, file: T) -> IResult<()> {
std::fs::write(file, serde_json::to_string(self).unwrap())?;
Expand Down
14 changes: 5 additions & 9 deletions lib/src/epub/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -708,25 +708,21 @@ fn has_epub_type(e: &BytesStart, value: &str) -> bool {
}

#[derive(Debug, Clone)]
struct EpubReader<T> {
struct EpubReader<T: Read + Seek> {
inner: zip::ZipArchive<T>,
}

// impl <T: Read + Seek> From<Vec<u8>> for EpubReader<T> {
// fn from(value: Vec<u8>) -> Self {

// EpubReader{
// inner:
// }
impl<T: Read + Seek> Drop for EpubReader<T> {
fn drop(&mut self) {}
}

// }
// }
impl<T: Read + Seek> EpubReader<T> {
pub fn new(value: T) -> IResult<Self> {
let r = zip::ZipArchive::new(value)?;
Ok(EpubReader { inner: r })
}
}

impl<T: Read + Seek + Sync + Send> EpubReaderTrait for EpubReader<T> {
fn read(&mut self, book: &mut EpubBook) -> IResult<()> {
let reader = &mut self.inner;
Expand Down
18 changes: 17 additions & 1 deletion lib/src/mobi/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ where
Ok(true)
}

pub struct MobiReader<T> {
pub struct MobiReader<T: Read + Seek> {
reader: BufReader<T>,
pub(crate) pdb_header: PDBHeader,
pub(crate) mobi_doc_header: MOBIDOCHeader,
Expand All @@ -406,6 +406,13 @@ pub struct MobiReader<T> {
id: AtomicUsize,
}

impl<T: Read + Seek> Drop for MobiReader<T> {
fn drop(&mut self) {
self.release_memory();
self.exth_header = None;
}
}

impl<T: Read + Seek> MobiReader<T> {
pub fn new(v: T) -> IResult<MobiReader<T>> {
// let fs = std::fs::File::open(file)?;
Expand Down Expand Up @@ -649,6 +656,15 @@ impl<T: Read + Seek> MobiReader<T> {

Ok(pos)
}

pub fn release_memory(&mut self) {
if let Some(mut cache) = self.text_cache.take() {
// 显式清空 Vec,释放内存
cache.clear();
// Vec 会在离开作用域时自动释放,但 clear() 可以立即释放容量
cache.shrink_to_fit();
}
}
}

/// 文本分节
Expand Down