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];