Merge pull request #2039 from qianfanguojin/master

修复Issue 1972 的一些问题
pull/2042/head
kunfei 2 years ago committed by GitHub
commit 6e7689b5fe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 11
      app/src/main/java/io/legado/app/lib/webdav/WebDav.kt
  2. 3
      app/src/main/java/io/legado/app/lib/webdav/WebDavFile.kt
  3. 2
      app/src/main/java/io/legado/app/model/localBook/EpubFile.kt
  4. 61
      epublib/src/main/java/me/ag2s/epublib/epub/PackageDocumentReader.java

@ -137,6 +137,9 @@ open class WebDav(val path: String, val authorization: Authorization) {
val contentType = element
.getElementsByTag("d:getcontenttype")
.firstOrNull()?.text().orEmpty()
val resourceType = element
.getElementsByTag("d:resourcetype")
.firstOrNull()?.html()?.trim().orEmpty()
val size = kotlin.runCatching {
element.getElementsByTag("d:getcontentlength")
.firstOrNull()?.text()?.toLong() ?: 0
@ -155,6 +158,7 @@ open class WebDav(val path: String, val authorization: Authorization) {
urlName = urlName,
size = size,
contentType = contentType,
resourceType = resourceType,
lastModify = lastModify
)
list.add(webDavFile)
@ -171,11 +175,14 @@ open class WebDav(val path: String, val authorization: Authorization) {
* 文件是否存在
*/
suspend fun exists(): Boolean {
val url = httpUrl ?: return false
//当使用自建的WebDav服务时,在末尾有否 ”/“ 会影响请求的成功与否
//使用坚果云的WebDav则不会,这里做一个简单的替换来解决这个问题
val testUrl = url.removeSuffix("/") + "/"
return kotlin.runCatching {
return okHttpClient.newCallResponse {
url(url)
url(testUrl)
addHeader(authorization.name, authorization.data)
head()
}.code == 200
}.getOrDefault(false)
}

@ -11,11 +11,12 @@ class WebDavFile(
val urlName: String,
val size: Long,
val contentType: String,
val resourceType: String,
val lastModify: Long
) : WebDav(urlStr, authorization) {
val isDir by lazy {
contentType == "httpd/unix-directory"
contentType == "httpd/unix-directory" || resourceType.lowercase() == "<d:collection />"
}
}

@ -155,6 +155,8 @@ class EpubFile(var book: Book) {
elements.add(getBody(res, startFragmentId, endFragmentId))
}
}
//title标签中的内容不需要显示在正文中,去除
elements.select("title").remove()
var html = elements.outerHtml()
val tag = Book.rubyTag
if (book.getDelTag(tag)) {

@ -72,6 +72,60 @@ public class PackageDocumentReader extends PackageDocumentBase {
}
}
/**
*
* 修复一些非标准epub格式由于 opf 文件内容不全而读取不到图片的问题
* @author qianfanguojin
* @return 修复图片路径后的一个Element列表
*/
private static ArrayList<Element> ensureImageInfo(Resources resources,
Element manifestElement){
ArrayList<Element> fixedElements = new ArrayList<>();
//加入当前所有的 item 标签
NodeList originItemElements = manifestElement
.getElementsByTagNameNS(NAMESPACE_OPF, OPFTags.item);
for (int i = 0; i < originItemElements.getLength(); i++) {
fixedElements.add((Element) originItemElements.item(i));
}
//如果有图片资源未定义在 originItemElements ,则加入该图片信息得到 fixedElements 中
for (Resource resource : resources.getAll()) {
MediaType currentMediaType = resource.getMediaType();
if ( currentMediaType == MediaTypes.JPG || currentMediaType == MediaTypes.PNG){
String imageHref = resource.getHref();
//确保该图片信息 resource 在原 originItemElements 列表中没有出现过
boolean flag = false;
int i;
for (i = 0; i < originItemElements.getLength(); i++){
Element itemElement = (Element) originItemElements.item(i);
String href = DOMUtil
.getAttribute(itemElement, NAMESPACE_OPF, OPFAttributes.href);
try {
href = URLDecoder.decode(href, Constants.CHARACTER_ENCODING);
} catch (UnsupportedEncodingException e) {
Log.e(TAG, e.getMessage());
}
if (href.equals(imageHref)){
break;
}
}
if (i == originItemElements.getLength()){
flag = true;
}
if (flag){
//由于暂时无法实例化一个Element,则选择克隆一个已存在的节点来修改以达到新增 Element 的效果,作为临时解决方案
Element tempElement = (Element) manifestElement.getElementsByTagNameNS(NAMESPACE_OPF,OPFTags.item).item(0).cloneNode(true);
tempElement.setAttribute("id",imageHref.replace("/",""));
tempElement.setAttribute("href",imageHref);
tempElement.setAttribute("media-type", currentMediaType.getName());
fixedElements.add(tempElement);
}
}
}
return fixedElements;
}
/**
* Reads the manifest containing the resource ids, hrefs and mediatypes.
*
@ -96,10 +150,9 @@ public class PackageDocumentReader extends PackageDocumentBase {
"Package document does not contain element " + OPFTags.manifest);
return result;
}
NodeList itemElements = manifestElement
.getElementsByTagNameNS(NAMESPACE_OPF, OPFTags.item);
for (int i = 0; i < itemElements.getLength(); i++) {
Element itemElement = (Element) itemElements.item(i);
List<Element> ensuredElements = ensureImageInfo(resources,manifestElement);
for (Element itemElement : ensuredElements) {
// Element itemElement = ;
String id = DOMUtil
.getAttribute(itemElement, NAMESPACE_OPF, OPFAttributes.id);
String href = DOMUtil

Loading…
Cancel
Save