diff --git a/Source/EPUBCore/FREpubParser.swift b/Source/EPUBCore/FREpubParser.swift index 6cc0e43c5..1ac88baac 100755 --- a/Source/EPUBCore/FREpubParser.swift +++ b/Source/EPUBCore/FREpubParser.swift @@ -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)) @@ -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. */ diff --git a/Source/FolioReaderContainer.swift b/Source/FolioReaderContainer.swift index 151f6dfc1..53a3bdddf 100755 --- a/Source/FolioReaderContainer.swift +++ b/Source/FolioReaderContainer.swift @@ -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 }