Skip to content

Commit d2a7d85

Browse files
Add more logic to linkWithHref()
For more explanation see commit with same commit message in r2-streamer-kotlin
1 parent c1f242f commit d2a7d85

File tree

1 file changed

+40
-7
lines changed

1 file changed

+40
-7
lines changed

r2-shared/src/main/java/org/readium/r2/shared/Publication.kt

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ package org.readium.r2.shared
1212
import org.json.JSONArray
1313
import org.json.JSONObject
1414
import java.io.Serializable
15+
import java.net.URI
1516
import java.net.URL
17+
import java.net.URLDecoder
1618

1719

1820
fun URL.removeLastComponent(): URL {
@@ -93,6 +95,7 @@ class Publication : Serializable {
9395
EPUB(".epub"),
9496
CBZ(".cbz"),
9597
JSON(".json");
98+
9699
companion object : EnumCompanion<String, EXTENSION>(EXTENSION.values().associateBy(EXTENSION::value))
97100
}
98101

@@ -157,18 +160,53 @@ class Publication : Serializable {
157160
return str
158161
}
159162

160-
fun resource(relativePath: String): Link? = (spine + resources).first { (it.href == relativePath) || (it.href == "/$relativePath") }
163+
fun resource(href: String): Link? =
164+
(spine + resources).firstOrNull {
165+
isLinkWithHref(href, it) ||
166+
isLinkWithHrefURIDecoded(href, it) ||
167+
isLinkWithLinkHrefURLDecoded(href, it)
168+
}
161169

162170
fun linkWithRel(rel: String): Link? {
163171
val findLinkWithRel: (Link) -> Boolean = { it.rel.contains(rel) }
164172
return findLinkInPublicationLinks(findLinkWithRel)
165173
}
166174

167175
fun linkWithHref(href: String): Link? {
168-
val findLinkWithHref: (Link) -> Boolean = { (href == it.href) || ("/$href" == it.href) }
176+
val findLinkWithHref: (Link) -> Boolean = {
177+
isLinkWithHref(href, it) ||
178+
isLinkWithHrefURIDecoded(href, it) ||
179+
isLinkWithLinkHrefURLDecoded(href, it)
180+
}
169181
return findLinkInPublicationLinks(findLinkWithHref)
170182
}
171183

184+
private fun isLinkWithHref(href: String, link: Link): Boolean {
185+
return href == link.href || "/$href" == link.href
186+
}
187+
188+
private fun isLinkWithHrefURIDecoded(href: String, link: Link): Boolean {
189+
return try {
190+
val decodedHref = URI(null, null, href, null).toString()
191+
decodedHref == link.href || "/$decodedHref" == link.href
192+
} catch (e: Exception) {
193+
false
194+
}
195+
}
196+
197+
private fun isLinkWithLinkHrefURLDecoded(href: String, link: Link): Boolean {
198+
return try {
199+
val decodedLinkHref = URLDecoder.decode(link.href, "UTF-8")
200+
href == decodedLinkHref || "/$href" == decodedLinkHref
201+
} catch (e: Exception) {
202+
false
203+
}
204+
}
205+
206+
private fun findLinkInPublicationLinks(closure: (Link) -> Boolean) =
207+
resources.firstOrNull(closure) ?: spine.firstOrNull(closure)
208+
?: links.firstOrNull(closure) ?: pageList.firstOrNull(closure)
209+
172210
fun addSelfLink(endPoint: String, baseURL: URL) {
173211
val publicationUrl: URL
174212
val link = Link()
@@ -181,14 +219,9 @@ class Publication : Serializable {
181219
links.add(link)
182220
}
183221

184-
private fun findLinkInPublicationLinks(closure: (Link) -> Boolean) =
185-
resources.firstOrNull(closure) ?: spine.firstOrNull(closure)
186-
?: links.firstOrNull(closure) ?: pageList.firstOrNull(closure)
187-
188222
enum class PublicationError(var v: String) {
189223
InvalidPublication("Invalid publication")
190224
}
191-
192225
}
193226

194227

0 commit comments

Comments
 (0)