Skip to content

Commit

Permalink
Merge pull request #89 from 3lvis/improve/store-downloads-in-document…
Browse files Browse the repository at this point in the history
…s-folder

Store downloads in documents folder
  • Loading branch information
3lvis committed May 14, 2016
2 parents e720929 + ff50926 commit c3533ee
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 28 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
39 changes: 22 additions & 17 deletions Sources/Networking.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 = TestCheck.isTesting ? NSSearchPathDirectory.CachesDirectory : 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)"])
}
}

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion Tests/Helpers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
14 changes: 7 additions & 7 deletions Tests/ImageTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions Tests/NetworkingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,15 @@ 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")
}

func testDestinationURLCache() {
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")
}

Expand Down

0 comments on commit c3533ee

Please sign in to comment.