Răsfoiți Sursa

Merge pull request #1688 from onevcat/tctony-feature/autoExtAfterHashedFileName

Tctony feature/auto ext after hashed file name
Wei Wang 4 ani în urmă
părinte
comite
65ec4c4b1a

+ 8 - 0
Sources/Cache/DiskStorage.swift

@@ -316,6 +316,9 @@ public enum DiskStorage {
                 let hashedKey = key.kf.md5
                 if let ext = config.pathExtension {
                     return "\(hashedKey).\(ext)"
+                } else if config.autoExtAfterHashedFileName,
+                          let ext = key.kf.ext {
+                    return "\(hashedKey).\(ext)"
                 }
                 return hashedKey
             } else {
@@ -448,6 +451,11 @@ extension DiskStorage {
         /// Default is `true`, means that the cache file name will be hashed before storing.
         public var usesHashedFileName = true
 
+        /// Default is `false`
+        /// 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
         let fileManager: FileManager
         let directory: URL?

+ 12 - 0
Sources/Utility/String+MD5.swift

@@ -53,6 +53,18 @@ extension KingfisherWrapper where Base == String {
         }
         return MD5String
     }
+
+    var ext: String? {
+        var ext = ""
+        if let index  = base.lastIndex(of: ".") {
+            let extRange = base.index(index, offsetBy: 1)..<base.endIndex
+            ext = String(base[extRange])
+        }
+        guard let firstSeg = ext.split(separator: "@").first else {
+            return nil
+        }
+        return firstSeg.count > 0 ? String(firstSeg) : nil
+    }
 }
 
 // array of bytes, little-endian representation

+ 35 - 0
Tests/KingfisherTests/DiskStorageTests.swift

@@ -194,6 +194,41 @@ class DiskStorageTests: XCTestCase {
         XCTAssertEqual(originalFileName, key)
     }
 
+    func testConfigUsesHashedFileNameWithAutoExt() {
+        let key = "test.gif"
+
+        // 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 + ".gif")
+
+        // fileName without hash
+        storage.config.usesHashedFileName = false
+        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")!]