diff --git a/Cartfile b/Cartfile index 6489a27..7baf1e6 100644 --- a/Cartfile +++ b/Cartfile @@ -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 diff --git a/Cartfile.resolved b/Cartfile.resolved index f88b647..81683ea 100644 --- a/Cartfile.resolved +++ b/Cartfile.resolved @@ -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" diff --git a/r2-lcp-swift.xcodeproj/project.pbxproj b/r2-lcp-swift.xcodeproj/project.pbxproj index 3bb6376..dbf026f 100644 --- a/r2-lcp-swift.xcodeproj/project.pbxproj +++ b/r2-lcp-swift.xcodeproj/project.pbxproj @@ -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 */; }; @@ -61,6 +62,7 @@ CA4A3891220994E300599297 /* EPUBLicenseContainer.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EPUBLicenseContainer.swift; sourceTree = ""; }; CA50B87F22B2A777003AFF24 /* R2LCPLocalizedString.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = R2LCPLocalizedString.swift; sourceTree = ""; }; CA50B88522B2A822003AFF24 /* en */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = en; path = en.lproj/Localizable.strings; sourceTree = ""; }; + CA9428FC22B9442300305CDB /* LCPDFLicenseContainer.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LCPDFLicenseContainer.swift; sourceTree = ""; }; CAB131F5220D81E60097DFB5 /* LicensesRepository.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LicensesRepository.swift; sourceTree = ""; }; CAB1320A220D9B200097DFB5 /* LCPService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LCPService.swift; sourceTree = ""; }; CAB13210220DB2B10097DFB5 /* LCPAuthenticating.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LCPAuthenticating.swift; sourceTree = ""; }; @@ -155,6 +157,7 @@ CA4A388D220988C800599297 /* LCPLLicenseContainer.swift */, CA4A388F22098A9400599297 /* ZIPLicenseContainer.swift */, CA4A3891220994E300599297 /* EPUBLicenseContainer.swift */, + CA9428FC22B9442300305CDB /* LCPDFLicenseContainer.swift */, ); path = Container; sourceTree = ""; @@ -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 */, diff --git a/readium-lcp-swift/License/Container/LCPDFLicenseContainer.swift b/readium-lcp-swift/License/Container/LCPDFLicenseContainer.swift new file mode 100644 index 0000000..c6417ce --- /dev/null +++ b/readium-lcp-swift/License/Container/LCPDFLicenseContainer.swift @@ -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") + } + +} diff --git a/readium-lcp-swift/License/Container/LicenseContainer.swift b/readium-lcp-swift/License/Container/LicenseContainer.swift index 0b745e2..bf75f12 100644 --- a/readium-lcp-swift/License/Container/LicenseContainer.swift +++ b/readium-lcp-swift/License/Container/LicenseContainer.swift @@ -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 { @@ -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 + } +} diff --git a/readium-lcp-swift/License/License.swift b/readium-lcp-swift/License/License.swift index 3c4b1c7..e0fae7f 100644 --- a/readium-lcp-swift/License/License.swift +++ b/readium-lcp-swift/License/License.swift @@ -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) diff --git a/readium-lcp-swift/Services/LicensesService.swift b/readium-lcp-swift/Services/LicensesService.swift index b776401..457606c 100644 --- a/readium-lcp-swift/Services/LicensesService.swift +++ b/readium-lcp-swift/Services/LicensesService.swift @@ -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)) + } } }