@@ -12,7 +12,9 @@ package org.readium.r2.shared
12
12
import org.json.JSONArray
13
13
import org.json.JSONObject
14
14
import java.io.Serializable
15
+ import java.net.URI
15
16
import java.net.URL
17
+ import java.net.URLDecoder
16
18
17
19
18
20
fun URL.removeLastComponent (): URL {
@@ -93,6 +95,7 @@ class Publication : Serializable {
93
95
EPUB (" .epub" ),
94
96
CBZ (" .cbz" ),
95
97
JSON (" .json" );
98
+
96
99
companion object : EnumCompanion <String , EXTENSION >(EXTENSION .values().associateBy(EXTENSION ::value))
97
100
}
98
101
@@ -157,18 +160,53 @@ class Publication : Serializable {
157
160
return str
158
161
}
159
162
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
+ }
161
169
162
170
fun linkWithRel (rel : String ): Link ? {
163
171
val findLinkWithRel: (Link ) -> Boolean = { it.rel.contains(rel) }
164
172
return findLinkInPublicationLinks(findLinkWithRel)
165
173
}
166
174
167
175
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
+ }
169
181
return findLinkInPublicationLinks(findLinkWithHref)
170
182
}
171
183
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
+
172
210
fun addSelfLink (endPoint : String , baseURL : URL ) {
173
211
val publicationUrl: URL
174
212
val link = Link ()
@@ -181,14 +219,9 @@ class Publication : Serializable {
181
219
links.add(link)
182
220
}
183
221
184
- private fun findLinkInPublicationLinks (closure : (Link ) -> Boolean ) =
185
- resources.firstOrNull(closure) ? : spine.firstOrNull(closure)
186
- ? : links.firstOrNull(closure) ? : pageList.firstOrNull(closure)
187
-
188
222
enum class PublicationError (var v : String ) {
189
223
InvalidPublication (" Invalid publication" )
190
224
}
191
-
192
225
}
193
226
194
227
0 commit comments