aboutsummaryrefslogtreecommitdiff
path: root/iosApp/iosApp/Shared/Utils.swift
diff options
context:
space:
mode:
Diffstat (limited to 'iosApp/iosApp/Shared/Utils.swift')
-rw-r--r--iosApp/iosApp/Shared/Utils.swift52
1 files changed, 52 insertions, 0 deletions
diff --git a/iosApp/iosApp/Shared/Utils.swift b/iosApp/iosApp/Shared/Utils.swift
index 61a8f90..4e04d1b 100644
--- a/iosApp/iosApp/Shared/Utils.swift
+++ b/iosApp/iosApp/Shared/Utils.swift
@@ -20,6 +20,25 @@ import CryptoKit
import WhirlyGlobe
import shared
+// Source: https://stackoverflow.com/a/55092044
+extension Data {
+ func dataToFile(fileName: String) -> NSURL? {
+ let fileManager = FileManager.default
+ let data = self
+ let filePath = Utils.getReportTmpDirectory().appendingPathComponent(fileName)
+ do {
+ if(fileManager.fileExists(atPath: filePath)) {
+ try fileManager.removeItem(atPath: filePath)
+ }
+ fileManager.createFile(atPath: filePath, contents: data, attributes: nil)
+ return NSURL(fileURLWithPath: filePath)
+ } catch {
+ print("Error writing the file: \(error.localizedDescription)")
+ }
+ return nil
+ }
+}
+
class Utils {
static func MD5(string: String) -> String {
let digest = Insecure.MD5.hash(data: string.data(using: .utf8) ?? Data())
@@ -45,4 +64,37 @@ class Utils {
.replacingOccurrences(of: "{x}", with: "\(latitude)")
.replacingOccurrences(of: "{y}", with: "\(longitude)"))
}
+
+ static func createReportTmpDirectory() {
+ let fileManager = FileManager.default
+ let tmpDir = getReportTmpDirectory() as String
+ do {
+ if (!fileManager.fileExists(atPath: tmpDir)) {
+ try fileManager.createDirectory(atPath: tmpDir,
+ withIntermediateDirectories: false,
+ attributes: nil)
+ }
+ } catch {
+ print("Could not create temp folder: \(error)")
+ }
+ }
+
+ static func getReportTmpDirectory() -> NSString {
+ let tmpDir = NSTemporaryDirectory()
+ return (tmpDir as NSString).appendingPathComponent("reports") as NSString
+ }
+
+ // Source: https://stackoverflow.com/a/33937110
+ static func clearReportTmpDirectory() {
+ let fileManager = FileManager.default
+ let reportTmpDir = getReportTmpDirectory()
+ do {
+ let filePaths = try fileManager.contentsOfDirectory(atPath: reportTmpDir as String)
+ for filePath in filePaths {
+ try fileManager.removeItem(atPath: reportTmpDir.appendingPathComponent(filePath))
+ }
+ } catch {
+ print("Could not clear temp folder: \(error)")
+ }
+ }
}