-
-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathStorage.swift
139 lines (119 loc) · 4.05 KB
/
Storage.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
//
// Storage.swift
// EasyStash-iOS
//
// Created by khoa on 27/05/2019.
// Copyright © 2019 Khoa Pham. All rights reserved.
//
import Foundation
public enum StorageError: Error {
case notFound
case encodeData
case decodeData
case createFile
case missingFileAttributeKey(key: FileAttributeKey)
case expired(maxAge: Double)
}
public class Storage {
public let cache = NSCache<NSString, AnyObject>()
public let options: Options
public let folderUrl: URL
public let fileManager: FileManager = .default
public init(options: Options) throws {
self.options = options
var url: URL
if let directoryUrl = options.directoryUrl {
url = directoryUrl
} else {
url = try fileManager.url(
for: options.searchPathDirectory,
in: .userDomainMask,
appropriateFor: nil,
create: true
)
}
self.folderUrl = url
.appendingPathComponent(options.folder, isDirectory: true)
try createDirectoryIfNeeded(folderUrl: folderUrl)
try applyAttributesIfAny(folderUrl: folderUrl)
}
public func exists(forKey key: String) -> Bool {
return fileManager.fileExists(atPath: fileUrl(forKey: key).path)
}
public func removeAll() throws {
cache.removeAllObjects()
try fileManager.removeItem(at: folderUrl)
try createDirectoryIfNeeded(folderUrl: folderUrl)
}
public func remove(forKey key: String) throws {
cache.removeObject(forKey: key as NSString)
try fileManager.removeItem(at: fileUrl(forKey: key))
}
public func fileUrl(forKey key: String) -> URL {
return folderUrl.appendingPathComponent(key, isDirectory: false)
}
}
extension Storage {
func createDirectoryIfNeeded(folderUrl: URL) throws {
var isDirectory = ObjCBool(true)
guard !fileManager.fileExists(atPath: folderUrl.path, isDirectory: &isDirectory) else {
return
}
try fileManager.createDirectory(
atPath: folderUrl.path,
withIntermediateDirectories: true,
attributes: nil
)
}
func applyAttributesIfAny(folderUrl: URL) throws {
#if os(iOS) || os(tvOS)
let attributes: [FileAttributeKey: Any] = [
FileAttributeKey.protectionKey: FileProtectionType.complete
]
try fileManager.setAttributes(attributes, ofItemAtPath: folderUrl.path)
#endif
}
func verify(
maxAge: TimeInterval,
forKey key: String,
fromDate date: @escaping (() -> Date) = { Date() }
) throws -> Bool {
date().timeIntervalSince(try modificationDate(forKey: key)) <= maxAge
}
}
extension Storage {
func commonSave(object: AnyObject, forKey key: String, toData: () throws -> Data) throws {
let data = try toData()
cache.setObject(object, forKey: key as NSString)
try fileManager
.createFile(atPath: fileUrl(forKey: key).path, contents: data, attributes: nil)
.trueOrThrow(StorageError.createFile)
}
func commonLoad<T>(forKey key: String,
withExpiry expiry: Expiry,
fromDate date: @escaping (() -> Date) = { Date() },
fromData: (Data) throws -> T) throws -> T {
switch expiry {
case .never:
break
case .maxAge(let maxAge):
guard try verify(maxAge: maxAge, forKey: key, fromDate: date) else {
throw StorageError.expired(maxAge: maxAge)
}
}
if let object = cache.object(forKey: key as NSString) as? T {
return object
} else {
let data = try Data(contentsOf: fileUrl(forKey: key))
let object = try fromData(data)
cache.setObject(object as AnyObject, forKey: key as NSString)
return object
}
}
}
extension Storage {
public enum Expiry {
case never
case maxAge(maxAge: TimeInterval)
}
}