// // Attachments.swift // QuickDrop // // Created by Leon Böttger on 17.08.25. // import Foundation import UniformTypeIdentifiers import SwiftUI #if os(macOS) import Cocoa #endif func loadAttachments(with extensionContext: NSExtensionContext?, loadedItems: @escaping (AttachmentDetails) -> Void) { guard let extensionContext = extensionContext else { fatalError() } log("Loading attachments...") var result = AttachmentDetails(urls: [], textToSend: nil, shortDescription: "", previewImage: nil) var ignoredAttachments = 0 let item = extensionContext.inputItems[0] as! NSExtensionItem if let attachments = item.attachments { if !attachments.isEmpty { for attachment in attachments { if attachment.hasItemConformingToTypeIdentifier(UTType.url.identifier) { attachment.loadItem(forTypeIdentifier: UTType.url.identifier, options: nil) { data, _ in if let urlData = data as? Data, let url = URL(dataRepresentation: urlData, relativeTo: nil, isAbsolute: false) { result.urls += [(url as URL)] } else if let url = data as? NSURL { result.urls += [(url as URL)] } else { log("Attachment is not a URL, ignoring. All attachments: \(attachments.description)") ignoredAttachments += 1 } checkIfAttachmentsLoaded(result: result, attachmentCount: attachments.count, ignoredAttachments: ignoredAttachments) } } else { attachment.loadItem(forTypeIdentifier: attachment.registeredTypeIdentifiers.first!, options: nil) { data, _ in if let url = data as? NSURL { log("Found NSURL: \(url)") result.urls += [(url as URL)] checkIfAttachmentsLoaded(result: result, attachmentCount: attachments.count, ignoredAttachments: ignoredAttachments) } else { log("Attachment is not a URL, ignoring. All attachments: \(attachments.description)") ignoredAttachments += 1 checkIfAttachmentsLoaded(result: result, attachmentCount: attachments.count, ignoredAttachments: ignoredAttachments) } } } } } else { loadTextIfPossible() } } else { log("No attachments found in extension context. Exiting.") let cancelError = NSError(domain: NSCocoaErrorDomain, code: NSUserCancelledError, userInfo: nil) extensionContext.cancelRequest(withError: cancelError) } func loadTextIfPossible() { if let text = item.attributedContentText?.string { log("Found text content: \(text)") result.textToSend = text #if os(macOS) result.previewImage = NSImage(named: NSImage.multipleDocumentsName) #else result.previewImage = Image(systemName: "text.document") #endif let maxLength = 50 let cleanedText = text.replacingOccurrences(of: "\n", with: " ").replacingOccurrences(of: "\r", with: "") if cleanedText.count > maxLength { let index = cleanedText.index(cleanedText.startIndex, offsetBy: maxLength) result.shortDescription = String(cleanedText[..