Bläddra i källkod

Add processor support to autoExtAfterHashedFileName

onevcat 4 år sedan
förälder
incheckning
71b5402018

+ 2 - 1
Sources/Cache/DiskStorage.swift

@@ -452,7 +452,8 @@ extension DiskStorage {
         public var usesHashedFileName = true
 
         /// Default is `false`
-        /// if set to `true`, image extension will be extracted from original file name and append to hasedFileName
+        /// If set to `true`, image extension will be extracted from original file name and append to
+        /// the hased file name and used as the cache key on disk.
         public var autoExtAfterHashedFileName = false
 
         let name: String

+ 4 - 1
Sources/Utility/String+MD5.swift

@@ -60,7 +60,10 @@ extension KingfisherWrapper where Base == String {
             let extRange = base.index(index, offsetBy: 1)..<base.endIndex
             ext = String(base[extRange])
         }
-        return ext.count > 0 ? ext : nil
+        guard let firstSeg = ext.split(separator: "@").first else {
+            return nil
+        }
+        return firstSeg.count > 0 ? String(firstSeg) : nil
     }
 }
 

+ 18 - 0
Tests/KingfisherTests/DiskStorageTests.swift

@@ -210,6 +210,24 @@ class DiskStorageTests: XCTestCase {
         let originalFileName = storage.cacheFileName(forKey: key)
         XCTAssertEqual(originalFileName, key)
     }
+    
+    func testConfigUsesHashedFileNameWithAutoExtAndProcessor() {
+        // The key of an image with processor will be as this format.
+        let key = "test.jpeg@abc"
+        
+        // hashed fileName
+        storage.config.usesHashedFileName = true
+        storage.config.autoExtAfterHashedFileName = true
+        let hashedFileName = storage.cacheFileName(forKey: key)
+        XCTAssertNotEqual(hashedFileName, key)
+        // validation md5 hash of the key
+        XCTAssertEqual(hashedFileName, key.kf.md5 + ".jpeg")
+
+        // fileName without hash
+        storage.config.usesHashedFileName = false
+        let originalFileName = storage.cacheFileName(forKey: key)
+        XCTAssertEqual(originalFileName, key)
+    }
 
     func testFileMetaOrder() {
         let urls = [URL(string: "test1")!, URL(string: "test2")!, URL(string: "test3")!]