Skip to content

Commit

Permalink
Add a retrieveLicenseDocument method to LcpService (readium#519)
Browse files Browse the repository at this point in the history
  • Loading branch information
qnga committed May 21, 2024
1 parent a91e76f commit ac6392c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
8 changes: 8 additions & 0 deletions readium/lcp/src/main/java/org/readium/r2/lcp/LcpService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import org.readium.r2.shared.publication.protection.ContentProtection
import org.readium.r2.shared.util.Try
import org.readium.r2.shared.util.asset.Asset
import org.readium.r2.shared.util.asset.AssetRetriever
import org.readium.r2.shared.util.asset.ContainerAsset
import org.readium.r2.shared.util.format.Format

/**
Expand Down Expand Up @@ -118,6 +119,13 @@ public interface LcpService {
allowUserInteraction: Boolean
): Try<LcpLicense, LcpError>

/**
* Retrieves the license document from a LCP-protected publication asset.
*/
public suspend fun retrieveLicenseDocument(
asset: ContainerAsset
): Try<LicenseDocument, LcpError>

/**
* Injects a [licenseDocument] into the given [publicationFile] package.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import org.readium.r2.shared.util.FileExtension
import org.readium.r2.shared.util.Try
import org.readium.r2.shared.util.asset.Asset
import org.readium.r2.shared.util.asset.AssetRetriever
import org.readium.r2.shared.util.asset.ContainerAsset
import org.readium.r2.shared.util.format.Format
import org.readium.r2.shared.util.format.FormatHints
import org.readium.r2.shared.util.format.FormatSpecification
Expand Down Expand Up @@ -243,6 +244,21 @@ internal class LicensesService(
Try.failure(LcpError.wrap(e))
}

override suspend fun retrieveLicenseDocument(
asset: ContainerAsset
): Try<LicenseDocument, LcpError> =
withContext(Dispatchers.IO) {
try {
val licenseContainer = createLicenseContainer(context, asset)
val licenseData = licenseContainer.read()
Try.success(LicenseDocument(licenseData))
} catch (e: CancellationException) {
throw e
} catch (e: Exception) {
Try.failure(LcpError.wrap(e))
}
}

private suspend fun retrieveLicense(
container: LicenseContainer,
authentication: LcpAuthenticating,
Expand Down

0 comments on commit ac6392c

Please sign in to comment.