Added convenience functions to create paths for NC files (and create directories...
authorClaudio Cambra <claudio.cambra@nextcloud.com>
Thu, 12 Jan 2023 16:50:49 +0000 (17:50 +0100)
committerClaudio Cambra <claudio.cambra@nextcloud.com>
Fri, 12 May 2023 05:21:11 +0000 (13:21 +0800)
Signed-off-by: Claudio Cambra <claudio.cambra@nextcloud.com>
shell_integration/MacOSX/NextcloudIntegration/FileProviderExt/NextcloudFileProviderFilesUtils.swift

index 01fcf1d4f8fcdf787ce68b297d90a608eb7db65e..91b8062eaf776c70faba919af310aa5ef5f6a058 100644 (file)
@@ -27,3 +27,51 @@ func pathForFileProviderExtData() -> URL? {
     let containerUrl = pathForAppGroupContainer()
     return containerUrl?.appendingPathExtension("FileProviderExt/")
 }
+
+func pathForFileProviderExtFiles() -> URL? {
+    let fileProviderDataUrl = pathForFileProviderExtData()
+    return fileProviderDataUrl?.appendingPathExtension("Files/")
+}
+
+@discardableResult func localPathForNCDirectory(ocId: String) throws -> URL {
+    guard let fileProviderFilesPathUrl = pathForFileProviderExtFiles() else {
+        throw URLError(.badURL)
+    }
+
+    let folderPathUrl = fileProviderFilesPathUrl.appendingPathExtension(ocId)
+    let folderPath = folderPathUrl.path
+
+    if !FileManager.default.fileExists(atPath: folderPath) {
+        try FileManager.default.createDirectory(at: folderPathUrl, withIntermediateDirectories: true)
+    }
+
+    return folderPathUrl
+}
+
+@discardableResult func localPathForNCDirectory(directoryMetadata: NextcloudDirectoryMetadataTable) throws -> URL {
+    let ocId = directoryMetadata.ocId
+    return try localPathForNCDirectory(ocId: ocId)
+}
+
+@discardableResult func localPathForNCDirectory(fileMetadata: NextcloudFileMetadataTable) throws -> URL {
+    let ocId = fileMetadata.ocId
+    return try localPathForNCDirectory(ocId: ocId)
+}
+
+@discardableResult func localPathForNCFile(ocId: String, fileNameView: String) throws -> URL {
+    let fileFolderPathUrl = try localPathForNCDirectory(ocId: ocId)
+    let filePathUrl = fileFolderPathUrl.appendingPathExtension(fileNameView)
+    let filePath = filePathUrl.path
+
+    if !FileManager.default.fileExists(atPath: filePath) {
+        FileManager.default.createFile(atPath: filePath, contents: nil)
+    }
+
+    return filePathUrl
+}
+
+@discardableResult func localPathForNCFile(fileMetadata: NextcloudFileMetadataTable) throws -> URL {
+    let ocId = fileMetadata.ocId
+    let fileNameView = fileMetadata.fileNameView
+    return try localPathForNCFile(ocId: ocId, fileNameView: fileNameView)
+}