Skip to content

Commit

Permalink
Only normalize when comparing
Browse files Browse the repository at this point in the history
  • Loading branch information
mickael-menu committed Jun 13, 2024
1 parent 96963bd commit ff12255
Show file tree
Hide file tree
Showing 13 changed files with 43 additions and 18 deletions.
3 changes: 2 additions & 1 deletion readium/lcp/src/main/java/org/readium/r2/lcp/LcpDecryptor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import org.readium.r2.shared.util.Try
import org.readium.r2.shared.util.Url
import org.readium.r2.shared.util.data.ReadError
import org.readium.r2.shared.util.flatMap
import org.readium.r2.shared.util.getEquivalent
import org.readium.r2.shared.util.getOrElse
import org.readium.r2.shared.util.resource.FailureResource
import org.readium.r2.shared.util.resource.Resource
Expand All @@ -38,7 +39,7 @@ internal class LcpDecryptor(

fun transform(url: Url, resource: Resource): Resource {
return resource.flatMap {
val encryption = encryptionData[url]
val encryption = encryptionData.getEquivalent(url)

// Checks if the resource is encrypted and whether the encryption schemes of the resource
// and the DRM license are the same.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -615,13 +615,13 @@ public class EpubNavigatorFragment internal constructor(

listener?.onJumpToLocator(locator)

val href = locator.href.removeFragment().normalize()
val href = locator.href.removeFragment()

fun setCurrent(resources: List<PageResource>) {
val page = resources.withIndex().firstOrNull { (_, res) ->
when (res) {
is PageResource.EpubReflowable ->
res.link.url() == href
res.link.url().isEquivalent(href)
is PageResource.EpubFxl ->
res.leftUrl?.toString()?.endsWith(href.toString()) == true || res.rightUrl?.toString()?.endsWith(
href.toString()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ public fun Publication.normalizeLocator(locator: Locator): Locator {

return if (self == null) { // Packaged publication
locator.copy(
href = Url(locator.href.toString().removePrefix("/"))?.normalize()
href = Url(locator.href.toString().removePrefix("/"))
?: return locator
)
} else { // Remote publication
// Check that the locator HREF relative to `self` exists int he manifest.
val relativeHref = self.relativize(locator.href).normalize()
// Check that the locator HREF relative to `self` exists in the manifest.
val relativeHref = self.relativize(locator.href)
if (linkWithHref(relativeHref) != null) {
locator.copy(href = relativeHref)
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public class Href private constructor(private val href: Url) : Parcelable {
public fun resolve(
base: SharedUrl? = null,
parameters: Map<String, String> = emptyMap()
): SharedUrl = href.resolve(base, parameters).normalize()
): SharedUrl = href.resolve(base, parameters)

/**
* Indicates whether this HREF is templated.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public data class Link(
public fun url(
base: Url? = null,
parameters: Map<String, String> = emptyMap()
): Url = href.resolve(base, parameters).normalize()
): Url = href.resolve(base, parameters)

/**
* List of URI template parameter keys, if the [Link] is templated.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public data class Manifest(
public fun linkWithHref(href: Url): Link? {
fun List<Link>.deepLinkWithHref(href: Url): Link? {
for (l in this) {
if (l.url() == href) {
if (l.url().normalize() == href) {
return l
} else {
l.alternates.deepLinkWithHref(href)?.let { return it }
Expand Down
21 changes: 18 additions & 3 deletions readium/shared/src/main/java/org/readium/r2/shared/util/Url.kt
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,12 @@ public sealed class Url : Parcelable {
return true
}

/**
* Returns whether the receiver is equivalent to the given `url` after normalization.
*/
public fun isEquivalent(url: Url): Boolean =
normalize() == url.normalize()

override fun hashCode(): Int =
uri.toString().hashCode()

Expand Down Expand Up @@ -347,9 +353,8 @@ public fun Url.Companion.fromLegacyHref(href: String): Url? =
* if we can't parse the URL.
*/
@InternalReadiumApi
public fun Url.Companion.fromEpubHref(href: String): Url? {
return (Url(href) ?: fromDecodedPath(href))?.normalize()
}
public fun Url.Companion.fromEpubHref(href: String): Url? =
Url(href) ?: fromDecodedPath(href)

public fun File.toUrl(): AbsoluteUrl =
checkNotNull(AbsoluteUrl(Uri.fromFile(this)))
Expand Down Expand Up @@ -414,3 +419,13 @@ public value class FileExtension(
*/
public fun FileExtension?.appendToFilename(filename: String): String =
this?.let { "$filename.$value" } ?: filename

/**
* Returns the value of the first key matching `key` after normalization.
*/
public fun <T> Map<Url, T>.getEquivalent(key: Url): T? =
get(key) ?: run {
val url = key.normalize()
keys.firstOrNull { it.normalize() == url }
?.let { get(it) }
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package org.readium.r2.shared.util.data

import org.readium.r2.shared.util.Try
import org.readium.r2.shared.util.Url
import org.readium.r2.shared.util.getEquivalent

internal class CachingReadable(
private val source: Readable
Expand Down Expand Up @@ -69,7 +70,7 @@ internal class CachingContainer(
mutableMapOf()

override fun get(url: Url): Readable? {
cache[url]?.let { return it }
cache.getEquivalent(url)?.let { return it }

val entry = container[url]
?: return null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class SingleResourceContainer(
override val entries: Set<Url> = setOf(entryUrl)

override fun get(url: Url): Resource? {
if (url.removeFragment().removeQuery() != entryUrl) {
if (!url.removeFragment().removeQuery().isEquivalent(entryUrl)) {
return null
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package org.readium.r2.shared.util.format

import org.readium.r2.shared.util.Url
import org.readium.r2.shared.util.data.Container
import org.readium.r2.shared.util.getEquivalent
import org.readium.r2.shared.util.resource.Resource
import org.readium.r2.shared.util.resource.StringResource

Expand All @@ -25,7 +26,7 @@ class TestContainer(
resources.keys

override fun get(url: Url): Resource? =
resources[url]?.let { StringResource(it) }
resources.getEquivalent(url)?.let { StringResource(it) }

override fun close() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

package org.readium.r2.streamer.parser.audio

import org.readium.r2.shared.InternalReadiumApi
import org.readium.r2.shared.publication.Link
import org.readium.r2.shared.publication.LocalizedString
import org.readium.r2.shared.publication.Manifest
Expand All @@ -22,6 +23,7 @@ import org.readium.r2.shared.util.data.Container
import org.readium.r2.shared.util.data.ReadError
import org.readium.r2.shared.util.format.Format
import org.readium.r2.shared.util.format.Specification
import org.readium.r2.shared.util.getEquivalent
import org.readium.r2.shared.util.getOrElse
import org.readium.r2.shared.util.logging.WarningLogger
import org.readium.r2.shared.util.resource.Resource
Expand Down Expand Up @@ -66,6 +68,7 @@ public class AudioParser(
return finalizeParsing(container, readingOrderWithFormat, null)
}

@OptIn(InternalReadiumApi::class)
private suspend fun parseContainerAsset(
asset: ContainerAsset
): Try<Publication.Builder, PublicationParser.ParseError> {
Expand All @@ -79,7 +82,7 @@ public class AudioParser(

val readingOrderWithFormat =
asset.container
.mapNotNull { url -> entryFormats[url]?.let { url to it } }
.mapNotNull { url -> entryFormats.getEquivalent(url)?.let { url to it } }
.filter { (_, format) -> format.specification.specifications.any { it in audioSpecifications } }
.sortedBy { it.first.toString() }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@
package org.readium.r2.streamer.parser.epub

import kotlin.experimental.xor
import org.readium.r2.shared.InternalReadiumApi
import org.readium.r2.shared.publication.encryption.Encryption
import org.readium.r2.shared.util.Try
import org.readium.r2.shared.util.Url
import org.readium.r2.shared.util.data.ReadError
import org.readium.r2.shared.util.data.ReadTry
import org.readium.r2.shared.util.getEquivalent
import org.readium.r2.shared.util.resource.Resource
import org.readium.r2.shared.util.resource.TransformingResource
import org.readium.r2.shared.util.resource.flatMap
Expand All @@ -26,10 +28,11 @@ internal class EpubDeobfuscator(
private val encryptionData: Map<Url, Encryption>
) {

@OptIn(InternalReadiumApi::class)
@Suppress("Unused_parameter")
fun transform(url: Url, resource: Resource): Resource =
resource.flatMap {
val algorithm = encryptionData[url]?.algorithm
val algorithm = encryptionData.getEquivalent(url)?.algorithm
if (algorithm != null && algorithm2length.containsKey(algorithm)) {
DeobfuscatingResource(resource, algorithm)
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import org.readium.r2.shared.util.data.Container
import org.readium.r2.shared.util.data.ReadError
import org.readium.r2.shared.util.format.Format
import org.readium.r2.shared.util.format.Specification
import org.readium.r2.shared.util.getEquivalent
import org.readium.r2.shared.util.getOrElse
import org.readium.r2.shared.util.logging.WarningLogger
import org.readium.r2.shared.util.mediatype.MediaType
Expand Down Expand Up @@ -81,7 +82,7 @@ public class ImageParser(

val readingOrderWithFormat =
asset.container
.mapNotNull { url -> entryFormats[url]?.let { url to it } }
.mapNotNull { url -> entryFormats.getEquivalent(url)?.let { url to it } }
.filter { (_, format) -> format.specification.specifications.any { it in bitmapSpecifications } }
.sortedBy { it.first.toString() }

Expand Down

0 comments on commit ff12255

Please sign in to comment.