forked from avito-tech/Emcee
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 in MA/ios-test-runner from MBS-2930_accept_URLs…
… to master Automatically merge pull request #3 in MA/ios-test-runner from MBS-2930_accept_URLs to master * commit '96d4bac24f2036f15c8e0f29750c6d1fcc19496c': MBS-2930: less accuracy in unit test MBS-2930: renaming MBS-2930: use semaphore MBS-2930: resolve fb tools paths through ResourceLocation MBS-2930: URLResource + tests MBS-2930: FileCache with tests
- Loading branch information
Showing
25 changed files
with
578 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import Foundation | ||
|
||
extension Data { | ||
public func avito_sha256Hash() -> Data { | ||
let transform = SecDigestTransformCreate(kSecDigestSHA2, 256, nil) | ||
SecTransformSetAttribute(transform, kSecTransformInputAttributeName, self as CFTypeRef, nil) | ||
return SecTransformExecute(transform, nil) as! Data | ||
} | ||
} | ||
|
||
extension String { | ||
public enum AvitoSHA256Error: Error { | ||
case unableToHash | ||
} | ||
|
||
public func avito_sha256Hash(encoding: String.Encoding = .utf8) throws -> String { | ||
guard let dataToHash = self.data(using: encoding) else { throw AvitoSHA256Error.unableToHash } | ||
let hashedData = dataToHash.avito_sha256Hash() | ||
|
||
return hashedData.reduce("") { (result, byte) -> String in | ||
result + String(format:"%02x", UInt8(byte)) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import Foundation | ||
|
||
public extension FileCache { | ||
private func itemNameForUrl(_ url: URL) -> String { | ||
return url.absoluteString | ||
} | ||
|
||
func contains(itemForURL url: URL) -> Bool { | ||
return self.contains(itemWithName: itemNameForUrl(url)) | ||
} | ||
|
||
func urlForCachedContents(ofUrl url: URL) throws -> URL { | ||
return try self.url(forItemWithName: itemNameForUrl(url)) | ||
} | ||
|
||
func store(contentsUrl: URL, ofUrl url: URL) throws { | ||
try self.store(itemAtURL: contentsUrl, underName: itemNameForUrl(url)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import Extensions | ||
import Foundation | ||
|
||
public final class FileCache { | ||
private let cachesUrl: URL | ||
private let nameKeyer: NameKeyer | ||
private let fileManager = FileManager() | ||
|
||
public init(cachesUrl: URL, nameHasher: NameKeyer = SHA256NameKeyer()) { | ||
self.cachesUrl = cachesUrl | ||
self.nameKeyer = nameHasher | ||
} | ||
|
||
// MARK: - Public API | ||
|
||
public func contains(itemWithName name: String) -> Bool { | ||
do { | ||
let fileUrl = try url(forItemWithName: name) | ||
return fileManager.fileExists(atPath: fileUrl.path) | ||
} catch { | ||
return false | ||
} | ||
} | ||
|
||
public func remove(itemWithName name: String) throws { | ||
let container = try containerUrl(forItemWithName: name) | ||
try fileManager.removeItem(at: container) | ||
} | ||
|
||
public func store(itemAtURL itemUrl: URL, underName name: String) throws { | ||
if contains(itemWithName: name) { | ||
try remove(itemWithName: name) | ||
} | ||
|
||
let container = try containerUrl(forItemWithName: name) | ||
let filename = itemUrl.lastPathComponent | ||
try fileManager.copyItem( | ||
at: itemUrl, | ||
to: container.appendingPathComponent(filename, isDirectory: false)) | ||
|
||
let itemInfo = CachedItemInfo(fileName: filename, timestamp: Date().timeIntervalSince1970) | ||
let data = try encoder.encode(itemInfo) | ||
try data.write(to: try cachedItemInfoFileUrl(forItemWithName: name), options: .atomicWrite) | ||
} | ||
|
||
public func url(forItemWithName name: String) throws -> URL { | ||
let itemInfo = try cachedItemInfo(forItemWithName: name) | ||
let container = try containerUrl(forItemWithName: name) | ||
return container.appendingPathComponent(itemInfo.fileName, isDirectory: false) | ||
} | ||
|
||
// MARK: - Internals | ||
|
||
private struct CachedItemInfo: Codable { | ||
let fileName: String | ||
let timestamp: TimeInterval | ||
} | ||
|
||
private let encoder = JSONEncoder() | ||
private let decoder = JSONDecoder() | ||
|
||
private func containerUrl(forItemWithName name: String) throws -> URL { | ||
let key = try nameKeyer.key(forName: name) | ||
let containerUrl = cachesUrl.appendingPathComponent(key, isDirectory: true) | ||
if !fileManager.fileExists(atPath: containerUrl.path) { | ||
try fileManager.createDirectory(at: containerUrl, withIntermediateDirectories: true) | ||
} | ||
return containerUrl | ||
} | ||
|
||
private func cachedItemInfoFileUrl(forItemWithName name: String) throws -> URL { | ||
let key = try nameKeyer.key(forName: name) | ||
let container = try containerUrl(forItemWithName: name) | ||
return container.appendingPathComponent(key, isDirectory: false).appendingPathExtension("json") | ||
} | ||
|
||
private func cachedItemInfo(forItemWithName name: String) throws -> CachedItemInfo { | ||
let infoFileUrl = try cachedItemInfoFileUrl(forItemWithName: name) | ||
let data = try Data(contentsOf: infoFileUrl) | ||
return try decoder.decode(CachedItemInfo.self, from: data) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import Foundation | ||
|
||
public protocol NameKeyer { | ||
func key(forName name: String) throws -> String | ||
} |
Oops, something went wrong.