Add static method to create shares in ShareController
authorClaudio Cambra <claudio.cambra@nextcloud.com>
Tue, 5 Mar 2024 11:14:33 +0000 (19:14 +0800)
committerClaudio Cambra <claudio.cambra@nextcloud.com>
Wed, 17 Apr 2024 08:38:57 +0000 (16:38 +0800)
Signed-off-by: Claudio Cambra <claudio.cambra@nextcloud.com>
shell_integration/MacOSX/NextcloudIntegration/FileProviderUIExt/ShareController.swift

index 6eaa8ed23f71e8b6ab4f8698db5110cbf4d06381..a6f8d3def8750eb2de1a298d458953371c717a12 100644 (file)
@@ -14,6 +14,58 @@ class ShareController: ObservableObject {
     @Published private(set) var share: NKShare
     private let kit: NextcloudKit
 
+    static func create(
+        kit: NextcloudKit,
+        shareType: NKShare.ShareType,
+        itemServerRelativePath: String,
+        shareWith: String?,
+        password: String? = nil,
+        expireDate: String? = nil,
+        permissions: Int = 1,
+        publicUpload: Bool = false,
+        note: String? = nil,
+        label: String? = nil,
+        hideDownload: Bool,
+        attributes: String? = nil,
+        options: NKRequestOptions = NKRequestOptions()
+    ) async -> NKError? {
+        Logger.shareController.info("Creating share: \(itemServerRelativePath)")
+        return await withCheckedContinuation { continuation in
+            if shareType == .publicLink {
+                kit.createShareLink(
+                    path: itemServerRelativePath,
+                    publicUpload: publicUpload
+                ) { account, share, data, error in
+                    defer { continuation.resume(returning: error) }
+                    guard error == .success else {
+                        Logger.shareController.error("Error creating link share: \(error)")
+                        return
+                    }
+                }
+            } else {
+                guard let shareWith = shareWith else {
+                    let errorString = "No recipient for share!"
+                    Logger.shareController.error("\(errorString)")
+                    let error = NKError(statusCode: 0, fallbackDescription: errorString)
+                    continuation.resume(returning: error)
+                    return
+                }
+
+                kit.createShare(
+                    path: itemServerRelativePath,
+                    shareType: shareType.rawValue,
+                    shareWith: shareWith
+                ) { account, share, data, error in
+                    defer { continuation.resume(returning: error) }
+                    guard error == .success else {
+                        Logger.shareController.error("Error creating share: \(error)")
+                        return
+                    }
+                }
+            }
+        }
+    }
+
     init(share: NKShare, kit: NextcloudKit) {
         self.share = share
         self.kit = kit