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 #117 from readium/fixes/l10n
Browse files Browse the repository at this point in the history
Localize the framework
  • Loading branch information
aferditamuriqi authored Jun 16, 2019
2 parents 04bfce8 + 2f7cf20 commit 44e59ae
Show file tree
Hide file tree
Showing 18 changed files with 60 additions and 105 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.11
github "readium/r2-shared-swift" == 1.2.12
github "dexman/Minizip"
github "edrlab/Fuzi" == 2.2.2
github "krzyzanowskim/CryptoSwift" == 0.15.0
Expand Down
2 changes: 1 addition & 1 deletion Cartfile.resolved
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
github "dexman/Minizip" "1.4.0"
github "edrlab/Fuzi" "2.2.2"
github "krzyzanowskim/CryptoSwift" "0.15.0"
github "readium/r2-shared-swift" "1.2.11"
github "readium/r2-shared-swift" "1.2.12"
github "swisspol/GCDWebServer" "3.5.2"
4 changes: 2 additions & 2 deletions r2-streamer-swift.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -245,10 +245,10 @@
CA130D25229D4245000A627C /* Parser */ = {
isa = PBXGroup;
children = (
CA130D26229D4245000A627C /* CBZ */,
CA130D29229D4245000A627C /* PDF */,
CA130D2E229D4245000A627C /* PublicationParser.swift */,
CA130D26229D4245000A627C /* CBZ */,
CA130D2F229D4245000A627C /* EPUB */,
CA130D29229D4245000A627C /* PDF */,
);
path = Parser;
sourceTree = "<group>";
Expand Down
10 changes: 5 additions & 5 deletions r2-streamer-swift/Fetcher/ContentFilter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import Fuzi
/// Protocol defining the content filters. They are implemented below and used
/// in the fetcher. They come in different flavors depending of the container
/// data mimetype.
internal protocol ContentFilters {
internal protocol ContentFilters: Loggable {
init()

func apply(to input: SeekableInputStream,
Expand Down Expand Up @@ -161,12 +161,12 @@ final internal class ContentFiltersEpub: ContentFilters {

// Inserting at the start of <HEAD>.
guard let headStart = resourceHtml.endIndex(of: "<head>") else {
print("Invalid resource")
log(.error, "Invalid resource")
abort()
}

guard let baseUrl = publication.baseURL?.deletingLastPathComponent() else {
print("Invalid host")
log(.error, "Invalid host")
abort()
}

Expand All @@ -184,7 +184,7 @@ final internal class ContentFiltersEpub: ContentFilters {

// Inserting at the end of <HEAD>.
guard let headEnd = resourceHtml.startIndex(of: "</head>") else {
print("Invalid resource")
log(.error, "Invalid resource")
abort()
}
let cssAfter = getHtmlLink(forResource: "\(baseUrl)styles/\(styleSubFolder)/ReadiumCSS-after.css")
Expand Down Expand Up @@ -214,7 +214,7 @@ final internal class ContentFiltersEpub: ContentFilters {
return stream
}
guard let endHeadIndex = resourceHtml.startIndex(of: "</head>") else {
print("Invalid resource")
log(.error, "Invalid resource")
abort()
}

Expand Down
4 changes: 2 additions & 2 deletions r2-streamer-swift/Fetcher/DRMDecoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import Foundation
import R2Shared

/// Decrypt DRM encrypted content.
class DrmDecoder {
class DrmDecoder: Loggable {

/// Decode the given stream using DRM. If it fails, just return the
/// stream unchanged.
Expand Down Expand Up @@ -47,7 +47,7 @@ class DrmDecoder {

data = data.subdata(in: Range.init(uncheckedBounds: (0, data.count - padding)))
guard let inflatedBuffer = data.inflate() else {
print("Inflate error")
log(.error, "Inflate error")
return input
}
data = inflatedBuffer
Expand Down
4 changes: 2 additions & 2 deletions r2-streamer-swift/Fetcher/Fetcher.swift
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,9 @@ internal class Fetcher {
throw FetcherError.missingContainerMimetype
}
switch mimeType {
case EpubConstant.mimetype, EpubConstant.mimetypeOEBPS:
case EPUBConstant.mimetype, EPUBConstant.mimetypeOEBPS:
return ContentFiltersEpub()
case CbzConstant.mimetype:
case CBZConstant.mimetype:
return ContentFiltersCbz()
case PDFConstant.pdfMimetype, PDFConstant.lcpdfMimetype:
return ContentFiltersPDF()
Expand Down
24 changes: 6 additions & 18 deletions r2-streamer-swift/Model/Container.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,29 +21,18 @@ import R2Shared
/// - xmlParse: An error occured while parsing XML (See underlyingError for more infos).
/// - missingLink: The given `Link` ressource couldn't be found in the container.
public enum ContainerError: Error {
// Stream initialization failed.
case streamInitFailed
// The file couldn't be found.
case fileNotFound
// An error occured while accessing the file attributes.
case fileError
// The file is missing from the publication.
case missingFile(path: String)
// Error while parsing XML
case xmlParse(underlyingError: Error)
// The link with given title couldn't be found in the container
case missingLink(title: String?)

public var localizedDescription: String {
switch self {
case .streamInitFailed:
return "Stream initialization failed."
case .fileNotFound:
return "The file couldn't be found."
case .fileError:
return "An error occured while accessing the file attributes."
case .missingFile(let path):
return "The file at \(path) is missing from the archive."
case .xmlParse(let underlyingError):
return "Error while parsing XML: \(underlyingError.localizedDescription)"
case .missingLink(let title):
return "The link, titled \(title ?? "missing"), couldn't be found in the container."
}
}
}

/// Provide methods for accessing raw data from container's files.
Expand Down Expand Up @@ -91,7 +80,6 @@ public extension Container {
let url = NSURL(fileURLWithPath: rootFile.rootPath)
var modificationDate : AnyObject?
try? url.getResourceValue(&modificationDate, forKey: .contentModificationDateKey)
print("\(rootFile.rootPath) - \(modificationDate as! Date)")
return (modificationDate as? Date) ?? Date()
}

Expand Down
4 changes: 2 additions & 2 deletions r2-streamer-swift/Parser/CBZ/CBZContainer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ protocol CBZContainer: Container {
final class CBZArchiveContainer: ArchiveContainer, CBZContainer {

init?(path: String) {
super.init(path: path, mimetype: CbzConstant.mimetype)
super.init(path: path, mimetype: CBZConstant.mimetype)

do {
try archive.buildFilesList()
Expand All @@ -48,7 +48,7 @@ final class CBZArchiveContainer: ArchiveContainer, CBZContainer {
final class CBZDirectoryContainer: DirectoryContainer, CBZContainer {

init?(directory: String) {
super.init(directory: directory, mimetype: CbzConstant.mimetype)
super.init(directory: directory, mimetype: CBZConstant.mimetype)
}

var files: [String] {
Expand Down
24 changes: 10 additions & 14 deletions r2-streamer-swift/Parser/CBZ/CBZParser.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// CbzParser.swift
// CBZParser.swift
// r2-streamer-swift
//
// Created by Alexandre Camilleri on 3/31/17.
Expand All @@ -15,19 +15,15 @@ import R2Shared
/// Errors related to the CBZ publications.
///
/// - missingFile: The file at 'path' is missing from the container.
public enum CbzParserError: LocalizedError {
public enum CBZParserError: Error {
case missingFile(path: String)

public var errorDescription: String? {
switch self {
case .missingFile(let path):
return "The file '\(path)' is missing."
}
}
}

@available(*, deprecated, renamed: "CBZParserError")
public typealias CbzParserError = CBZParserError

/// CBZ related constants.
public struct CbzConstant {
struct CBZConstant {
public static let mimetype = "application/x-cbr"
}

Expand Down Expand Up @@ -65,7 +61,7 @@ public class CbzParser: PublicationParser {
///
/// - Parameter path: The path of the file to parse.
/// - Returns: The resulting `PubBox` object.
/// - Throws: Throws `CbzParserError.missingFile`.
/// - Throws: Throws `CBZParserError.missingFile`.
public static func parse(fileAtPath path: String) throws -> (PubBox, PubParsingCallback) {
let container = try generateContainerFrom(fileAtPath: path)
let publication = parsePublication(in: container, at: path)
Expand Down Expand Up @@ -109,13 +105,13 @@ public class CbzParser: PublicationParser {
///
/// - Parameter path: The absolute path of the file.
/// - Returns: The generated Container.
/// - Throws: `EpubParserError.missingFile`.
/// - Throws: `CBZParserError.missingFile`.
private static func generateContainerFrom(fileAtPath path: String) throws -> CBZContainer {
var container: CBZContainer?
var isDirectory: ObjCBool = false

guard FileManager.default.fileExists(atPath: path, isDirectory: &isDirectory) else {
throw CbzParserError.missingFile(path: path)
throw CBZParserError.missingFile(path: path)
}
if isDirectory.boolValue {
container = CBZDirectoryContainer(directory: path)
Expand All @@ -124,7 +120,7 @@ public class CbzParser: PublicationParser {
}

guard let containerUnwrapped = container else {
throw CbzParserError.missingFile(path: path)
throw CBZParserError.missingFile(path: path)
}
return containerUnwrapped
}
Expand Down
4 changes: 2 additions & 2 deletions r2-streamer-swift/Parser/EPUB/EPUBContainerParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@ final class EPUBContainerParser: Loggable {
let data = try container.data(relativePath: path)
try self.init(data: data)
} catch {
throw EpubParserError.missingFile(path: path)
throw EPUBParserError.missingFile(path: path)
}
}

/// Parses the container.xml file and retrieves the relative path to the OPF file (rootFilePath) (the default one for now, not handling multiple renditions).
func parseRootFilePath() throws -> String {
// Get the path of the OPF file, relative to the metadata.rootPath.
guard let path = document.firstChild(xpath: "/cn:container/cn:rootfiles/cn:rootfile")?.attr("full-path") else {
throw EpubParserError.missingElement(message: "Missing rootfile in `container.xml`.")
throw EPUBParserError.missingRootfile
}
return path
}
Expand Down
2 changes: 1 addition & 1 deletion r2-streamer-swift/Parser/EPUB/EPUBEncryptionParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ final class EPUBEncryptionParser: Loggable {
let data = try container.data(relativePath: path)
self.init(data: data, drm: drm)
} catch {
throw EpubParserError.missingFile(path: path)
throw EPUBParserError.missingFile(path: path)
}
}

Expand Down
44 changes: 18 additions & 26 deletions r2-streamer-swift/Parser/EPUB/EPUBParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import R2Shared
import Fuzi

/// Epub related constants.
public struct EpubConstant {
struct EPUBConstant {
/// Lcpl file path.
public static let lcplFilePath = "META-INF/license.lcpl"
/// Epub mime-type.
Expand All @@ -31,26 +31,18 @@ public struct EpubConstant {
/// - missingFile: A file is missing from the container at `path`.
/// - xmlParse: An XML parsing error occurred.
/// - missingElement: An XML element is missing.
public enum EpubParserError: LocalizedError {
public enum EPUBParserError: Error {
/// The mimetype of the EPUB is not valid.
case wrongMimeType
case missingFile(path: String)
case xmlParse(underlyingError: Error)
case missingElement(message: String)

public var errorDescription: String? {
switch self {
case .wrongMimeType:
return "The mimetype of the Epub is not valid."
case .missingFile(let path):
return "The file '\(path)' is missing."
case .xmlParse(let underlyingError):
return "Error while parsing XML (\(underlyingError))."
case .missingElement(let message):
return "Missing element: \(message)."
}
}
/// Missing rootfile in `container.xml`.
case missingRootfile
}

@available(*, deprecated, renamed: "EPUBParserError")
public typealias EpubParserError = EPUBParserError

extension EpubParser: Loggable {}

/// An EPUB container parser that extracts the information from the relevant
Expand All @@ -66,9 +58,9 @@ final public class EpubParser: PublicationParser {
/// The point is to get DRM informations in the DRM object, and
/// inform the decypher() function in the DRM object to allow
/// the fetcher to decypher encrypted resources.
/// - Throws: `EpubParserError.wrongMimeType`,
/// `EpubParserError.xmlParse`,
/// `EpubParserError.missingFile`
/// - Throws: `EPUBParserError.wrongMimeType`,
/// `EPUBParserError.xmlParse`,
/// `EPUBParserError.missingFile`
static public func parse(fileAtPath path: String) throws -> (PubBox, PubParsingCallback) {
// Generate the `Container` for `fileAtPath`
var container = try generateContainerFrom(fileAtPath: path)
Expand Down Expand Up @@ -127,7 +119,7 @@ final public class EpubParser: PublicationParser {
static internal func scanForDRM(in container: Container) -> DRM? {
/// LCP.
// Check if a LCP license file is present in the container.
if ((try? container.data(relativePath: EpubConstant.lcplFilePath)) != nil) {
if ((try? container.data(relativePath: EPUBConstant.lcplFilePath)) != nil) {
return DRM(brand: .lcp)
}
return nil
Expand Down Expand Up @@ -228,7 +220,7 @@ final public class EpubParser: PublicationParser {
continue
}
link.mediaOverlays.append(node)
link.properties.mediaOverlay = EpubConstant.mediaOverlayURL + link.href
link.properties.mediaOverlay = EPUBConstant.mediaOverlayURL + link.href
}
}

Expand All @@ -237,21 +229,21 @@ final public class EpubParser: PublicationParser {
///
/// - Parameter path: The absolute path of the file.
/// - Returns: The generated Container.
/// - Throws: `EpubParserError.missingFile`.
/// - Throws: `EPUBParserError.missingFile`.
static fileprivate func generateContainerFrom(fileAtPath path: String) throws -> Container {
var isDirectory: ObjCBool = false
guard FileManager.default.fileExists(atPath: path, isDirectory: &isDirectory) else {
throw EpubParserError.missingFile(path: path)
throw EPUBParserError.missingFile(path: path)
}

guard let container: Container = {
if isDirectory.boolValue {
return DirectoryContainer(directory: path, mimetype: EpubConstant.mimetype)
return DirectoryContainer(directory: path, mimetype: EPUBConstant.mimetype)
} else {
return ArchiveContainer(path: path, mimetype: EpubConstant.mimetype)
return ArchiveContainer(path: path, mimetype: EPUBConstant.mimetype)
}
}() else {
throw EpubParserError.missingFile(path: path)
throw EPUBParserError.missingFile(path: path)
}

container.rootFile.rootFilePath = try EPUBContainerParser(container: container).parseRootFilePath()
Expand Down
10 changes: 1 addition & 9 deletions r2-streamer-swift/Parser/EPUB/OPFParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,8 @@ public enum EPUBTitleType: String {
public enum OPFParserError: Error {
/// The Epub have no title. Title is mandatory.
case missingPublicationTitle
/// Smile resource couldn't be parsed.
case invalidSmilResource

var localizedDescription: String {
switch self {
case .missingPublicationTitle:
return "The publication is missing a title."
case .invalidSmilResource:
return "Smile resource couldn't beparsed."
}
}
}

/// EpubParser support class, able to parse the OPF package document.
Expand Down
2 changes: 1 addition & 1 deletion r2-streamer-swift/Parser/EPUB/SMILParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ final class SMILParser {
continue
}
link.mediaOverlays.append(newNode)
link.properties.mediaOverlay = EpubConstant.mediaOverlayURL + link.href
link.properties.mediaOverlay = EPUBConstant.mediaOverlayURL + link.href
}
}

Expand Down
Loading

0 comments on commit 44e59ae

Please sign in to comment.