mirror of
https://github.com/leonboe1/QuickDrop.git
synced 2026-11-22 14:26:01 +00:00
Fix cannot share screenshots
This commit is contained in:
1 parent
4700a9c56e
commit
ea213906d6
3 files changed
+104
-17
No files matched your search
@@ -1774,7 +1774,7 @@
|
|||||||
ONLY_ACTIVE_ARCH = YES;
|
ONLY_ACTIVE_ARCH = YES;
|
||||||
SDKROOT = macosx;
|
SDKROOT = macosx;
|
||||||
SHARED_BUILD = 137;
|
SHARED_BUILD = 137;
|
||||||
SHARED_VERSION = 2.14;
|
SHARED_VERSION = 2.14.1;
|
||||||
STRING_CATALOG_GENERATE_SYMBOLS = YES;
|
STRING_CATALOG_GENERATE_SYMBOLS = YES;
|
||||||
SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG;
|
SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG;
|
||||||
SWIFT_EMIT_LOC_STRINGS = YES;
|
SWIFT_EMIT_LOC_STRINGS = YES;
|
||||||
@@ -1837,7 +1837,7 @@
|
|||||||
MTL_FAST_MATH = YES;
|
MTL_FAST_MATH = YES;
|
||||||
SDKROOT = macosx;
|
SDKROOT = macosx;
|
||||||
SHARED_BUILD = 137;
|
SHARED_BUILD = 137;
|
||||||
SHARED_VERSION = 2.14;
|
SHARED_VERSION = 2.14.1;
|
||||||
STRING_CATALOG_GENERATE_SYMBOLS = YES;
|
STRING_CATALOG_GENERATE_SYMBOLS = YES;
|
||||||
SWIFT_COMPILATION_MODE = wholemodule;
|
SWIFT_COMPILATION_MODE = wholemodule;
|
||||||
SWIFT_EMIT_LOC_STRINGS = YES;
|
SWIFT_EMIT_LOC_STRINGS = YES;
|
||||||
|
|||||||
@@ -124,7 +124,7 @@
|
|||||||
"location" : "https://github.com/siteline/swiftui-introspect",
|
"location" : "https://github.com/siteline/swiftui-introspect",
|
||||||
"state" : {
|
"state" : {
|
||||||
"branch" : "main",
|
"branch" : "main",
|
||||||
"revision" : "fe758545cf980ab1085779d4a321a50d33b00f61"
|
"revision" : "26986a57e31c813f98262ddb62d4b07d40008535"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -11,6 +11,8 @@ import SwiftUI
|
|||||||
import LUI
|
import LUI
|
||||||
#if os(macOS)
|
#if os(macOS)
|
||||||
import Cocoa
|
import Cocoa
|
||||||
|
#elseif canImport(UIKit)
|
||||||
|
import UIKit
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
func loadAttachments(with extensionContext: NSExtensionContext?, loadedItems: @escaping (AttachmentDetails) -> Void) {
|
func loadAttachments(with extensionContext: NSExtensionContext?, loadedItems: @escaping (AttachmentDetails) -> Void) {
|
||||||
@@ -42,7 +44,31 @@ func loadAttachments(with extensionContext: NSExtensionContext?, loadedItems: @e
|
|||||||
ignoredAttachments += 1
|
ignoredAttachments += 1
|
||||||
}
|
}
|
||||||
|
|
||||||
checkIfAttachmentsLoaded(result: result, attachmentCount: attachments.count, ignoredAttachments: ignoredAttachments)
|
checkIfAttachmentsLoaded(result: result,
|
||||||
|
attachmentCount: attachments.count,
|
||||||
|
ignoredAttachments: ignoredAttachments,
|
||||||
|
item: item,
|
||||||
|
loadedItems: loadedItems)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if attachment.hasItemConformingToTypeIdentifier(UTType.image.identifier) {
|
||||||
|
let imageTypeIdentifier = attachment.registeredTypeIdentifiers.first(where: {
|
||||||
|
UTType($0)?.conforms(to: .image) == true
|
||||||
|
}) ?? UTType.image.identifier
|
||||||
|
attachment.loadItem(forTypeIdentifier: imageTypeIdentifier, options: nil) { data, _ in
|
||||||
|
if let url = imageUrl(from: data, typeIdentifier: imageTypeIdentifier) {
|
||||||
|
result.urls += [url]
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
log("Attachment is not an image URL or data, ignoring. All attachments: \(attachments.description)")
|
||||||
|
ignoredAttachments += 1
|
||||||
|
}
|
||||||
|
|
||||||
|
checkIfAttachmentsLoaded(result: result,
|
||||||
|
attachmentCount: attachments.count,
|
||||||
|
ignoredAttachments: ignoredAttachments,
|
||||||
|
item: item,
|
||||||
|
loadedItems: loadedItems)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
@@ -52,20 +78,28 @@ func loadAttachments(with extensionContext: NSExtensionContext?, loadedItems: @e
|
|||||||
log("Found NSURL: \(url)")
|
log("Found NSURL: \(url)")
|
||||||
result.urls += [(url as URL)]
|
result.urls += [(url as URL)]
|
||||||
|
|
||||||
checkIfAttachmentsLoaded(result: result, attachmentCount: attachments.count, ignoredAttachments: ignoredAttachments)
|
checkIfAttachmentsLoaded(result: result,
|
||||||
|
attachmentCount: attachments.count,
|
||||||
|
ignoredAttachments: ignoredAttachments,
|
||||||
|
item: item,
|
||||||
|
loadedItems: loadedItems)
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
log("Attachment is not a URL, ignoring. All attachments: \(attachments.description)")
|
log("Attachment is not a URL, ignoring. All attachments: \(attachments.description)")
|
||||||
ignoredAttachments += 1
|
ignoredAttachments += 1
|
||||||
|
|
||||||
checkIfAttachmentsLoaded(result: result, attachmentCount: attachments.count, ignoredAttachments: ignoredAttachments)
|
checkIfAttachmentsLoaded(result: result,
|
||||||
|
attachmentCount: attachments.count,
|
||||||
|
ignoredAttachments: ignoredAttachments,
|
||||||
|
item: item,
|
||||||
|
loadedItems: loadedItems)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
loadTextIfPossible()
|
deliverTextIfPossible(from: item, loadedItems: loadedItems)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
@@ -74,14 +108,23 @@ func loadAttachments(with extensionContext: NSExtensionContext?, loadedItems: @e
|
|||||||
let cancelError = NSError(domain: NSCocoaErrorDomain, code: NSUserCancelledError, userInfo: nil)
|
let cancelError = NSError(domain: NSCocoaErrorDomain, code: NSUserCancelledError, userInfo: nil)
|
||||||
extensionContext.cancelRequest(withError: cancelError)
|
extensionContext.cancelRequest(withError: cancelError)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
func loadTextIfPossible() {
|
private func deliverTextIfPossible(from item: NSExtensionItem, loadedItems: @escaping (AttachmentDetails) -> Void) {
|
||||||
if let text = item.attributedContentText?.string {
|
guard let details = textAttachmentDetails(from: item) else { return }
|
||||||
|
DispatchQueue.main.async {
|
||||||
|
loadedItems(details)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private func textAttachmentDetails(from item: NSExtensionItem) -> AttachmentDetails? {
|
||||||
|
guard let text = item.attributedContentText?.string else { return nil }
|
||||||
|
|
||||||
log("Found text content: \(text)")
|
log("Found text content: \(text)")
|
||||||
|
|
||||||
result.textToSend = text
|
var result = AttachmentDetails(urls: [], textToSend: text, shortDescription: "", previewImage: nil)
|
||||||
|
|
||||||
#if os(macOS)
|
#if os(macOS)
|
||||||
result.previewImage = NSImage(named: NSImage.multipleDocumentsName)
|
result.previewImage = NSImage(named: NSImage.multipleDocumentsName)
|
||||||
@@ -99,14 +142,15 @@ func loadAttachments(with extensionContext: NSExtensionContext?, loadedItems: @e
|
|||||||
result.shortDescription = cleanedText
|
result.shortDescription = cleanedText
|
||||||
}
|
}
|
||||||
|
|
||||||
DispatchQueue.main.async {
|
return result
|
||||||
loadedItems(result)
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
func checkIfAttachmentsLoaded(result: AttachmentDetails, attachmentCount: Int, ignoredAttachments: Int = 0) {
|
private func checkIfAttachmentsLoaded(result: AttachmentDetails,
|
||||||
|
attachmentCount: Int,
|
||||||
|
ignoredAttachments: Int = 0,
|
||||||
|
item: NSExtensionItem,
|
||||||
|
loadedItems: @escaping (AttachmentDetails) -> Void) {
|
||||||
|
|
||||||
var result = result
|
var result = result
|
||||||
|
|
||||||
@@ -114,7 +158,7 @@ func loadAttachments(with extensionContext: NSExtensionContext?, loadedItems: @e
|
|||||||
|
|
||||||
// Unable to load anything useful, check if there is text as last resort
|
// Unable to load anything useful, check if there is text as last resort
|
||||||
if result.urls.count == 0 {
|
if result.urls.count == 0 {
|
||||||
loadTextIfPossible()
|
deliverTextIfPossible(from: item, loadedItems: loadedItems)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
else if result.urls.count == 1 {
|
else if result.urls.count == 1 {
|
||||||
@@ -152,5 +196,48 @@ func loadAttachments(with extensionContext: NSExtensionContext?, loadedItems: @e
|
|||||||
loadedItems(result)
|
loadedItems(result)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private func imageUrl(from data: Any?, typeIdentifier: String) -> URL? {
|
||||||
|
if let url = data as? NSURL {
|
||||||
|
log("Found image URL: \(url)")
|
||||||
|
return url as URL
|
||||||
|
}
|
||||||
|
|
||||||
|
if let data = data as? Data {
|
||||||
|
let fileExtension = UTType(typeIdentifier)?.preferredFilenameExtension ?? "png"
|
||||||
|
return writeDataToTemporaryFile(data: data, fileExtension: fileExtension)
|
||||||
|
}
|
||||||
|
|
||||||
|
#if canImport(UIKit)
|
||||||
|
if let image = data as? UIImage,
|
||||||
|
let imageData = image.pngData() {
|
||||||
|
return writeDataToTemporaryFile(data: imageData, fileExtension: "png")
|
||||||
|
}
|
||||||
|
#elseif os(macOS)
|
||||||
|
if let image = data as? NSImage,
|
||||||
|
let tiff = image.tiffRepresentation,
|
||||||
|
let rep = NSBitmapImageRep(data: tiff),
|
||||||
|
let pngData = rep.representation(using: .png, properties: [:]) {
|
||||||
|
return writeDataToTemporaryFile(data: pngData, fileExtension: "png")
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private func writeDataToTemporaryFile(data: Data, fileExtension: String) -> URL? {
|
||||||
|
let sanitizedExtension = fileExtension.isEmpty ? "dat" : fileExtension
|
||||||
|
let tempUrl = FileManager.default.temporaryDirectory
|
||||||
|
.appendingPathComponent("\(UUID().uuidString).\(sanitizedExtension)")
|
||||||
|
do {
|
||||||
|
try data.write(to: tempUrl)
|
||||||
|
return tempUrl
|
||||||
|
}
|
||||||
|
catch {
|
||||||
|
log("Failed to write attachment data to temp file: \(error)")
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user