Browse Source

Merge pull request #1114 from onevcat/fix/file-url-path

Mark wrong method as deprecated and add URL based one
Wei Wang 7 years ago
parent
commit
1227980be7
2 changed files with 39 additions and 10 deletions
  1. 35 7
      Sources/Cache/ImageCache.swift
  2. 4 3
      Tests/KingfisherTests/ImageCacheTests.swift

+ 35 - 7
Sources/Cache/ImageCache.swift

@@ -213,7 +213,7 @@ open class ImageCache {
     ///                   You should not use the same `name` for different caches, otherwise, the disk storage would
     ///                   be conflicting to each other. The `name` should not be an empty string.
     public convenience init(name: String) {
-        try! self.init(name: name, path: nil, diskCachePathClosure: nil)
+        try! self.init(name: name, cacheDirectoryURL: nil, diskCachePathClosure: nil)
     }
 
     /// Creates an `ImageCache` with a given `name`, cache directory `path`
@@ -223,21 +223,22 @@ open class ImageCache {
     ///   - name: The name of cache object. It is used to setup disk cache directories and IO queue.
     ///           You should not use the same `name` for different caches, otherwise, the disk storage would
     ///           be conflicting to each other.
-    ///   - path: Location of cache path on disk. It will be internally pass to the initializer of `DiskStorage` as the
-    ///           disk cache directory.
+    ///   - cacheDirectoryURL: Location of cache directory URL on disk. It will be internally pass to the
+    ///                        initializer of `DiskStorage` as the disk cache directory. If `nil`, the cache
+    ///                        directory under user domain mask will be used.
     ///   - diskCachePathClosure: Closure that takes in an optional initial path string and generates
     ///                           the final disk cache path. You could use it to fully customize your cache path.
     /// - Throws: An error that happens during image cache creating, such as unable to create a directory at the given
     ///           path.
     public convenience init(
         name: String,
-        path: String?,
+        cacheDirectoryURL: URL?,
         diskCachePathClosure: DiskCachePathClosure? = nil) throws
     {
         if name.isEmpty {
             fatalError("[Kingfisher] You should specify a name for the cache. A cache with empty name is not permitted.")
         }
-        
+
         let totalMemory = ProcessInfo.processInfo.physicalMemory
         let costLimit = totalMemory / 4
         let memoryStorage = MemoryStorage.Backend<Image>(config:
@@ -246,14 +247,14 @@ open class ImageCache {
         var diskConfig = DiskStorage.Config(
             name: name,
             sizeLimit: 0,
-            directory: path.flatMap { URL(string: $0) }
+            directory: cacheDirectoryURL
         )
         if let closure = diskCachePathClosure {
             diskConfig.cachePathBlock = closure
         }
         let diskStorage = try DiskStorage.Backend<Data>(config: diskConfig)
         diskConfig.cachePathBlock = nil
-        
+
         self.init(memoryStorage: memoryStorage, diskStorage: diskStorage)
     }
     
@@ -813,3 +814,30 @@ extension String {
         }
     }
 }
+
+extension ImageCache {
+
+    /// Creates an `ImageCache` with a given `name`, cache directory `path`
+    /// and a closure to modify the cache directory.
+    ///
+    /// - Parameters:
+    ///   - name: The name of cache object. It is used to setup disk cache directories and IO queue.
+    ///           You should not use the same `name` for different caches, otherwise, the disk storage would
+    ///           be conflicting to each other.
+    ///   - path: Location of cache URL on disk. It will be internally pass to the initializer of `DiskStorage` as the
+    ///           disk cache directory.
+    ///   - diskCachePathClosure: Closure that takes in an optional initial path string and generates
+    ///                           the final disk cache path. You could use it to fully customize your cache path.
+    /// - Throws: An error that happens during image cache creating, such as unable to create a directory at the given
+    ///           path.
+    @available(*, deprecated, message: "Use `init(name:cacheDirectoryURL:diskCachePathClosure:)` instead",
+    renamed: "init(name:cacheDirectoryURL:diskCachePathClosure:)")
+    public convenience init(
+        name: String,
+        path: String?,
+        diskCachePathClosure: DiskCachePathClosure? = nil) throws
+    {
+        let directoryURL = path.flatMap { URL(string: $0) }
+        try self.init(name: name, cacheDirectoryURL: directoryURL, diskCachePathClosure: diskCachePathClosure)
+    }
+}

+ 4 - 3
Tests/KingfisherTests/ImageCacheTests.swift

@@ -50,7 +50,8 @@ class ImageCacheTests: XCTestCase {
     
     func testInvalidCustomCachePath() {
         let customPath = "/path/to/image/cache"
-        XCTAssertThrowsError(try ImageCache(name: "test", path: customPath)) { error in
+        let url = URL(fileURLWithPath: customPath)
+        XCTAssertThrowsError(try ImageCache(name: "test", cacheDirectoryURL: url)) { error in
             guard case KingfisherError.cacheError(reason: .cannotCreateDirectory(let path, _)) = error else {
                 XCTFail("Should be KingfisherError with cacheError reason.")
                 return
@@ -65,7 +66,7 @@ class ImageCacheTests: XCTestCase {
         let subFolder = cacheURL.appendingPathComponent("temp")
 
         let customPath = subFolder.path
-        let cache = try! ImageCache(name: "test", path: customPath)
+        let cache = try! ImageCache(name: "test", cacheDirectoryURL: subFolder)
         XCTAssertEqual(
             cache.diskStorage.directoryURL.path,
             (customPath as NSString).appendingPathComponent("com.onevcat.Kingfisher.ImageCache.test"))
@@ -73,7 +74,7 @@ class ImageCacheTests: XCTestCase {
     }
     
     func testCustomCachePathByBlock() {
-        let cache = try! ImageCache(name: "test", path: nil, diskCachePathClosure: { (url, path) -> URL in
+        let cache = try! ImageCache(name: "test", cacheDirectoryURL: nil, diskCachePathClosure: { (url, path) -> URL in
             let modifiedPath = path + "-modified"
             return url.appendingPathComponent(modifiedPath, isDirectory: true)
         })