From 213196d26c810e288281459dac1579d4d9e47d3d Mon Sep 17 00:00:00 2001 From: 131 <11222509+Netrvin@users.noreply.github.com> Date: Sat, 12 Feb 2022 23:10:47 +0800 Subject: [PATCH] =?UTF-8?q?Data=20URI=20=E6=94=AF=E6=8C=81=EF=BC=88EPUB?= =?UTF-8?q?=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/me/ag2s/epublib/domain/Resources.java | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/epublib/src/main/java/me/ag2s/epublib/domain/Resources.java b/epublib/src/main/java/me/ag2s/epublib/domain/Resources.java index 27d512700..1a57c6a9c 100644 --- a/epublib/src/main/java/me/ag2s/epublib/domain/Resources.java +++ b/epublib/src/main/java/me/ag2s/epublib/domain/Resources.java @@ -10,6 +10,9 @@ import java.util.Collection; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.regex.Matcher; +import java.util.regex.Pattern; +import android.util.Base64; /** * All the resources that make up the book. @@ -314,7 +317,17 @@ public class Resources implements Serializable { return null; } href = StringUtil.substringBefore(href, Constants.FRAGMENT_SEPARATOR_CHAR); - return resources.get(href); + + Pattern dataUriRegex = Pattern.compile("data:([\\w/\\-\\.]+);base64,(.*)"); + Matcher dataUriMatcher = dataUriRegex.matcher(href); + if (dataUriMatcher.find()) { + String dataUriMediaTypeString = dataUriMatcher.group(1); + MediaType dataUriMediaType = new MediaType(dataUriMediaTypeString, "." + StringUtil.substringAfterLast(dataUriMediaTypeString, '/')); + byte[] dataUriData = Base64.decode(dataUriMatcher.group(2), Base64.DEFAULT); + return new Resource(dataUriData, dataUriMediaType); + } else { + return resources.get(href); + } } /**