Skip to content
This repository has been archived by the owner on Aug 12, 2022. It is now read-only.

Commit

Permalink
Merge pull request #52 from readium/fixes/lcpdf
Browse files Browse the repository at this point in the history
Read a license from a LCPDF container
  • Loading branch information
aferditamuriqi authored Jul 7, 2019
2 parents 162d639 + 4b944de commit ce2bbe0
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Cartfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
github "readium/r2-shared-swift" == 1.2.13
github "readium/r2-shared-swift" == 1.3.0
github "stephencelis/SQLite.swift" == 0.11.5
github "krzyzanowskim/CryptoSwift" == 0.15.0
github "weichsel/ZIPFoundation" == 0.9.8
2 changes: 1 addition & 1 deletion Cartfile.resolved
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
github "krzyzanowskim/CryptoSwift" "0.15.0"
github "readium/r2-shared-swift" "1.2.13"
github "readium/r2-shared-swift" "1.3.0"
github "stephencelis/SQLite.swift" "0.11.5"
github "weichsel/ZIPFoundation" "0.9.8"
4 changes: 4 additions & 0 deletions r2-lcp-swift.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
CA4A3892220994E300599297 /* EPUBLicenseContainer.swift in Sources */ = {isa = PBXBuildFile; fileRef = CA4A3891220994E300599297 /* EPUBLicenseContainer.swift */; };
CA50B88022B2A777003AFF24 /* R2LCPLocalizedString.swift in Sources */ = {isa = PBXBuildFile; fileRef = CA50B87F22B2A777003AFF24 /* R2LCPLocalizedString.swift */; };
CA50B88422B2A822003AFF24 /* Localizable.strings in Resources */ = {isa = PBXBuildFile; fileRef = CA50B88622B2A822003AFF24 /* Localizable.strings */; };
CA9428FD22B9442300305CDB /* LCPDFLicenseContainer.swift in Sources */ = {isa = PBXBuildFile; fileRef = CA9428FC22B9442300305CDB /* LCPDFLicenseContainer.swift */; };
CAB131F6220D81E60097DFB5 /* LicensesRepository.swift in Sources */ = {isa = PBXBuildFile; fileRef = CAB131F5220D81E60097DFB5 /* LicensesRepository.swift */; };
CAB1320B220D9B200097DFB5 /* LCPService.swift in Sources */ = {isa = PBXBuildFile; fileRef = CAB1320A220D9B200097DFB5 /* LCPService.swift */; };
CAB13211220DB2B10097DFB5 /* LCPAuthenticating.swift in Sources */ = {isa = PBXBuildFile; fileRef = CAB13210220DB2B10097DFB5 /* LCPAuthenticating.swift */; };
Expand Down Expand Up @@ -61,6 +62,7 @@
CA4A3891220994E300599297 /* EPUBLicenseContainer.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EPUBLicenseContainer.swift; sourceTree = "<group>"; };
CA50B87F22B2A777003AFF24 /* R2LCPLocalizedString.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = R2LCPLocalizedString.swift; sourceTree = "<group>"; };
CA50B88522B2A822003AFF24 /* en */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = en; path = en.lproj/Localizable.strings; sourceTree = "<group>"; };
CA9428FC22B9442300305CDB /* LCPDFLicenseContainer.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LCPDFLicenseContainer.swift; sourceTree = "<group>"; };
CAB131F5220D81E60097DFB5 /* LicensesRepository.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LicensesRepository.swift; sourceTree = "<group>"; };
CAB1320A220D9B200097DFB5 /* LCPService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LCPService.swift; sourceTree = "<group>"; };
CAB13210220DB2B10097DFB5 /* LCPAuthenticating.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LCPAuthenticating.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -155,6 +157,7 @@
CA4A388D220988C800599297 /* LCPLLicenseContainer.swift */,
CA4A388F22098A9400599297 /* ZIPLicenseContainer.swift */,
CA4A3891220994E300599297 /* EPUBLicenseContainer.swift */,
CA9428FC22B9442300305CDB /* LCPDFLicenseContainer.swift */,
);
path = Container;
sourceTree = "<group>";
Expand Down Expand Up @@ -372,6 +375,7 @@
CABEB3F62215C86C00090B6C /* LCPError+wrap.swift in Sources */,
CA4A388722086F7500599297 /* PassphrasesService.swift in Sources */,
CAE7FB82220C64D100D03587 /* CRLService.swift in Sources */,
CA9428FD22B9442300305CDB /* LCPDFLicenseContainer.swift in Sources */,
F36F9E261F826C17001D0DB4 /* Licenses.swift in Sources */,
F35AFA011F6AD54400D3F670 /* License.swift in Sources */,
F38FB03C1F6683F300F9D602 /* UserKey.swift in Sources */,
Expand Down
21 changes: 21 additions & 0 deletions readium-lcp-swift/License/Container/LCPDFLicenseContainer.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//
// LCPDFLicenseContainer.swift
// r2-lcp-swift
//
// Created by Mickaël Menu on 18.06.19.
//
// Copyright 2019 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.
//

import Foundation

/// Access a License Document stored in an LCPDF archive.
final class LCPDFLicenseContainer: ZIPLicenseContainer {

init(lcpdf: URL) {
super.init(zip: lcpdf, pathInZIP: "license.lcpl")
}

}
12 changes: 12 additions & 0 deletions readium-lcp-swift/License/Container/LicenseContainer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
//

import Foundation
import R2Shared

/// Encapsulates the read/write access to the packaged License Document (eg. in an EPUB container, or a standalone LCPL file)
protocol LicenseContainer {
Expand All @@ -18,3 +19,14 @@ protocol LicenseContainer {
func write(_ license: LicenseDocument) throws

}

func makeLicenseContainer(for publication: URL, mimetype: String? = nil) throws -> LicenseContainer {
switch Publication.Format(file: publication, mimetype: mimetype) {
case .pdf:
return LCPDFLicenseContainer(lcpdf: publication)
case .epub:
return EPUBLicenseContainer(epub: publication)
default:
throw LCPError.licenseContainer
}
}
2 changes: 1 addition & 1 deletion readium-lcp-swift/License/License.swift
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ extension License {

do {
// Saves the License Document into the downloaded publication
let container = EPUBLicenseContainer(epub: downloadedFile)
let container = try makeLicenseContainer(for: downloadedFile, mimetype: task?.response?.mimeType)
try container.write(license)
completion((downloadedFile, task), nil)

Expand Down
10 changes: 7 additions & 3 deletions readium-lcp-swift/Services/LicensesService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,13 @@ extension LicensesService: LCPService {
}

func retrieveLicense(from publication: URL, authentication: LCPAuthenticating?, completion: @escaping (LCPLicense?, LCPError?) -> Void) {
let container = EPUBLicenseContainer(epub: publication)
retrieveLicense(from: container, authentication: authentication)
.resolve(LCPError.wrap(completion))
do {
let container = try makeLicenseContainer(for: publication)
retrieveLicense(from: container, authentication: authentication)
.resolve(LCPError.wrap(completion))
} catch {
completion(nil, LCPError.wrap(error))
}
}

}

0 comments on commit ce2bbe0

Please sign in to comment.