Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import Foundation
- import Appwrite
- class imageUploadNode: ObservableObject {
- static let shared = imageUploadNode()
- private var client: Client
- private var account: Account
- private var storage: Storage
- private init() {
- self.client = Client()
- .setEndpoint("https://192.168.0.218:9443/v1")
- .setProject("66cc6ce90006997fa5a0")
- .setSelfSigned(true)
- self.account = Account(client)
- self.storage = Storage(client)
- }
- func uploadImage(_ data: Data, userId: String) async -> Bool {
- let filename = "\(UUID().uuidString).jpg"
- do {
- let file = try await storage.createFile(
- bucketId: "66cc7031002044095926",
- fileId: "unique()",
- file: InputFile.fromData(data, filename: filename, mimeType: "image/jpeg"),
- permissions: [
- "user:\(userId).read",
- "user:\(userId).write"
- ]
- )
- print("File uploaded successfully with ID: \(file.id)")
- return true
- } catch {
- print("Upload error: \(error.localizedDescription)")
- return false
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment