@@ -272,7 +272,42 @@ - (void)processItem:(NSItemProvider*)item {
272272 }
273273 };
274274
275+ NSItemProviderCompletionHandler dataHandler = ^(NSData * data, NSError * error) {
276+ NSURL * filePayloadURL = [self getPayloadURLFromExt: @" data" ];
277+ BOOL OK = [data writeToURL: filePayloadURL atomically: true ];
278+ if (!OK ) {
279+ [self completeItemAndAppendManifestAndLogErrorWithText: @" dataHandler: unable to write payload file" error: nil ];
280+ return ;
281+ }
282+ [self completeItemAndAppendManifestType: @" file" originalFileURL: filePayloadURL];
283+ };
284+
285+ NSItemProviderCompletionHandler fileHandlerSimple = ^(NSURL * url, NSError * error) {
286+ if (error != nil ) {
287+ if ([item hasItemConformingToTypeIdentifier: @" public.data" ]) {
288+ [item loadItemForTypeIdentifier: @" public.data" options: nil completionHandler: dataHandler];
289+ return ;
290+ }
291+ [self completeItemAndAppendManifestAndLogErrorWithText: @" fileHandlerSimple: no url or public.data" error: error];
292+ return ;
293+ }
294+ NSURL * filePayloadURL = [self getPayloadURLFromURL: url];
295+ [[NSFileManager defaultManager ] copyItemAtURL: url toURL: filePayloadURL error: &error];
296+ if (error != nil ) {
297+ [self completeItemAndAppendManifestAndLogErrorWithText: @" fileHandlerSimple: copy error" error: error];
298+ return ;
299+ }
300+ [self completeItemAndAppendManifestType: @" file" originalFileURL: filePayloadURL];
301+ };
302+
275303 NSItemProviderCompletionHandler textHandler = ^(NSString * text, NSError * error) {
304+ if (error != nil ) {
305+ if ([item hasItemConformingToTypeIdentifier: @" public.data" ]) {
306+ [item loadItemForTypeIdentifier: @" public.data" options: nil completionHandler: fileHandlerSimple];
307+ return ;
308+ }
309+ [self completeItemAndAppendManifestAndLogErrorWithText: @" textHandler: error and no public.data" error: nil ];
310+ }
276311 [self handleText: text chatOnly: false loadError: error];
277312 };
278313
@@ -294,7 +329,7 @@ - (void)processItem:(NSItemProvider*)item {
294329 // The NSItemProviderCompletionHandler interface is a little tricky. The caller of our handler
295330 // will inspect the arguments that we have given, and will attempt to give us the attachment
296331 // in this form. For files, we always want a file URL, and so that is what we pass in.
297- NSItemProviderCompletionHandler fileHandler = ^(NSURL * url, NSError * error) {
332+ NSItemProviderCompletionHandler fileHandlerMedia = ^(NSURL * url, NSError * error) {
298333 BOOL hasImage = [item hasItemConformingToTypeIdentifier: @" public.image" ];
299334 BOOL hasVideo = [item hasItemConformingToTypeIdentifier: @" public.movie" ];
300335
@@ -305,7 +340,7 @@ - (void)processItem:(NSItemProvider*)item {
305340 [item loadItemForTypeIdentifier: @" public.image" options: nil completionHandler: imageHandler];
306341 return ;
307342 }
308- [self completeItemAndAppendManifestAndLogErrorWithText: @" fileHandler: no url or image" error: error];
343+ [self completeItemAndAppendManifestAndLogErrorWithText: @" fileHandler: no url or public. image" error: error];
309344 return ;
310345 }
311346
@@ -316,7 +351,6 @@ - (void)processItem:(NSItemProvider*)item {
316351 return ;
317352 }
318353
319-
320354 if (hasVideo) {
321355 [self handleAndCompleteMediaFile: filePayloadURL isVideo: true ];
322356 } else if (hasImage) {
@@ -327,21 +361,22 @@ - (void)processItem:(NSItemProvider*)item {
327361 };
328362
329363 if ([item hasItemConformingToTypeIdentifier: @" public.movie" ]) {
330- [item loadItemForTypeIdentifier: @" public.movie" options: nil completionHandler: fileHandler ];
364+ [item loadItemForTypeIdentifier: @" public.movie" options: nil completionHandler: fileHandlerMedia ];
331365 } else if ([item hasItemConformingToTypeIdentifier: @" public.image" ]) {
332366 // Use the fileHandler here, so if the image is from e.g. the Photos app,
333367 // we'd go with the copy routine instead of having to encode an NSImage.
334368 // This is important for staying under the mem limit.
335- [item loadItemForTypeIdentifier: @" public.image" options: nil completionHandler: fileHandler ];
369+ [item loadItemForTypeIdentifier: @" public.image" options: nil completionHandler: fileHandlerMedia ];
336370 } else if ([item hasItemConformingToTypeIdentifier: @" public.file-url" ]) {
337- [item loadItemForTypeIdentifier: @" public.file-url" options: nil completionHandler: fileHandler];
371+ // Although this will be covered in the catch-all below, do it before public.text and public.url so that we get the file instead of a web URL when user shares a downloaded file from safari.
372+ [item loadItemForTypeIdentifier: @" public.file-url" options: nil completionHandler: fileHandlerSimple];
338373 } else if ([item hasItemConformingToTypeIdentifier: @" public.text" ]) {
339374 [item loadItemForTypeIdentifier: @" public.text" options: nil completionHandler: textHandler];
340375 } else if ([item hasItemConformingToTypeIdentifier: @" public.url" ]) {
341376 [item loadItemForTypeIdentifier: @" public.url" options: nil completionHandler: urlHandler];
342377 } else {
343- // Example: pdf in Safari
344- [item loadItemForTypeIdentifier: @" public.data " options: nil completionHandler: fileHandler ];
378+ // catch-all, including file-url or stuff like pdf from safari, or contact card.
379+ [item loadItemForTypeIdentifier: @" public.item " options: nil completionHandler: fileHandlerSimple ];
345380 }
346381}
347382
0 commit comments