Skip to content

Commit ef2314c

Browse files
committed
Add injectLicense to LCPService
1 parent 7c0436c commit ef2314c

File tree

2 files changed

+48
-15
lines changed

2 files changed

+48
-15
lines changed

Sources/LCP/LCPService.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,18 @@ public final class LCPService: Loggable {
8080
}
8181
}
8282

83+
/// Injects a `licenseDocument` into a publication package at `url`.
84+
///
85+
/// This is useful if you downloaded the publication yourself instead of using `acquirePublication`.
86+
public func injectLicense(
87+
_ license: LicenseDocument,
88+
in url : FileURL
89+
) async -> Result<Void, LCPError> {
90+
await wrap {
91+
try await licenses.injectLicense(license, in: url)
92+
}
93+
}
94+
8395
/// Opens the LCP license of a protected publication, to access its DRM
8496
/// metadata and decipher its content.
8597
///

Sources/LCP/Services/LicensesService.swift

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -120,28 +120,49 @@ final class LicensesService: Loggable {
120120
onProgress: { onProgress(.percent(Float($0))) }
121121
).get()
122122

123-
var hints = FormatHints()
124-
if let type = license.link(for: .publication)?.mediaType {
125-
hints.mediaTypes.append(type)
126-
}
127-
if let type = download.mediaType {
128-
hints.mediaTypes.append(type)
129-
}
130-
131-
let asset = try await assetRetriever.retrieve(url: download.location, hints: hints)
132-
.mapError { LCPError.licenseContainer(ContainerError.openFailed($0)) }
133-
.get()
134-
135-
try await injectLicense(license, in: asset)
123+
let format = try await injectLicenseAndGetFormat(
124+
license,
125+
in: download.location,
126+
mediaTypeHint: download.mediaType
127+
)
136128

137129
return LCPAcquiredPublication(
138130
localURL: download.location,
139-
format: asset.format,
140-
suggestedFilename: asset.format.fileExtension.appendedToFilename(license.id),
131+
format: format,
132+
suggestedFilename: format.fileExtension.appendedToFilename(license.id),
141133
licenseDocument: license
142134
)
143135
}
144136

137+
func injectLicense(
138+
_ license: LicenseDocument,
139+
in url: FileURL
140+
) async throws {
141+
let _ = try await injectLicenseAndGetFormat(license, in: url, mediaTypeHint: nil)
142+
}
143+
144+
private func injectLicenseAndGetFormat(
145+
_ license: LicenseDocument,
146+
in url: FileURL,
147+
mediaTypeHint: MediaType? = nil
148+
) async throws -> Format {
149+
var formatHints = FormatHints()
150+
if let type = license.publicationLink.mediaType {
151+
formatHints.mediaTypes.append(type)
152+
}
153+
if let type = mediaTypeHint {
154+
formatHints.mediaTypes.append(type)
155+
}
156+
157+
let asset = try await assetRetriever.retrieve(url: url, hints: formatHints)
158+
.mapError { LCPError.licenseContainer(ContainerError.openFailed($0)) }
159+
.get()
160+
161+
try await injectLicense(license, in: asset)
162+
163+
return asset.format
164+
}
165+
145166
private func readLicense(from lcpl: LicenseDocumentSource) async throws -> LicenseDocument? {
146167
switch lcpl {
147168
case let .data(data):

0 commit comments

Comments
 (0)