Skip to content

Commit 13420f0

Browse files
authored
Allow to disable Readium Navigator injections in the Server (#37)
Allow to disable injections in the Streamer to break the strong coupling between the streamer and the navigator. Navigator's script names hard-coded in the streamer used to regularly break third-party navigators.
1 parent 35b3fa2 commit 13420f0

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
lines changed

readium/streamer/src/main/java/org/readium/r2/streamer/server/Server.kt

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,17 @@ import java.net.URL
2525
import java.net.URLDecoder
2626
import java.util.*
2727

28-
class Server(port: Int, context: Context) : AbstractServer(port, context.applicationContext)
29-
30-
abstract class AbstractServer(private var port: Int, private val context: Context) : RouterNanoHTTPD("127.0.0.1", port) {
28+
class Server(
29+
port: Int,
30+
context: Context,
31+
enableReadiumNavigatorSupport: Boolean = true
32+
) : AbstractServer(port, context.applicationContext, enableReadiumNavigatorSupport)
33+
34+
abstract class AbstractServer(
35+
private var port: Int,
36+
private val context: Context,
37+
private val enableReadiumNavigatorSupport: Boolean = true,
38+
) : RouterNanoHTTPD("127.0.0.1", port) {
3139

3240
private val MANIFEST_HANDLE = "/manifest"
3341
private val JSON_MANIFEST_HANDLE = "/manifest.json"
@@ -81,7 +89,7 @@ abstract class AbstractServer(private var port: Int, private val context: Contex
8189
}
8290
}
8391

84-
fun addPublication(publication: Publication, userPropertiesFile: File?): URL? {
92+
fun addPublication(publication: Publication, userPropertiesFile: File? = null): URL? {
8593
return addPublication(publication, null, "/${UUID.randomUUID()}", userPropertiesFile?.path)
8694
}
8795

@@ -92,6 +100,7 @@ abstract class AbstractServer(private var port: Int, private val context: Contex
92100
val baseUrl = URL(Publication.localBaseUrlOf(filename = filename, port = port))
93101
val fetcher = ServingFetcher(
94102
publication,
103+
enableReadiumNavigatorSupport,
95104
userPropertiesPath,
96105
customResources
97106
)

readium/streamer/src/main/java/org/readium/r2/streamer/server/ServingFetcher.kt

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,30 @@ import org.readium.r2.streamer.fetcher.HtmlInjector
1717

1818
internal class ServingFetcher(
1919
val publication: Publication,
20+
private val enableReadiumNavigatorSupport: Boolean,
2021
userPropertiesPath: String?,
21-
customResources: Resources? = null
22+
customResources: Resources? = null,
2223
) : Fetcher {
2324

24-
private val htmlInjector: HtmlInjector =
25+
private val htmlInjector: HtmlInjector by lazy {
2526
HtmlInjector(
2627
publication,
2728
userPropertiesPath,
2829
customResources
2930
)
31+
}
3032

3133
override suspend fun links(): List<Link> = emptyList()
3234

3335
override fun get(link: Link): Resource {
3436
val resource = publication.get(link)
37+
return if (enableReadiumNavigatorSupport)
38+
transformResourceForReadiumNavigator(resource)
39+
else
40+
resource
41+
}
42+
43+
private fun transformResourceForReadiumNavigator(resource: Resource): Resource {
3544
return if (publication.type == Publication.TYPE.EPUB)
3645
htmlInjector.transform(resource)
3746
else

0 commit comments

Comments
 (0)