This repository has been archived by the owner on Jul 29, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #107 from readium/fix/upgrade-readium-css
Simplify Readium CSS injection and remove injection of the default stylesheet
- Loading branch information
Showing
15 changed files
with
489 additions
and
520 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
r2-streamer/src/main/java/org/readium/r2/streamer/server/Assets.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
* Module: r2-streamer-kotlin | ||
* Developers: Mickaël Menu | ||
* | ||
* Copyright (c) 2020. Readium Foundation. All rights reserved. | ||
* Use of this source code is governed by a BSD-style license which is detailed in the | ||
* LICENSE file present in the project repository where this source code is maintained. | ||
*/ | ||
|
||
package org.readium.r2.streamer.server | ||
|
||
import android.content.res.AssetManager | ||
import android.net.Uri | ||
import org.readium.r2.shared.extensions.isParentOf | ||
import org.readium.r2.shared.format.Format | ||
import org.readium.r2.shared.format.MediaType | ||
import java.io.File | ||
import java.io.InputStream | ||
|
||
|
||
/** | ||
* Files to be served from the application's assets. | ||
* | ||
* @param basePath Base path (ignoring host) from where the files are served. | ||
* @param fallbackMediaType Media type which will be used for responses when it can't be determined | ||
* from the served file. | ||
*/ | ||
internal class Assets( | ||
private val assetManager: AssetManager, | ||
private val basePath: String, | ||
private val fallbackMediaType: MediaType = MediaType.BINARY | ||
) { | ||
private val assets: MutableList<Pair<String, File>> = mutableListOf() | ||
|
||
fun add(href: String, path: String) { | ||
// Inserts at the beginning to take precedence over already registered assets. | ||
assets.add(0, Pair(href, File("/$path").canonicalFile)) | ||
} | ||
|
||
fun find(uri: Uri): ServedAsset? { | ||
val path = uri.path?.removePrefix(basePath) ?: return null | ||
|
||
for ((href, file) in assets) { | ||
if (path.startsWith(href)) { | ||
val requestedFile = File(file, path.removePrefix(href)).canonicalFile | ||
// Makes sure that the requested file is `file` or one of its descendant. | ||
if (file == requestedFile || file.isParentOf(requestedFile)) { | ||
val mediaType = Format.of(fileExtension = requestedFile.extension)?.mediaType ?: fallbackMediaType | ||
return ServedAsset(assetManager.open(requestedFile.path.removePrefix("/")), mediaType) | ||
} | ||
} | ||
} | ||
|
||
return null | ||
} | ||
|
||
data class ServedAsset( | ||
val stream: InputStream, | ||
val mediaType: MediaType | ||
) | ||
|
||
} |
60 changes: 60 additions & 0 deletions
60
r2-streamer/src/main/java/org/readium/r2/streamer/server/Files.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/* | ||
* Module: r2-streamer-kotlin | ||
* Developers: Aferdita Muriqi, Clément Baumann | ||
* | ||
* Copyright (c) 2018. Readium Foundation. All rights reserved. | ||
* Use of this source code is governed by a BSD-style license which is detailed in the | ||
* LICENSE file present in the project repository where this source code is maintained. | ||
*/ | ||
|
||
package org.readium.r2.streamer.server | ||
|
||
import android.net.Uri | ||
import org.readium.r2.shared.extensions.isParentOf | ||
import org.readium.r2.shared.format.Format | ||
import org.readium.r2.shared.format.MediaType | ||
import java.io.File | ||
|
||
/** | ||
* Files to be served from the file system. | ||
* | ||
* @param basePath Base path (ignoring host) from where the files are served. | ||
* @param fallbackMediaType Media type which will be used for responses when it can't be determined | ||
* from the served file. | ||
*/ | ||
internal class Files( | ||
private val basePath: String, | ||
private val fallbackMediaType: MediaType = MediaType.BINARY | ||
) { | ||
private val files: MutableMap<String, File> = mutableMapOf() | ||
|
||
operator fun set(href: String, file: File) { | ||
files[href] = file.canonicalFile | ||
} | ||
|
||
operator fun get(key: String): File? = files[key] | ||
|
||
fun find(uri: Uri): ServedFile? { | ||
val path = uri.path?.removePrefix(basePath) ?: return null | ||
|
||
for ((href, file) in files) { | ||
if (path.startsWith(href)) { | ||
val requestedFile = File(file, path.removePrefix(href)).canonicalFile | ||
// Makes sure that the requested file is `file` or one of its descendant. | ||
if (file.isParentOf(requestedFile)) { | ||
return ServedFile(requestedFile, fallbackMediaType) | ||
} | ||
} | ||
} | ||
|
||
return null | ||
} | ||
|
||
data class ServedFile( | ||
val file: File, | ||
private val fallbackMediaType: MediaType | ||
) { | ||
val mediaType: MediaType get() = Format.of(file)?.mediaType ?: fallbackMediaType | ||
} | ||
|
||
} |
24 changes: 0 additions & 24 deletions
24
r2-streamer/src/main/java/org/readium/r2/streamer/server/Fonts.kt
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.