SPIO203

Untitled

Aug 27th, 2024
31
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 1.24 KB | None | 0 0
  1. import Foundation
  2. import Appwrite
  3.  
  4. class imageUploadNode: ObservableObject {
  5.     static let shared = imageUploadNode()
  6.  
  7.     private var client: Client
  8.     private var account: Account
  9.     private var storage: Storage
  10.  
  11.     private init() {
  12.         self.client = Client()
  13.             .setEndpoint("https://192.168.0.218:9443/v1")
  14.             .setProject("66cc6ce90006997fa5a0")
  15.             .setSelfSigned(true)
  16.         self.account = Account(client)
  17.         self.storage = Storage(client)
  18.     }
  19.  
  20.     func uploadImage(_ data: Data, userId: String) async -> Bool {
  21.         let filename = "\(UUID().uuidString).jpg"
  22.  
  23.         do {
  24.             let file = try await storage.createFile(
  25.                 bucketId: "66cc7031002044095926",
  26.                 fileId: "unique()",
  27.                 file: InputFile.fromData(data, filename: filename, mimeType: "image/jpeg"),
  28.                 permissions: [
  29.                     "user:\(userId).read",
  30.                     "user:\(userId).write"
  31.                 ]
  32.             )
  33.    
  34.             print("File uploaded successfully with ID: \(file.id)")
  35.             return true
  36.         } catch {
  37.             print("Upload error: \(error.localizedDescription)")
  38.             return false
  39.         }
  40.     }
  41. }
  42.  
Advertisement
Add Comment
Please, Sign In to add comment