From c65803845b2ef3c6a8285ad8a7f2d44f58142265 Mon Sep 17 00:00:00 2001 From: James Magahern Date: Sun, 7 Jan 2024 18:11:59 -0800 Subject: [PATCH] FetchAttachment: need to check for some edge cases wrt preview generation --- .../Operations/MBIMFetchAttachmentOperation.m | 34 +++++++++++++------ 1 file changed, 24 insertions(+), 10 deletions(-) diff --git a/kordophone/Bridge/Operations/MBIMFetchAttachmentOperation.m b/kordophone/Bridge/Operations/MBIMFetchAttachmentOperation.m index 1822d70..be39d9f 100644 --- a/kordophone/Bridge/Operations/MBIMFetchAttachmentOperation.m +++ b/kordophone/Bridge/Operations/MBIMFetchAttachmentOperation.m @@ -52,20 +52,34 @@ NSURL *localURL = [transfer localURL]; NSString *extension = [[localURL pathExtension] lowercaseString]; if (preview) { + IMPreviewConstraints constraints = IMPreviewConstraintsZero(); + + // Fetch preview constraints from transfer + NSDictionary *previewConstraintsDict = [[transfer attributionInfo] objectForKey:@"pgenszc"]; + if (previewConstraintsDict) { + constraints = IMPreviewConstraintsFromDictionary(previewConstraintsDict); + } else { + // Or, make a guess. + constraints.maxPxWidth = 500.0; + constraints.scale = 1.0; + } + NSURL *previewURL = IMAttachmentPreviewFileURL(localURL, extension, YES); + if (!previewURL) { + // I'm not sure why this sometimes returns nil... + MBIMLogInfo(@"Unable to generate attachment preview cache URL for %@, making one up.", localURL); + + NSURL *temporaryAttachmentCache = [[[NSFileManager defaultManager] temporaryDirectory] URLByAppendingPathComponent:@"kordophone_attachment_cache"]; + temporaryAttachmentCache = [temporaryAttachmentCache URLByAppendingPathComponent:guid]; + + [[NSFileManager defaultManager] createDirectoryAtURL:temporaryAttachmentCache withIntermediateDirectories:YES attributes:nil error:nil]; + + previewURL = [temporaryAttachmentCache URLByAppendingPathComponent:[localURL lastPathComponent]]; + } + if (![[NSFileManager defaultManager] fileExistsAtPath:[previewURL path]]) { MBIMLogInfo(@"Generating preview image for guid: %@ at %@", guid, [previewURL path]); - // Fetch preview constraints from transfer - NSDictionary *previewConstraintsDict = [[transfer attributionInfo] objectForKey:@"pgenszc"]; - if (!previewConstraintsDict) { - MBIMLogInfo(@"No preview constraints for attachment guid: %@", guid); - response = [[HTTPErrorResponse alloc] initWithErrorCode:500]; - break; - } - - IMPreviewConstraints constraints = IMPreviewConstraintsFromDictionary(previewConstraintsDict); - // Generate preview using preview generator manager NSError *error = nil; IMPreviewGeneratorManager *generator = [IMPreviewGeneratorManager sharedInstance];