|
|
|
|
@ -19,23 +19,15 @@ class FileUtils { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
static func readDocumentFile(fileName: String) throws -> String { |
|
|
|
|
let fileURL: URL = try self.documentDirectoryURLForFileName(fileName) |
|
|
|
|
// Logger.log("url = \(fileURL.absoluteString)") |
|
|
|
|
let fileURL: URL = try self.pathForFileInDocumentDirectory(fileName) |
|
|
|
|
return try String(contentsOf: fileURL, encoding: .utf8) |
|
|
|
|
|
|
|
|
|
// if let dir: URL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first { |
|
|
|
|
// let fileURL: URL = dir.appendingPathComponent(fileName) |
|
|
|
|
// Logger.log("url = \(fileURL.absoluteString)") |
|
|
|
|
// return try String(contentsOf: fileURL, encoding: .utf8) |
|
|
|
|
// } |
|
|
|
|
// throw FileError.documentDirectoryNotFound |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
static func readFile(fileURL: URL) throws -> String { |
|
|
|
|
return try String(contentsOf: fileURL, encoding: .utf8) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
static func documentDirectoryURLForFileName(_ fileName: String) throws -> URL { |
|
|
|
|
static func pathForFileInDocumentDirectory(_ fileName: String) throws -> URL { |
|
|
|
|
if let dir: URL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first { |
|
|
|
|
return dir.appendingPathComponent(fileName) |
|
|
|
|
} |
|
|
|
|
@ -43,23 +35,34 @@ class FileUtils { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
static func writeToDocumentDirectory(content: String, fileName: String) throws -> URL { |
|
|
|
|
let fileURL = try self.pathForFileInDocumentDirectory(fileName) |
|
|
|
|
try content.write(to: fileURL, atomically: false, encoding: .utf8) |
|
|
|
|
return fileURL |
|
|
|
|
|
|
|
|
|
if let dir: URL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first { |
|
|
|
|
let fileURL: URL = dir.appendingPathComponent(fileName) |
|
|
|
|
try content.write(to: fileURL, atomically: false, encoding: .utf8) |
|
|
|
|
return fileURL |
|
|
|
|
} |
|
|
|
|
throw FileError.documentDirectoryNotFound |
|
|
|
|
// if let dir: URL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first { |
|
|
|
|
// let fileURL: URL = dir.appendingPathComponent(fileName) |
|
|
|
|
// try content.write(to: fileURL, atomically: false, encoding: .utf8) |
|
|
|
|
// return fileURL |
|
|
|
|
// } |
|
|
|
|
// throw FileError.documentDirectoryNotFound |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@discardableResult static func writeToDocumentDirectory(data: Data, fileName: String) throws -> URL { |
|
|
|
|
|
|
|
|
|
if let dir: URL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first { |
|
|
|
|
let fileURL: URL = dir.appendingPathComponent(fileName) |
|
|
|
|
try data.write(to: fileURL) |
|
|
|
|
return fileURL |
|
|
|
|
} |
|
|
|
|
throw FileError.documentDirectoryNotFound |
|
|
|
|
let fileURL = try self.pathForFileInDocumentDirectory(fileName) |
|
|
|
|
try data.write(to: fileURL) |
|
|
|
|
return fileURL |
|
|
|
|
|
|
|
|
|
// if let dir: URL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first { |
|
|
|
|
// let fileURL: URL = dir.appendingPathComponent(fileName) |
|
|
|
|
// try data.write(to: fileURL) |
|
|
|
|
// return fileURL |
|
|
|
|
// } |
|
|
|
|
// throw FileError.documentDirectoryNotFound |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
static func removeFileFromDocumentDirectory(fileName: String) throws { |
|
|
|
|
let fileURL = try self.pathForFileInDocumentDirectory(fileName) |
|
|
|
|
try FileManager.default.removeItem(atPath: fileURL.path()) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|