c3b75ebeb2
Closes #1907
71 lines
2.4 KiB
Swift
71 lines
2.4 KiB
Swift
import ExpoModulesCore
|
|
import Foundation
|
|
|
|
public class NativeFileModule: Module {
|
|
public func definition() -> ModuleDefinition {
|
|
Name("NativeFile")
|
|
|
|
Function("writeFile") { (path: String, content: String) in
|
|
try content.write(toFile: path, atomically: true, encoding: .utf8)
|
|
}
|
|
|
|
Function("readFile") { (path: String) in
|
|
try String(contentsOfFile: path, encoding: .utf8)
|
|
}
|
|
|
|
Function("copyFile") { (sourcePath: String, destPath: String) in
|
|
try FileManager.default.copyItem(atPath: sourcePath, toPath: destPath)
|
|
}
|
|
|
|
Function("moveFile") { (sourcePath: String, destPath: String) in
|
|
try FileManager.default.moveItem(atPath: sourcePath, toPath: destPath)
|
|
}
|
|
|
|
Function("exists") { (filePath: String) in
|
|
FileManager.default.fileExists(atPath: filePath)
|
|
}
|
|
|
|
Function("mkdir") { (filePath: String) in
|
|
try FileManager.default.createDirectory(atPath: filePath, withIntermediateDirectories: true, attributes: nil)
|
|
}
|
|
|
|
Function("unlink") { (filePath: String) in
|
|
try FileManager.default.removeItem(atPath: filePath)
|
|
}
|
|
|
|
Function("readDir") { (dirPath: String) -> [[String: Any]] in
|
|
let contents = try FileManager.default.contentsOfDirectory(atPath: dirPath)
|
|
return contents.map { fileName in
|
|
let path = (dirPath as NSString).appendingPathComponent(fileName)
|
|
var isDirectory: ObjCBool = false
|
|
FileManager.default.fileExists(atPath: path, isDirectory: &isDirectory)
|
|
return [
|
|
"name": fileName,
|
|
"path": path,
|
|
"isDirectory": isDirectory.boolValue
|
|
]
|
|
}
|
|
}
|
|
|
|
AsyncFunction("downloadFile") { (url: String, destPath: String, method: String, headers: [String: String], body: String?, promise: Promise) in
|
|
// Stub — download implementation not ported for iOS
|
|
promise.reject("NOT_IMPLEMENTED", "downloadFile is not implemented on iOS")
|
|
}
|
|
|
|
Constant("DocumentDirectoryPath") {
|
|
let paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
|
|
return paths.first ?? ""
|
|
}
|
|
|
|
Constant("ExternalDirectoryPath") {
|
|
let paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
|
|
return paths.first ?? ""
|
|
}
|
|
|
|
Constant("ExternalCachesDirectoryPath") {
|
|
let paths = NSSearchPathForDirectoriesInDomains(.cachesDirectory, .userDomainMask, true)
|
|
return paths.first ?? ""
|
|
}
|
|
}
|
|
}
|