Skip to content

Commit

Permalink
Backward compatibility of hrefs prefixed with /
Browse files Browse the repository at this point in the history
  • Loading branch information
mickael-menu committed Aug 31, 2023
1 parent 79e4a2c commit 72c3027
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
import org.readium.navigator.media2.MediaNavigator.Companion.create
import org.readium.r2.navigator.Navigator
import org.readium.r2.navigator.extensions.normalizeLocator
import org.readium.r2.shared.publication.Link
import org.readium.r2.shared.publication.Locator
import org.readium.r2.shared.publication.Publication
Expand Down Expand Up @@ -243,6 +244,8 @@ public class MediaNavigator private constructor(
* Seeks to the given locator.
*/
public suspend fun go(locator: Locator): Try<Unit, Exception> {
@Suppress("NAME_SHADOWING") val locator = publication.normalizeLocator(locator)

val itemIndex = publication.readingOrder.indexOfFirstWithHref(locator.href)
?: return Try.failure(Exception.InvalidArgument("Invalid href ${locator.href}."))
val position = locator.locations.time ?: Duration.ZERO
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ import org.readium.r2.shared.publication.Link
import org.readium.r2.shared.publication.Locator
import org.readium.r2.shared.publication.Publication
import org.readium.r2.shared.publication.ReadingProgression as PublicationReadingProgression
import org.readium.r2.navigator.extensions.normalizeLocator
import org.readium.r2.shared.publication.epub.EpubLayout
import org.readium.r2.shared.publication.presentation.presentation
import org.readium.r2.shared.publication.services.isRestricted
Expand Down Expand Up @@ -571,6 +572,8 @@ public class EpubNavigatorFragment internal constructor(
}

override fun go(locator: Locator, animated: Boolean, completion: () -> Unit): Boolean {
@Suppress("NAME_SHADOWING") val locator = publication.normalizeLocator(locator)

listener?.onJumpToLocator(locator)

val href = locator.href
Expand Down Expand Up @@ -718,6 +721,9 @@ public class EpubNavigatorFragment internal constructor(
}

override suspend fun applyDecorations(decorations: List<Decoration>, group: String) {
@Suppress("NAME_SHADOWING") val decorations = decorations
.map { it.copy(locator = publication.normalizeLocator(it.locator)) }

run(viewModel.applyDecorations(decorations, group))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
package org.readium.r2.navigator.extensions

import kotlinx.coroutines.runBlocking
import org.readium.r2.shared.InternalReadiumApi
import org.readium.r2.shared.publication.Locator
import org.readium.r2.shared.publication.Publication
import org.readium.r2.shared.publication.services.positions
Expand All @@ -21,3 +22,17 @@ internal val Publication.positionsSync: List<Locator>

internal val Publication.positionsByResource: Map<String, List<Locator>>
get() = runBlocking { positions().groupBy { it.href } }

/**
* Historically, we used to have "absolute" HREFs starting with a `/` in the manifest for packaged
* publications. We removed the root prefix, but we still need to support the locators created with
* the old HREFs.
*/
@InternalReadiumApi
public fun Publication.normalizeLocator(locator: Locator): Locator {
if (linkWithRel("self") != null) {
return locator
}

return locator.copy(href = locator.href.removePrefix("/"))
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import org.readium.r2.shared.publication.Link
import org.readium.r2.shared.publication.Locator
import org.readium.r2.shared.publication.Publication
import org.readium.r2.shared.publication.ReadingProgression as PublicationReadingProgression
import org.readium.r2.navigator.extensions.normalizeLocator
import org.readium.r2.shared.publication.indexOfFirstWithHref
import org.readium.r2.shared.publication.services.isRestricted
import org.readium.r2.shared.publication.services.positions
Expand Down Expand Up @@ -185,6 +186,8 @@ public class ImageNavigatorFragment private constructor(
}

override fun go(locator: Locator, animated: Boolean, completion: () -> Unit): Boolean {
@Suppress("NAME_SHADOWING") val locator = publication.normalizeLocator(locator)

val resourceIndex = publication.readingOrder.indexOfFirstWithHref(locator.href)
?: return false

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import androidx.media3.common.Player
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.flow.StateFlow
import org.readium.r2.navigator.extensions.normalizeLocator
import org.readium.r2.navigator.media3.api.Media3Adapter
import org.readium.r2.navigator.media3.api.MediaMetadataProvider
import org.readium.r2.navigator.media3.api.MediaNavigator
Expand Down Expand Up @@ -197,7 +198,7 @@ public class TtsNavigator<S : TtsEngine.Settings, P : TtsEngine.Preferences<P>,
}

public fun go(locator: Locator) {
player.go(locator)
player.go(publication.normalizeLocator(locator))
}

override fun goToPreviousUtterance() {
Expand Down Expand Up @@ -227,7 +228,7 @@ public class TtsNavigator<S : TtsEngine.Settings, P : TtsEngine.Preferences<P>,
location.mapStateIn(coroutineScope) { it.tokenLocator ?: it.utteranceLocator }

override fun go(locator: Locator, animated: Boolean, completion: () -> Unit): Boolean {
player.go(locator)
player.go(publication.normalizeLocator(locator))
return true
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import org.readium.r2.shared.publication.Link
import org.readium.r2.shared.publication.Locator
import org.readium.r2.shared.publication.Publication
import org.readium.r2.shared.publication.ReadingProgression as PublicationReadingProgression
import org.readium.r2.navigator.extensions.normalizeLocator
import org.readium.r2.shared.publication.services.isRestricted
import org.readium.r2.shared.resource.Resource
import org.readium.r2.shared.util.mediatype.MediaType
Expand Down Expand Up @@ -224,6 +225,7 @@ public class PdfNavigatorFragment<S : Configurable.Settings, P : Configurable.Pr
get() = viewModel.currentLocator

override fun go(locator: Locator, animated: Boolean, completion: () -> Unit): Boolean {
@Suppress("NAME_SHADOWING") val locator = publication.normalizeLocator(locator)
listener?.onJumpToLocator(locator)
val pageNumber = locator.locations.page ?: locator.locations.position ?: 1
return goToPageIndex(pageNumber - 1, animated, completion)
Expand Down

0 comments on commit 72c3027

Please sign in to comment.