Skip to content
This repository was archived by the owner on Nov 26, 2020. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 23 additions & 23 deletions Source/EPUBCore/FREpubParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,29 +26,41 @@ class FREpubParser: NSObject, SSZipArchiveDelegate {
Returns an UIImage.
*/
func parseCoverImage(epubPath: String) -> UIImage? {
let book = readEpub(epubPath: epubPath, removeEpub: false)

// Read the cover image
if let coverImage = book.coverImage {
return UIImage(contentsOfFile: coverImage.fullHref)
guard let book = readEpub(epubPath: epubPath, removeEpub: false), let coverImage = book.coverImage else {
return nil
}

return nil
return UIImage(contentsOfFile: coverImage.fullHref)
}

/**
Unzip, delete and read an epub file.
Returns a FRBook.
*/

func readEpub(epubPath withEpubPath: String, removeEpub: Bool = true) -> FRBook {
func readEpub(epubPath withEpubPath: String, removeEpub: Bool = true) -> FRBook? {
epubPathToRemove = withEpubPath
shouldRemoveEpub = removeEpub

// Unzip
var isDir: ObjCBool = false
let fileManager = NSFileManager.defaultManager()
let bookName = (withEpubPath as NSString).lastPathComponent
bookBasePath = (kApplicationDocumentsDirectory as NSString).stringByAppendingPathComponent(bookName)
SSZipArchive.unzipFileAtPath(withEpubPath, toDestination: bookBasePath, delegate: self)

guard fileManager.fileExistsAtPath(withEpubPath) else {
print("Epub file does not exist.")
return nil
}

// Unzip if necessary
var needsUnzip = false
if fileManager.fileExistsAtPath(bookBasePath, isDirectory:&isDir) {
if !isDir { needsUnzip = true }
} else {
needsUnzip = true
}

if needsUnzip {
SSZipArchive.unzipFileAtPath(withEpubPath, toDestination: bookBasePath, delegate: self)
}

// Skip from backup this folder
addSkipBackupAttributeToItemAtURL(NSURL(fileURLWithPath: bookBasePath, isDirectory: true))
Expand All @@ -59,18 +71,6 @@ class FREpubParser: NSObject, SSZipArchiveDelegate {
return book
}

/**
Read an unziped epub file.
Returns a FRBook.
*/
func readEpub(filePath withFilePath: String) -> FRBook {
bookBasePath = withFilePath
kBookId = (withFilePath as NSString).lastPathComponent
readContainer()
readOpf()
return book
}

/**
Read and parse container.xml file.
*/
Expand Down
12 changes: 2 additions & 10 deletions Source/FolioReaderContainer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -125,17 +125,9 @@ public class FolioReaderContainer: UIViewController {

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), { () -> Void in

var isDir: ObjCBool = false
let fileManager = NSFileManager.defaultManager()

if fileManager.fileExistsAtPath(self.epubPath, isDirectory:&isDir) {
if isDir {
book = FREpubParser().readEpub(filePath: self.epubPath)
}

book = FREpubParser().readEpub(epubPath: self.epubPath, removeEpub: self.shouldRemoveEpub)
if let parsedBook = FREpubParser().readEpub(epubPath: self.epubPath, removeEpub: self.shouldRemoveEpub) {
book = parsedBook
} else {
print("Epub file does not exist.")
self.errorOnLoad = true
}

Expand Down