From 9ced0ffe7c729689c41ba7cb89243391af4b4d45 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elvis=20Nu=C3=B1ez?= Date: Sat, 14 May 2016 14:45:08 +0200 Subject: [PATCH 1/2] Store downloads in documents folder --- README.md | 2 +- Sources/Networking.swift | 39 +++++++++++++++++++++---------------- Tests/Helpers.swift | 2 +- Tests/ImageTests.swift | 14 ++++++------- Tests/NetworkingTests.swift | 4 ++-- 5 files changed, 33 insertions(+), 28 deletions(-) diff --git a/README.md b/README.md index 7d5d3ed..3af193b 100755 --- a/README.md +++ b/README.md @@ -266,7 +266,7 @@ If you want to remove the downloaded image you can do it like this: ```swift let networking = Networking(baseURL: "http://httpbin.org") -let destinationURL = networking.destinationURL("/image/png") +let destinationURL = try networking.destinationURL("/image/png") if let path = destinationURL.path where NSFileManager.defaultManager().fileExistsAtPath(path) { try! NSFileManager.defaultManager().removeItemAtPath(path) } diff --git a/Sources/Networking.swift b/Sources/Networking.swift index 18a16f8..c5f92cd 100644 --- a/Sources/Networking.swift +++ b/Sources/Networking.swift @@ -179,22 +179,27 @@ public class Networking { - parameter path: The path used to download the resource. - returns: A NSURL where a resource has been stored. */ - public func destinationURL(path: String, cacheName: String? = nil) -> NSURL { - if let cacheName = cacheName { - let replacedPath = cacheName.stringByReplacingOccurrencesOfString("/", withString: "-") - guard let url = NSURL(string: replacedPath) else { fatalError("Couldn't create a destination url using cacheName: \(replacedPath)") } - guard let cachesURL = NSFileManager.defaultManager().URLsForDirectory(.CachesDirectory, inDomains: .UserDomainMask).first else { fatalError("Couldn't normalize url") } - let destinationURL = cachesURL.URLByAppendingPathComponent(url.absoluteString) - - return destinationURL + public func destinationURL(path: String, cacheName: String? = nil) throws -> NSURL { + #if os(tvOS) + let directory = NSSearchPathDirectory.CachesDirectory + #else + let directory = NSSearchPathDirectory.DocumentDirectory + #endif + let finalPath = cacheName ?? self.urlForPath(path).absoluteString + let replacedPath = finalPath.stringByReplacingOccurrencesOfString("/", withString: "-") + if let url = NSURL(string: replacedPath) { + if let cachesURL = NSFileManager.defaultManager().URLsForDirectory(directory, inDomains: .UserDomainMask).first { + #if !os(tvOS) + try cachesURL.setResourceValue(true, forKey: NSURLIsExcludedFromBackupKey) + #endif + let destinationURL = cachesURL.URLByAppendingPathComponent(url.absoluteString) + + return destinationURL + } else { + throw NSError(domain: Networking.ErrorDomain, code: 9999, userInfo: [NSLocalizedDescriptionKey : "Couldn't normalize url"]) + } } else { - let finalPath = self.urlForPath(path).absoluteString - let replacedPath = finalPath.stringByReplacingOccurrencesOfString("/", withString: "-") - guard let url = NSURL(string: replacedPath) else { fatalError("Couldn't create a url using replacedPath: \(replacedPath)") } - guard let cachesURL = NSFileManager.defaultManager().URLsForDirectory(.CachesDirectory, inDomains: .UserDomainMask).first else { fatalError("Couldn't normalize url") } - let destinationURL = cachesURL.URLByAppendingPathComponent(url.absoluteString) - - return destinationURL + throw NSError(domain: Networking.ErrorDomain, code: 9999, userInfo: [NSLocalizedDescriptionKey : "Couldn't create a url using replacedPath: \(replacedPath)"]) } } @@ -264,7 +269,7 @@ public class Networking { extension Networking { func objectFromCache(path: String, cacheName: String? = nil, responseType: ResponseType, completion: (object: AnyObject?) -> Void) { - let destinationURL = self.destinationURL(path, cacheName: cacheName) + let destinationURL = try! self.destinationURL(path, cacheName: cacheName) if let object = self.cache.objectForKey(destinationURL.absoluteString) { completion(object: object) @@ -375,7 +380,7 @@ extension Networking { self.dataRequest(requestType, path: path, cacheName: cacheName, parameterType: parameterType, parameters: parameters, responseType: responseType) { data, error in var returnedResponse: AnyObject? if let data = data where data.length > 0 { - let destinationURL = self.destinationURL(path, cacheName: cacheName) + let destinationURL = try! self.destinationURL(path, cacheName: cacheName) data.writeToURL(destinationURL, atomically: true) switch responseType { case .Data: diff --git a/Tests/Helpers.swift b/Tests/Helpers.swift index 8ad676d..d54462c 100644 --- a/Tests/Helpers.swift +++ b/Tests/Helpers.swift @@ -2,7 +2,7 @@ import Foundation struct Helper { static func removeFileIfNeeded(networking: Networking, path: String, cacheName: String? = nil) { - let destinationURL = networking.destinationURL(path, cacheName: cacheName) + let destinationURL = try! networking.destinationURL(path, cacheName: cacheName) if NSFileManager.defaultManager().fileExistsAtURL(destinationURL) { NSFileManager.defaultManager().removeFileAtURL(destinationURL) } diff --git a/Tests/ImageTests.swift b/Tests/ImageTests.swift index 37524e0..4aa602b 100644 --- a/Tests/ImageTests.swift +++ b/Tests/ImageTests.swift @@ -66,7 +66,7 @@ class ImageTests: XCTestCase { Helper.removeFileIfNeeded(networking, path: path) networking.downloadImage(path) { image, error in - let destinationURL = networking.destinationURL(path) + let destinationURL = try! networking.destinationURL(path) XCTAssertTrue(NSFileManager.defaultManager().fileExistsAtURL(destinationURL)) let data = NSFileManager.defaultManager().contentsAtPath(destinationURL.path!) XCTAssertEqual(data?.length, 8090) @@ -81,7 +81,7 @@ class ImageTests: XCTestCase { Helper.removeFileIfNeeded(networking, path: path, cacheName: cacheName) networking.downloadImage(path, cacheName: cacheName) { image, error in - let destinationURL = networking.destinationURL(path, cacheName: cacheName) + let destinationURL = try! networking.destinationURL(path, cacheName: cacheName) XCTAssertTrue(NSFileManager.defaultManager().fileExistsAtURL(destinationURL)) let data = NSFileManager.defaultManager().contentsAtPath(destinationURL.path!) XCTAssertEqual(data?.length, 8090) @@ -95,7 +95,7 @@ class ImageTests: XCTestCase { Helper.removeFileIfNeeded(networking, path: path) networking.downloadImage(path) { image, error in - let destinationURL = networking.destinationURL(path) + let destinationURL = try! networking.destinationURL(path) let image = networking.cache.objectForKey(destinationURL.absoluteString) as! UIImage let pigImage = UIImage(named: "pig.png", inBundle: NSBundle(forClass: ImageTests.self), compatibleWithTraitCollection: nil)! let pigImageData = UIImagePNGRepresentation(pigImage) @@ -112,7 +112,7 @@ class ImageTests: XCTestCase { Helper.removeFileIfNeeded(networking, path: path, cacheName: cacheName) networking.downloadImage(path, cacheName: cacheName) { image, error in - let destinationURL = networking.destinationURL(path, cacheName: cacheName) + let destinationURL = try! networking.destinationURL(path, cacheName: cacheName) let image = networking.cache.objectForKey(destinationURL.absoluteString) as! UIImage let pigImage = UIImage(named: "pig.png", inBundle: NSBundle(forClass: ImageTests.self), compatibleWithTraitCollection: nil)! let pigImageData = UIImagePNGRepresentation(pigImage) @@ -215,7 +215,7 @@ class ImageTests: XCTestCase { let path = "/image/png" Helper.removeFileIfNeeded(networking, path: path) networking.downloadImage(path) { image, error in - let destinationURL = networking.destinationURL(path) + let destinationURL = try! networking.destinationURL(path) cache.removeObjectForKey(destinationURL.absoluteString) networking.imageFromCache(path) { image in synchronous = true @@ -237,7 +237,7 @@ class ImageTests: XCTestCase { let cacheName = "hello" Helper.removeFileIfNeeded(networking, path: path, cacheName: cacheName) networking.downloadImage(path, cacheName: cacheName) { image, error in - let destinationURL = networking.destinationURL(path, cacheName: cacheName) + let destinationURL = try! networking.destinationURL(path, cacheName: cacheName) cache.removeObjectForKey(destinationURL.absoluteString) networking.imageFromCache(path, cacheName: cacheName) { image in synchronous = true @@ -258,7 +258,7 @@ class ImageTests: XCTestCase { let path = "/image/png" Helper.removeFileIfNeeded(networking, path: path) networking.downloadImage(path) { image, error in - let destinationURL = networking.destinationURL(path) + let destinationURL = try! networking.destinationURL(path) cache.removeObjectForKey(destinationURL.absoluteString) Helper.removeFileIfNeeded(networking, path: path) networking.imageFromCache(path) { image in diff --git a/Tests/NetworkingTests.swift b/Tests/NetworkingTests.swift index 4a06ac4..c7b9939 100644 --- a/Tests/NetworkingTests.swift +++ b/Tests/NetworkingTests.swift @@ -45,7 +45,7 @@ class NetworkingTests: XCTestCase { func testDestinationURL() { let networking = Networking(baseURL: baseURL) let path = "/image/png" - let destinationURL = networking.destinationURL(path) + let destinationURL = try! networking.destinationURL(path) XCTAssertEqual(destinationURL.lastPathComponent!, "http:--httpbin.org-image-png") } @@ -53,7 +53,7 @@ class NetworkingTests: XCTestCase { let networking = Networking(baseURL: baseURL) let path = "/image/png" let cacheName = "png/png" - let destinationURL = networking.destinationURL(path, cacheName: cacheName) + let destinationURL = try! networking.destinationURL(path, cacheName: cacheName) XCTAssertEqual(destinationURL.lastPathComponent!, "png-png") } From ff50926154f59e4068b49b734dd8c1744522941f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elvis=20Nu=C3=B1ez?= Date: Sat, 14 May 2016 14:52:01 +0200 Subject: [PATCH 2/2] Use caches directory when testing --- Sources/Networking.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/Networking.swift b/Sources/Networking.swift index c5f92cd..1fbe0be 100644 --- a/Sources/Networking.swift +++ b/Sources/Networking.swift @@ -183,7 +183,7 @@ public class Networking { #if os(tvOS) let directory = NSSearchPathDirectory.CachesDirectory #else - let directory = NSSearchPathDirectory.DocumentDirectory + let directory = TestCheck.isTesting ? NSSearchPathDirectory.CachesDirectory : NSSearchPathDirectory.DocumentDirectory #endif let finalPath = cacheName ?? self.urlForPath(path).absoluteString let replacedPath = finalPath.stringByReplacingOccurrencesOfString("/", withString: "-")