-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathFileAccess.swift
397 lines (358 loc) · 17.1 KB
/
FileAccess.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
import CommonCrypto
import Foundation
import ZIPFoundation
@objc(FileAccessImpl)
public class FileAccess : NSObject {
private let fetchCalls = NSMapTable<NSNumber, URLSessionDownloadTask>.init(keyOptions: .copyIn, valueOptions: .weakMemory)
@objc public func constantsToExport() -> [AnyHashable : Any] {
return [
"CacheDir": NSSearchPathForDirectoriesInDomains(.cachesDirectory, .userDomainMask, true).first!,
"DocumentDir": NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first!,
"LibraryDir": NSSearchPathForDirectoriesInDomains(.libraryDirectory, .userDomainMask, true).first!,
"MainBundleDir": Bundle.main.bundlePath
]
}
@objc public func supportedEvents() -> [String] {
return [NetworkHandler.FETCH_EVENT]
}
@objc(appendFile:withData:withEncoding:withResolver:withRejecter:)
public func appendFile(path: String, data: String, encoding: String, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) -> Void {
DispatchQueue.global().async {
guard let encodedData = encoding == "base64" ? Data(base64Encoded: data) : data.data(using: .utf8),
let handle = FileHandle(forWritingAtPath: path.path()) else {
reject("ERR", "Failed to append to '\(path)'.", nil)
return
}
handle.seekToEndOfFile()
handle.write(encodedData)
handle.closeFile()
resolve(nil)
}
}
@objc(cancelFetch:withResolver:withRejecter:)
public func cancelFetch(requestId: NSNumber, resolve: RCTPromiseResolveBlock, reject: RCTPromiseRejectBlock) -> Void {
if let call = fetchCalls.object(forKey: requestId) {
fetchCalls.removeObject(forKey: requestId)
call.cancel()
}
resolve(nil)
}
@objc(concatFiles:withTarget:withResolver:withRejecter:)
public func concatFiles(source: String, target: String, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) -> Void {
DispatchQueue.global().async {
guard let input = InputStream(fileAtPath: source.path()), let output = OutputStream(toFileAtPath: target.path(), append: true) else {
reject("ERR", "Failed to concat '\(source)' to '\(target)'.", nil)
return
}
input.open()
output.open()
var bytesCopied = 0
let bufferSize = 8 * 1024
var buffer = [UInt8](repeating: 0, count: bufferSize);
var bytes = input.read(&buffer, maxLength: bufferSize)
while bytes > 0 {
output.write(buffer, maxLength: bytes)
bytesCopied += bytes
bytes = input.read(&buffer, maxLength: bufferSize)
}
output.close()
input.close()
resolve(bytesCopied)
}
}
@objc(cp:withTarget:withResolver:withRejecter:)
public func cp(source: String, target: String, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) -> Void {
DispatchQueue.global().async {
do {
try FileManager.default.copyItem(atPath: source.path(), toPath: target.path())
resolve(nil)
} catch {
reject("ERR", "Failed to copy '\(source)' to '\(target)'. \(error.localizedDescription)", error)
}
}
}
@objc(cpAsset:withTarget:withResolver:withRejecter:)
public func cpAsset(asset: String, target: String, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) -> Void {
DispatchQueue.global().async {
guard let assetPath = Bundle.main.path(forResource: asset, ofType: nil) else {
reject("ENOENT", "Asset \(asset) not found", nil)
return
}
do {
try FileManager.default.copyItem(atPath: assetPath, toPath: target.path())
resolve(nil)
} catch {
reject("ERR", "Failed to copy '\(asset)' to '\(target)'. \(error.localizedDescription)", error)
}
}
}
@objc(cpExternal:withTargetName:withDir:withResolver:withRejecter:)
public func cpExternal(source: String, targetName: String, dir: String, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) -> Void {
let targetFolder: String
switch dir {
case "audio":
targetFolder = NSSearchPathForDirectoriesInDomains(.musicDirectory, .userDomainMask, true).first!
case "downloads":
targetFolder = NSSearchPathForDirectoriesInDomains(.downloadsDirectory, .userDomainMask, true).first!
case "images":
targetFolder = NSSearchPathForDirectoriesInDomains(.picturesDirectory, .userDomainMask, true).first!
case "video":
targetFolder = NSSearchPathForDirectoriesInDomains(.moviesDirectory, .userDomainMask, true).first!
default:
reject("ERR", "Unknown destination '\(dir)'.", nil)
return
}
try? FileManager.default.createDirectory(atPath: targetFolder, withIntermediateDirectories: true, attributes: nil)
let targetUrl = URL(fileURLWithPath: targetFolder, isDirectory: true)
.appendingPathComponent(targetName, isDirectory: false)
cp(source: source.path(), target: targetUrl.path, resolve: resolve, reject: reject)
}
@objc(df:withRejecter:)
public func df(resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) -> Void {
DispatchQueue.global().async {
#if NO_PRIVACY_API
reject("ERR", "Filesystem stat API disabled via compile time flag.", nil)
#else
do {
let stat = try FileManager.default.attributesOfFileSystem(
forPath: NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first!
)
resolve([
"internal_free": stat[.systemFreeSize],
"internal_total": stat[.systemSize]
])
} catch {
reject("ERR", "Failed to stat filesystem. \(error.localizedDescription)", error)
}
#endif
}
}
@objc(exists:withResolver:withRejecter:)
public func exists(path: String, resolve: @escaping RCTPromiseResolveBlock, reject: RCTPromiseRejectBlock) -> Void {
DispatchQueue.global().async {
resolve(FileManager.default.fileExists(atPath: path.path()))
}
}
@objc(fetch:withResource:withConfig:withEmitter:)
public func fetch(requestId: NSNumber, resource: String, config: NSDictionary, emitter: RCTEventEmitter) -> Void {
let handler = NetworkHandler(requestId: requestId, emitter: emitter) {
self.fetchCalls.removeObject(forKey: requestId)
}
if let call = handler.fetch(resource: resource, config: config) {
fetchCalls.setObject(call, forKey: requestId)
}
}
@objc(getAppGroupDir:withResolver:withRejecter:)
public func getAppGroupDir(groupName: String, resolve: RCTPromiseResolveBlock, reject: RCTPromiseRejectBlock) -> Void {
guard let groupURL = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: groupName) else {
reject("ERR", "Could not resolve app group directory. The group name '\(groupName)' is invalid.", nil)
return
}
resolve(groupURL.path)
}
@objc(hash:withAlgorithm:withResolver:withRejecter:)
public func hash(path: String, algorithm: String, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) -> Void {
DispatchQueue.global().async {
guard let data = NSData(contentsOfFile: path.path()) else {
reject("ERR", "Failed to read '\(path)'.", nil)
return
}
let hashAlgo: (UnsafeRawPointer?, CC_LONG, UnsafeMutablePointer<UInt8>?) -> UnsafeMutablePointer<UInt8>?
let digestLength: Int32
switch algorithm {
case "MD5":
hashAlgo = CC_MD5
digestLength = CC_MD5_DIGEST_LENGTH
case "SHA-1":
hashAlgo = CC_SHA1
digestLength = CC_SHA1_DIGEST_LENGTH
case "SHA-224":
hashAlgo = CC_SHA224
digestLength = CC_SHA224_DIGEST_LENGTH
case "SHA-256":
hashAlgo = CC_SHA256
digestLength = CC_SHA256_DIGEST_LENGTH
case "SHA-384":
hashAlgo = CC_SHA384
digestLength = CC_SHA384_DIGEST_LENGTH
case "SHA-512":
hashAlgo = CC_SHA512
digestLength = CC_SHA512_DIGEST_LENGTH
default:
reject("ERR", "Unknown algorithm '\(algorithm)'.", nil)
return
}
var digest = [UInt8](repeating: 0, count: Int(digestLength));
_ = hashAlgo(data.bytes, CC_LONG(data.length), &digest)
resolve(digest.map { String(format: "%02x", $0) }.joined())
}
}
@objc(isDir:withResolver:withRejecter:)
public func isDir(path: String, resolve: @escaping RCTPromiseResolveBlock, reject: RCTPromiseRejectBlock) -> Void {
DispatchQueue.global().async {
let status = self.checkIfIsDirectory(path: path.path())
resolve(status.exists && status.isDirectory)
}
}
@objc(ls:withResolver:withRejecter:)
public func ls(path: String, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) -> Void {
DispatchQueue.global().async {
do {
try resolve(FileManager.default.contentsOfDirectory(atPath: path.path()))
} catch {
reject("ERR", "Failed to list '\(path)'. \(error.localizedDescription)", error)
}
}
}
@objc(mkdir:withResolver:withRejecter:)
public func mkdir(path: String, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) -> Void {
DispatchQueue.global().async {
do {
try FileManager.default.createDirectory(atPath: path.path(), withIntermediateDirectories: true, attributes: nil)
resolve(path)
} catch {
reject("ERR", "Failed to create directory '\(path)'. \(error.localizedDescription)", error)
}
}
}
@objc(mv:withTarget:withResolver:withRejecter:)
public func mv(source: String, target: String, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) -> Void {
DispatchQueue.global().async {
do {
try? FileManager.default.removeItem(atPath: target.path())
try FileManager.default.moveItem(atPath: source.path(), toPath: target.path())
resolve(nil)
} catch {
reject("ERR", "Failed to rename '\(source)' to '\(target)'. \(error.localizedDescription)", error)
}
}
}
@objc(readFile:withEncoding:withResolver:withRejecter:)
public func readFile(path: String, encoding: String, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) -> Void {
DispatchQueue.global().async {
do {
if encoding == "base64" {
let binaryData = try Data(contentsOf: URL(fileURLWithPath: path.path()))
resolve(binaryData.base64EncodedString())
} else {
try resolve(String(contentsOfFile: path.path()))
}
} catch {
reject("ERR", "Failed to read '\(path)'. \(error.localizedDescription)", error)
}
}
}
@objc(readFileChunk:withOffset:withLength:withEncoding:withResolver:withRejecter:)
public func readFileChunk(path: String, offset: NSNumber, length: NSNumber, encoding: String, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) -> Void {
DispatchQueue.global().async {
do {
let fileHandle = try FileHandle(forReadingFrom: URL(fileURLWithPath: path.path()))
defer {
fileHandle.closeFile()
}
fileHandle.seek(toFileOffset: UInt64(truncating: offset))
let binaryData = fileHandle.readData(ofLength: Int(truncating: length))
if encoding == "base64" {
resolve(binaryData.base64EncodedString())
} else {
if let content = String(data: binaryData, encoding: .utf8) {
resolve(content)
} else {
reject("ERR", "Failed to decode content from file '\(path)' with specified encoding.", nil)
}
}
} catch {
reject("ERR", "Failed to read '\(path)'. \(error.localizedDescription)", error)
}
}
}
@objc(stat:withResolver:withRejecter:)
public func stat(path: String, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) -> Void {
DispatchQueue.global().async {
do {
resolve(try self.statFile(path: path))
} catch {
reject("ERR", "Failed to stat '\(path)'. \(error.localizedDescription)", error)
}
}
}
@objc(statDir:withResolver:withRejecter:)
public func statDir(path: String, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) -> Void {
DispatchQueue.global().async {
let base = URL(fileURLWithPath: path.path())
do {
try resolve(FileManager.default.contentsOfDirectory(atPath: path.path())
.compactMap { try? self.statFile(path: base.appendingPathComponent($0).path) }
)
} catch {
reject("ERR", "Failed to list '\(path)'. \(error.localizedDescription)", error)
}
}
}
@objc(unlink:withResolver:withRejecter:)
public func unlink(path: String, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) -> Void {
DispatchQueue.global().async {
do {
try FileManager.default.removeItem(atPath: path.path())
resolve(nil)
} catch {
reject("ERR", "Failed to unlink '\(path)'. \(error.localizedDescription)", error)
}
}
}
@objc(unzip:withTarget:withResolver:withRejecter:)
public func unzip(source: String, target: String, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) -> Void {
DispatchQueue.global().async {
let sourceUrl = URL(fileURLWithPath: source.path())
let targetUrl = URL(fileURLWithPath: target.path())
do {
try FileManager.default.unzipItem(at: sourceUrl, to: targetUrl)
resolve(nil)
} catch {
reject("ERR", "Failed to unzip '\(source)' to '\(target)'. \(error.localizedDescription)", error)
}
}
}
@objc(writeFile:withData:withEncoding:withResolver:withRejecter:)
public func writeFile(path: String, data: String, encoding: String, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) -> Void {
DispatchQueue.global().async {
do {
if encoding == "base64" {
let pathUrl = URL(fileURLWithPath: path.path())
guard let decoded = Data(base64Encoded: data, options: .ignoreUnknownCharacters) else {
reject("ERR", "Failed to write to '\(path)', invalid base64.", nil)
return
}
try decoded.write(to: pathUrl)
} else {
try data.write(toFile: path.path(), atomically: false, encoding: .utf8)
}
resolve(nil)
} catch {
reject("ERR", "Failed to write to '\(path)'. \(error.localizedDescription)", error)
}
}
}
private func checkIfIsDirectory(path: String) -> (exists: Bool, isDirectory: Bool) {
var isDir: ObjCBool = false
let exists = FileManager.default.fileExists(atPath: path.path(), isDirectory: &isDir)
let isDirectory = isDir.boolValue
return (exists, isDirectory)
}
private func statFile(path: String) throws -> [String : Any?] {
let pathUrl = URL(fileURLWithPath: path.path())
let attrs = try FileManager.default.attributesOfItem(atPath: path.path())
#if NO_PRIVACY_API
let lastModified = 0
#else
let lastModified = 1000 * (attrs[.modificationDate] as! NSDate).timeIntervalSince1970
#endif
return [
"filename": pathUrl.lastPathComponent,
"lastModified": lastModified,
"path": pathUrl.path,
"size": attrs[.size],
"type": self.checkIfIsDirectory(path: path).isDirectory ? "directory" : "file"
]
}
}