Private
Public Access
1
0

FetchAttachment: need to check for some edge cases wrt preview generation

This commit is contained in:
2024-01-07 18:11:59 -08:00
parent 147dc15d1d
commit c65803845b

View File

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