Skip to content
This repository has been archived by the owner on Nov 26, 2020. It is now read-only.

Swift23 #161

Merged
merged 8 commits into from
Oct 6, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Prevent to always unzip the ePub, performance improvement 📈
  • Loading branch information
hebertialmeida committed Sep 20, 2016
commit eae0d7bed1a3c12fb5b6ac7bc6361d265c0e77b2
45 changes: 24 additions & 21 deletions Source/EPUBCore/FREpubParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,29 +26,44 @@ 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 +74,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 @@ -124,17 +124,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