Skip to content

Commit 449b20d

Browse files
authored
Add LcpService.injectLicenseDocument (readium#473)
1 parent 33a0c77 commit 449b20d

File tree

3 files changed

+50
-1
lines changed

3 files changed

+50
-1
lines changed

CHANGELOG.md

+7-1
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,15 @@ All notable changes to this project will be documented in this file. Take a look
88

99
### Added
1010

11-
* The new `HyperlinkNavigator.shouldFollowInternalLink(Link, LinkContext?)` allows you to handle footnotes according to your preference.
11+
#### Navigator
12+
13+
* The new `HyperlinkNavigator.shouldfollowinternallink(Link, LinkContext?)` allows you to handle footnotes according to your preference.
1214
* By default, the navigator now moves to the footnote content instead of displaying a pop-up as it did in version 2.x.
1315

16+
#### LCP
17+
18+
* You can use `LcpService.injectLicenseDocument()` to insert an LCPL into a package, if you downloaded it manually instead of using `LcpService.acquirePublication()`.
19+
1420

1521
## [3.0.0-alpha.1]
1622

readium/lcp/src/main/java/org/readium/r2/lcp/LcpService.kt

+16
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import org.readium.r2.shared.util.Try
2929
import org.readium.r2.shared.util.asset.Asset
3030
import org.readium.r2.shared.util.asset.AssetRetriever
3131
import org.readium.r2.shared.util.format.Format
32+
import org.readium.r2.shared.util.mediatype.MediaType
3233

3334
/**
3435
* Service used to acquire and open publications protected with LCP.
@@ -49,6 +50,8 @@ public interface LcpService {
4950
/**
5051
* Acquires a protected publication from a standalone LCPL's bytes.
5152
*
53+
* License will be injected into the publication archive without explicitly calling
54+
* [injectLicenseDocument].
5255
* You can cancel the on-going acquisition by cancelling its parent coroutine context.
5356
*
5457
* @param onProgress Callback to follow the acquisition progress from 0.0 to 1.0.
@@ -61,6 +64,8 @@ public interface LcpService {
6164
/**
6265
* Acquires a protected publication from a standalone LCPL file.
6366
*
67+
* License will be injected into the publication archive without explicitly calling
68+
* [injectLicenseDocument].
6469
* You can cancel the on-going acquisition by cancelling its parent coroutine context.
6570
*
6671
* @param onProgress Callback to follow the acquisition progress from 0.0 to 1.0.
@@ -114,6 +119,17 @@ public interface LcpService {
114119
allowUserInteraction: Boolean
115120
): Try<LcpLicense, LcpError>
116121

122+
/**
123+
* Injects a [licenseDocument] into the given [publicationFile] package.
124+
*
125+
* This is useful if you downloaded the publication yourself instead of using [acquirePublication].
126+
*/
127+
public suspend fun injectLicenseDocument(
128+
licenseDocument: LicenseDocument,
129+
publicationFile: File,
130+
mediaType: MediaType? = null
131+
): Try<Unit, LcpError>
132+
117133
/**
118134
* Creates a [ContentProtection] instance which can be used with a Streamer to unlock
119135
* LCP protected publications.

readium/lcp/src/main/java/org/readium/r2/lcp/service/LicensesService.kt

+27
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,33 @@ internal class LicensesService(
6666
): ContentProtection =
6767
LcpContentProtection(this, authentication, assetRetriever)
6868

69+
override suspend fun injectLicenseDocument(
70+
licenseDocument: LicenseDocument,
71+
publicationFile: File,
72+
mediaType: MediaType?
73+
): Try<Unit, LcpError> {
74+
val format = assetRetriever.sniffFormat(publicationFile, FormatHints(mediaType))
75+
.getOrElse {
76+
Format(
77+
specification = FormatSpecification(
78+
ZipSpecification,
79+
EpubSpecification,
80+
LcpSpecification
81+
),
82+
mediaType = MediaType.EPUB,
83+
fileExtension = FileExtension("epub")
84+
)
85+
}
86+
87+
return try {
88+
val container = createLicenseContainer(publicationFile, format.specification)
89+
container.write(licenseDocument)
90+
Try.success(Unit)
91+
} catch (e: Exception) {
92+
Try.failure(LcpError.wrap(e))
93+
}
94+
}
95+
6996
override suspend fun acquirePublication(
7097
lcpl: File,
7198
onProgress: (Double) -> Unit

0 commit comments

Comments
 (0)