Skip to content

Commit 95b050a

Browse files
committed
fix(ios): support URI based paths to match Android
1 parent 72d7d1a commit 95b050a

File tree

4 files changed

+33
-25
lines changed

4 files changed

+33
-25
lines changed

example/src/App.tsx

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -155,10 +155,7 @@ export function App() {
155155
})
156156
);
157157

158-
const sourcFile =
159-
Platform.OS === 'ios' || Platform.OS === 'macos'
160-
? `${Dirs.CacheDir}/renamed.txt`
161-
: `file://${Dirs.CacheDir}/renamed.txt`;
158+
const sourcFile = `file://${Dirs.CacheDir}/renamed.txt`;
162159

163160
FileSystem.unlink(Dirs.CacheDir + '/3.txt')
164161
.then(() => console.log('Deleted 3.txt'))

ios/FileAccess.swift

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class FileAccess: RCTEventEmitter {
2222
@objc(appendFile:withData:withEncoding:withResolver:withRejecter:)
2323
func appendFile(path: String, data: String, encoding: String, resolve: RCTPromiseResolveBlock, reject: RCTPromiseRejectBlock) -> Void {
2424
guard let encodedData = encoding == "base64" ? Data(base64Encoded: data) : data.data(using: .utf8),
25-
let handle = FileHandle(forWritingAtPath: path) else {
25+
let handle = FileHandle(forWritingAtPath: path.path()) else {
2626
reject("ERR", "Failed to append to '\(path)'.", nil)
2727
return
2828
}
@@ -35,7 +35,7 @@ class FileAccess: RCTEventEmitter {
3535

3636
@objc(concatFiles:withTarget:withResolver:withRejecter:)
3737
func concatFiles(source: String, target: String, resolve: RCTPromiseResolveBlock, reject: RCTPromiseRejectBlock) -> Void {
38-
guard let input = InputStream(fileAtPath: source), let output = OutputStream(toFileAtPath: target, append: true) else {
38+
guard let input = InputStream(fileAtPath: source.path()), let output = OutputStream(toFileAtPath: target.path(), append: true) else {
3939
reject("ERR", "Failed to concat '\(source)' to '\(target)'.", nil)
4040
return
4141
}
@@ -60,7 +60,7 @@ class FileAccess: RCTEventEmitter {
6060
@objc(cp:withTarget:withResolver:withRejecter:)
6161
func cp(source: String, target: String, resolve: RCTPromiseResolveBlock, reject: RCTPromiseRejectBlock) -> Void {
6262
do {
63-
try FileManager.default.copyItem(atPath: source, toPath: target)
63+
try FileManager.default.copyItem(atPath: source.path(), toPath: target.path())
6464
resolve(nil)
6565
} catch {
6666
reject("ERR", "Failed to copy '\(source)' to '\(target)'.", error)
@@ -75,7 +75,7 @@ class FileAccess: RCTEventEmitter {
7575
}
7676

7777
do {
78-
try FileManager.default.copyItem(atPath: assetPath, toPath: target)
78+
try FileManager.default.copyItem(atPath: assetPath, toPath: target.path())
7979
resolve(nil)
8080
} catch {
8181
reject("ERR", "Failed to copy '\(asset)' to '\(target)'.", error)
@@ -103,7 +103,7 @@ class FileAccess: RCTEventEmitter {
103103

104104
let targetUrl = URL(fileURLWithPath: targetFolder, isDirectory: true)
105105
.appendingPathComponent(targetName, isDirectory: false)
106-
cp(source: source, target: targetUrl.path, resolve: resolve, reject: reject)
106+
cp(source: source.path(), target: targetUrl.path, resolve: resolve, reject: reject)
107107
}
108108

109109
@objc(df:withRejecter:)
@@ -124,7 +124,7 @@ class FileAccess: RCTEventEmitter {
124124

125125
@objc(exists:withResolver:withRejecter:)
126126
func exists(path: String, resolve: RCTPromiseResolveBlock, reject: RCTPromiseRejectBlock) -> Void {
127-
resolve(FileManager.default.fileExists(atPath: path))
127+
resolve(FileManager.default.fileExists(atPath: path.path()))
128128
}
129129

130130
@objc(fetch:withResource:withConfig:)
@@ -145,7 +145,7 @@ class FileAccess: RCTEventEmitter {
145145

146146
@objc(hash:withAlgorithm:withResolver:withRejecter:)
147147
func hash(path: String, algorithm: String, resolve: RCTPromiseResolveBlock, reject: RCTPromiseRejectBlock) -> Void {
148-
guard let data = NSData(contentsOfFile: path) else {
148+
guard let data = NSData(contentsOfFile: path.path()) else {
149149
reject("ERR", "Failed to read '\(path)'.", nil)
150150
return
151151
}
@@ -183,14 +183,14 @@ class FileAccess: RCTEventEmitter {
183183

184184
@objc(isDir:withResolver:withRejecter:)
185185
func isDir(path: String, resolve: RCTPromiseResolveBlock, reject: RCTPromiseRejectBlock) -> Void {
186-
let status = checkIfIsDirectory(path: path)
186+
let status = checkIfIsDirectory(path: path.path())
187187
resolve(status.exists && status.isDirectory)
188188
}
189189

190190
@objc(ls:withResolver:withRejecter:)
191191
func ls(path: String, resolve: RCTPromiseResolveBlock, reject: RCTPromiseRejectBlock) -> Void {
192192
do {
193-
try resolve(FileManager.default.contentsOfDirectory(atPath: path))
193+
try resolve(FileManager.default.contentsOfDirectory(atPath: path.path()))
194194
} catch {
195195
reject("ERR", "Failed to list '\(path)'.", error)
196196
}
@@ -199,7 +199,7 @@ class FileAccess: RCTEventEmitter {
199199
@objc(mkdir:withResolver:withRejecter:)
200200
func mkdir(path: String, resolve: RCTPromiseResolveBlock, reject: RCTPromiseRejectBlock) -> Void {
201201
do {
202-
try FileManager.default.createDirectory(atPath: path, withIntermediateDirectories: true, attributes: nil)
202+
try FileManager.default.createDirectory(atPath: path.path(), withIntermediateDirectories: true, attributes: nil)
203203
resolve(nil)
204204
} catch {
205205
reject("ERR", "Failed to create directory '\(path)'.", error)
@@ -209,8 +209,8 @@ class FileAccess: RCTEventEmitter {
209209
@objc(mv:withTarget:withResolver:withRejecter:)
210210
func mv(source: String, target: String, resolve: RCTPromiseResolveBlock, reject: RCTPromiseRejectBlock) -> Void {
211211
do {
212-
try? FileManager.default.removeItem(atPath: target)
213-
try FileManager.default.moveItem(atPath: source, toPath: target)
212+
try? FileManager.default.removeItem(atPath: target.path())
213+
try FileManager.default.moveItem(atPath: source.path(), toPath: target.path())
214214
resolve(nil)
215215
} catch {
216216
reject("ERR", "Failed to rename '\(source)' to '\(target)'.", error)
@@ -221,10 +221,10 @@ class FileAccess: RCTEventEmitter {
221221
func readFile(path: String, encoding: String, resolve: RCTPromiseResolveBlock, reject: RCTPromiseRejectBlock) -> Void {
222222
do {
223223
if encoding == "base64" {
224-
let binaryData = try Data(contentsOf: URL(fileURLWithPath: path))
224+
let binaryData = try Data(contentsOf: URL(fileURLWithPath: path.path()))
225225
resolve(binaryData.base64EncodedString())
226226
} else {
227-
try resolve(String(contentsOfFile: path))
227+
try resolve(String(contentsOfFile: path.path()))
228228
}
229229
} catch {
230230
reject("ERR", "Failed to read '\(path)'.", error)
@@ -234,8 +234,8 @@ class FileAccess: RCTEventEmitter {
234234
@objc(stat:withResolver:withRejecter:)
235235
func stat(path: String, resolve: RCTPromiseResolveBlock, reject: RCTPromiseRejectBlock) -> Void {
236236
do {
237-
let pathUrl = URL(fileURLWithPath: path)
238-
let attrs = try FileManager.default.attributesOfItem(atPath: path)
237+
let pathUrl = URL(fileURLWithPath: path.path())
238+
let attrs = try FileManager.default.attributesOfItem(atPath: path.path())
239239
resolve([
240240
"filename": pathUrl.lastPathComponent,
241241
"lastModified": 1000 * (attrs[.modificationDate] as! NSDate).timeIntervalSince1970,
@@ -251,7 +251,7 @@ class FileAccess: RCTEventEmitter {
251251
@objc(unlink:withResolver:withRejecter:)
252252
func unlink(path: String, resolve: RCTPromiseResolveBlock, reject: RCTPromiseRejectBlock) -> Void {
253253
do {
254-
try FileManager.default.removeItem(atPath: path)
254+
try FileManager.default.removeItem(atPath: path.path())
255255
resolve(nil)
256256
} catch {
257257
reject("ERR", "Failed to unlink '\(path)'.", error)
@@ -262,10 +262,10 @@ class FileAccess: RCTEventEmitter {
262262
func writeFile(path: String, data: String, encoding: String, resolve: RCTPromiseResolveBlock, reject: RCTPromiseRejectBlock) -> Void {
263263
do {
264264
if encoding == "base64" {
265-
let pathUrl = URL(fileURLWithPath: path)
265+
let pathUrl = URL(fileURLWithPath: path.path())
266266
try Data(base64Encoded: data)!.write(to: pathUrl)
267267
} else {
268-
try data.write(toFile: path, atomically: false, encoding: .utf8)
268+
try data.write(toFile: path.path(), atomically: false, encoding: .utf8)
269269
}
270270
resolve(nil)
271271
} catch {
@@ -275,7 +275,7 @@ class FileAccess: RCTEventEmitter {
275275

276276
private func checkIfIsDirectory(path: String) -> (exists: Bool, isDirectory: Bool) {
277277
var isDir: ObjCBool = false
278-
let exists = FileManager.default.fileExists(atPath: path, isDirectory: &isDir)
278+
let exists = FileManager.default.fileExists(atPath: path.path(), isDirectory: &isDir)
279279
let isDirectory = isDir.boolValue
280280
return (exists, isDirectory)
281281
}

ios/NetworkHandler.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class NetworkHandler: NSObject, URLSessionDownloadDelegate {
2020
}
2121

2222
currentUrl = resource
23-
destination = config["path"] as? String
23+
destination = (config["path"] as? String)?.path()
2424

2525
var request = URLRequest(url: url)
2626
if let method = config["method"] as? String {

ios/Util.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
extension String {
2+
/**
3+
* Normalize standard file system paths and file URIs.
4+
*/
5+
func path() -> String {
6+
if self.contains("://"), let pathUri = URL(string: self) {
7+
return pathUri.path
8+
}
9+
return self
10+
}
11+
}

0 commit comments

Comments
 (0)