From a4ffeea3d53066f17e6cff8b5d25e39d6a4725ea Mon Sep 17 00:00:00 2001 From: ag2s20150909 Date: Fri, 23 Apr 2021 17:28:43 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AF=BC=E5=87=BAepub=E6=97=B6=EF=BC=8C?= =?UTF-8?q?=E5=9B=BE=E7=89=87=E4=BD=BF=E7=94=A8LazyResource=E6=9D=A5?= =?UTF-8?q?=E9=81=BF=E5=85=8DOOM?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/io/legado/app/help/BookHelp.kt | 2 +- .../app/ui/book/cache/CacheViewModel.kt | 12 ++++-- .../epublib/domain/FileResourceProvider.java | 38 +++++++++++++++++++ .../java/me/ag2s/epublib/util/IOUtil.java | 1 + 4 files changed, 49 insertions(+), 4 deletions(-) create mode 100644 epublib/src/main/java/me/ag2s/epublib/domain/FileResourceProvider.java diff --git a/app/src/main/java/io/legado/app/help/BookHelp.kt b/app/src/main/java/io/legado/app/help/BookHelp.kt index aa89a5505..ee8dd0120 100644 --- a/app/src/main/java/io/legado/app/help/BookHelp.kt +++ b/app/src/main/java/io/legado/app/help/BookHelp.kt @@ -113,7 +113,7 @@ object BookHelp { ) } - private fun getImageSuffix(src: String): String { + fun getImageSuffix(src: String): String { var suffix = src.substringAfterLast(".").substringBefore(",") if (suffix.length > 5) { suffix = ".jpg" diff --git a/app/src/main/java/io/legado/app/ui/book/cache/CacheViewModel.kt b/app/src/main/java/io/legado/app/ui/book/cache/CacheViewModel.kt index 55ecb17eb..819686dcf 100644 --- a/app/src/main/java/io/legado/app/ui/book/cache/CacheViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/book/cache/CacheViewModel.kt @@ -24,7 +24,6 @@ import me.ag2s.epublib.epub.EpubWriter import me.ag2s.epublib.util.ResourceUtil import java.io.ByteArrayOutputStream import java.io.File -import java.io.FileInputStream import java.io.FileOutputStream import java.nio.charset.Charset @@ -184,6 +183,7 @@ class CacheViewModel(application: Application) : BaseViewModel(application) { } } + private fun exportEpub(file: File, book: Book) { val filename = "${book.name} by ${book.author}.epub" val epubBook = EpubBook() @@ -254,9 +254,12 @@ class CacheViewModel(application: Application) : BaseViewModel(application) { } private fun setPic(src: String, book: Book, epubBook: EpubBook) { + + val href = "${MD5Utils.md5Encode16(src)}${BookHelp.getImageSuffix(src)}" val vFile = BookHelp.getImage(book, src) + val fp = FileResourceProvider(vFile.parent) if (vFile.exists()) { - val img = Resource(FileInputStream(vFile), MD5Utils.md5Encode16(src) + ".jpg") + val img = LazyResource(fp, href) epubBook.resources.add(img) } } @@ -275,7 +278,10 @@ class CacheViewModel(application: Application) : BaseViewModel(application) { matchResult.groupValues[1].let { val src = NetworkUtils.getAbsoluteURL(chapter.url, it) setPic(src, book, epubBook) - text1 = text1.replace(src, MD5Utils.md5Encode16(src) + ".jpg") + text1 = text1.replace( + src, + "${MD5Utils.md5Encode16(src)}${BookHelp.getImageSuffix(src)}" + ) } } diff --git a/epublib/src/main/java/me/ag2s/epublib/domain/FileResourceProvider.java b/epublib/src/main/java/me/ag2s/epublib/domain/FileResourceProvider.java new file mode 100644 index 000000000..a9db2b5d9 --- /dev/null +++ b/epublib/src/main/java/me/ag2s/epublib/domain/FileResourceProvider.java @@ -0,0 +1,38 @@ +package me.ag2s.epublib.domain; + +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; + +/** + * 用于创建epub,添加大文件(如大量图片)时容易OOM,使用LazyResource,避免OOM. + * + */ + +public class FileResourceProvider implements LazyResourceProvider { + //需要导入资源的父目录 + String dir; + + /** + * 创建一个文件夹里面文件夹的LazyResourceProvider,用于LazyResource。 + * @param dir 文件的目录 + */ + public FileResourceProvider(String dir) { + this.dir = dir; + } + + /** + * 创建一个文件夹里面文件夹的LazyResourceProvider,用于LazyResource。 + * @param dirfile 文件夹 + */ + @SuppressWarnings("unused") + public FileResourceProvider(File dirfile) { + this.dir = dirfile.getPath(); + } + + @Override + public InputStream getResourceStream(String href) throws IOException { + return new FileInputStream(new File(dir, href)); + } +} diff --git a/epublib/src/main/java/me/ag2s/epublib/util/IOUtil.java b/epublib/src/main/java/me/ag2s/epublib/util/IOUtil.java index 2117a8e0c..0ce72d062 100644 --- a/epublib/src/main/java/me/ag2s/epublib/util/IOUtil.java +++ b/epublib/src/main/java/me/ag2s/epublib/util/IOUtil.java @@ -450,6 +450,7 @@ public class IOUtil { output.write(buffer, 0, n); count += n; } + input.close(); } return count; }