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

@ -11,11 +11,12 @@ class WebDavFile(
val urlName: String, val urlName: String,
val size: Long, val size: Long,
val contentType: String, val contentType: String,
val resourceType: String,
val lastModify: Long val lastModify: Long
) : WebDav(urlStr, authorization) { ) : WebDav(urlStr, authorization) {
val isDir by lazy { 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)) elements.add(getBody(res, startFragmentId, endFragmentId))
} }
} }
//title标签中的内容不需要显示在正文中,去除
elements.select("title").remove()
var html = elements.outerHtml() var html = elements.outerHtml()
val tag = Book.rubyTag val tag = Book.rubyTag
if (book.getDelTag(tag)) { 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. * 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); "Package document does not contain element " + OPFTags.manifest);
return result; return result;
} }
NodeList itemElements = manifestElement List<Element> ensuredElements = ensureImageInfo(resources,manifestElement);
.getElementsByTagNameNS(NAMESPACE_OPF, OPFTags.item); for (Element itemElement : ensuredElements) {
for (int i = 0; i < itemElements.getLength(); i++) { // Element itemElement = ;
Element itemElement = (Element) itemElements.item(i);
String id = DOMUtil String id = DOMUtil
.getAttribute(itemElement, NAMESPACE_OPF, OPFAttributes.id); .getAttribute(itemElement, NAMESPACE_OPF, OPFAttributes.id);
String href = DOMUtil String href = DOMUtil

Loading…
Cancel
Save