mirror of
https://github.com/leonboe1/QuickDrop.git
synced 2026-11-22 06:15:59 +00:00
Fix attachment loading for macOS app
This commit is contained in:
1 parent
120b235709
commit
e808b71446
1 file changed
+59
-41
@@ -16,7 +16,6 @@ func loadAttachments(with extensionContext: NSExtensionContext?, loadedItems: @e
|
||||
|
||||
guard let extensionContext = extensionContext else { fatalError() }
|
||||
|
||||
|
||||
log("Loading attachments...")
|
||||
|
||||
var result = AttachmentDetails(urls: [], textToSend: nil, shortDescription: "", previewImage: nil)
|
||||
@@ -48,58 +47,34 @@ func loadAttachments(with extensionContext: NSExtensionContext?, loadedItems: @e
|
||||
} else {
|
||||
for attachment in attachments {
|
||||
|
||||
attachment.loadItem(forTypeIdentifier: attachment.registeredTypeIdentifiers.first!, options: nil) { data, _ in
|
||||
|
||||
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) {
|
||||
|
||||
log("Found URL from Data: \(url)")
|
||||
result.urls += [url]
|
||||
result.urls += [(url as URL)]
|
||||
} else if let url = data as? NSURL {
|
||||
result.urls += [(url as URL)]
|
||||
}
|
||||
else if let url = data as? NSURL {
|
||||
|
||||
checkIfAttachmentsLoaded(result: result, attachments: attachments)
|
||||
}
|
||||
}
|
||||
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, attachments: attachments)
|
||||
}
|
||||
else {
|
||||
log("Attachment is not a URL, ignoring.")
|
||||
let cancelError = NSError(domain: NSCocoaErrorDomain, code: NSUserCancelledError, userInfo: nil)
|
||||
extensionContext.cancelRequest(withError: cancelError)
|
||||
}
|
||||
|
||||
if result.urls.count == attachments.count {
|
||||
if result.urls.count == 1 {
|
||||
|
||||
if result.urls[0].isFileURL {
|
||||
result.shortDescription = result.urls[0].lastPathComponent
|
||||
#if os(macOS)
|
||||
result.previewImage = NSWorkspace.shared.icon(forFile: result.urls[0].path)
|
||||
#else
|
||||
result.previewImage = Image(systemName: "document")
|
||||
#endif
|
||||
}
|
||||
else if result.urls[0].scheme == "http" || result.urls[0].scheme == "https" {
|
||||
result.shortDescription = result.urls[0].absoluteString
|
||||
#if os(macOS)
|
||||
result.previewImage = NSImage(named: NSImage.networkName)
|
||||
#else
|
||||
result.previewImage = Image(systemName: "network")
|
||||
#endif
|
||||
}
|
||||
}
|
||||
else {
|
||||
result.shortDescription = String.localizedStringWithFormat("NFiles".localized(), result.urls.count)
|
||||
#if os(macOS)
|
||||
result.previewImage = NSImage(named: NSImage.multipleDocumentsName)
|
||||
#else
|
||||
result.previewImage = Image(systemName: "document")
|
||||
#endif
|
||||
}
|
||||
|
||||
DispatchQueue.main.async {
|
||||
loadedItems(result)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -109,4 +84,47 @@ func loadAttachments(with extensionContext: NSExtensionContext?, loadedItems: @e
|
||||
let cancelError = NSError(domain: NSCocoaErrorDomain, code: NSUserCancelledError, userInfo: nil)
|
||||
extensionContext.cancelRequest(withError: cancelError)
|
||||
}
|
||||
|
||||
|
||||
func checkIfAttachmentsLoaded(result: AttachmentDetails, attachments: [NSItemProvider]) {
|
||||
|
||||
var result = result
|
||||
|
||||
if result.urls.count == attachments.count {
|
||||
if result.urls.count == 1 {
|
||||
|
||||
if result.urls[0].isFileURL {
|
||||
|
||||
log("Single file URL found: \(result.urls[0])")
|
||||
|
||||
result.shortDescription = result.urls[0].lastPathComponent
|
||||
#if os(macOS)
|
||||
result.previewImage = NSWorkspace.shared.icon(forFile: result.urls[0].path)
|
||||
#else
|
||||
result.previewImage = Image(systemName: "document")
|
||||
#endif
|
||||
}
|
||||
else if result.urls[0].scheme == "http" || result.urls[0].scheme == "https" {
|
||||
result.shortDescription = result.urls[0].absoluteString
|
||||
#if os(macOS)
|
||||
result.previewImage = NSImage(named: NSImage.networkName)
|
||||
#else
|
||||
result.previewImage = Image(systemName: "network")
|
||||
#endif
|
||||
}
|
||||
}
|
||||
else {
|
||||
result.shortDescription = String.localizedStringWithFormat("NFiles".localized(), result.urls.count)
|
||||
#if os(macOS)
|
||||
result.previewImage = NSImage(named: NSImage.multipleDocumentsName)
|
||||
#else
|
||||
result.previewImage = Image(systemName: "document")
|
||||
#endif
|
||||
}
|
||||
|
||||
DispatchQueue.main.async {
|
||||
loadedItems(result)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user