Private
Public Access
1
0

Adds support for image previews

Just need to append ?preview=1 to attachment fetch operation.
This commit is contained in:
James Magahern
2022-12-20 16:29:26 -08:00
parent ba8f76f4bd
commit 3082c4ab19
6 changed files with 144 additions and 12 deletions

View File

@@ -18,12 +18,18 @@ struct IMFileLocation_t {
int _field5; int _field5;
}; };
struct IMPreviewConstraints { typedef struct IMPreviewConstraints {
double _field1; CGFloat maxPxWidth;
struct CGSize _field2; CGSize minThumbnailPxSize;
double _field3; CGFloat scale;
char _field4; BOOL isSticker;
}; BOOL generateMetadata;
} IMPreviewConstraints;
extern IMPreviewConstraints IMPreviewConstraintsFromDictionary(NSDictionary *dictionary);
extern NSDictionary *IMPreviewConstraintsDictionaryFromConstraint(IMPreviewConstraints constraint);
extern BOOL IMPreviewConstraintsEqualToConstraints(IMPreviewConstraints constraints1, IMPreviewConstraints constraints2);
extern IMPreviewConstraints IMPreviewConstraintsZero(void);
struct _TidyDoc { struct _TidyDoc {
int _field1; int _field1;
@@ -155,6 +161,8 @@ struct __va_list_tag {
- (void)setObject:(id)arg1 forKey:(id)arg2; - (void)setObject:(id)arg1 forKey:(id)arg2;
@end @end
extern NSURL* IMAttachmentPreviewFileURL(NSURL *attachmentURL, NSString *extension, BOOL generateIntermediaryDirectories);
@protocol IMPreviewGeneratorProtocol @protocol IMPreviewGeneratorProtocol
+ (BOOL)shouldShadePreview; + (BOOL)shouldShadePreview;
+ (BOOL)shouldScaleUpPreview; + (BOOL)shouldScaleUpPreview;

View File

@@ -73,6 +73,10 @@
CD14F1A4219FF22700E7DD22 /* IMMessageItem+Encoded.m in Sources */ = {isa = PBXBuildFile; fileRef = CD14F1A3219FF22700E7DD22 /* IMMessageItem+Encoded.m */; }; CD14F1A4219FF22700E7DD22 /* IMMessageItem+Encoded.m in Sources */ = {isa = PBXBuildFile; fileRef = CD14F1A3219FF22700E7DD22 /* IMMessageItem+Encoded.m */; };
CD14F1AA219FF3B800E7DD22 /* MBIMUpdateQueue.m in Sources */ = {isa = PBXBuildFile; fileRef = CD14F1A9219FF3B800E7DD22 /* MBIMUpdateQueue.m */; }; CD14F1AA219FF3B800E7DD22 /* MBIMUpdateQueue.m in Sources */ = {isa = PBXBuildFile; fileRef = CD14F1A9219FF3B800E7DD22 /* MBIMUpdateQueue.m */; };
CD14F1AD219FFAE100E7DD22 /* MBIMConcurrentHTTPServer.m in Sources */ = {isa = PBXBuildFile; fileRef = CD14F1AC219FFAE100E7DD22 /* MBIMConcurrentHTTPServer.m */; }; CD14F1AD219FFAE100E7DD22 /* MBIMConcurrentHTTPServer.m in Sources */ = {isa = PBXBuildFile; fileRef = CD14F1AC219FFAE100E7DD22 /* MBIMConcurrentHTTPServer.m */; };
CD2782BC29527FE500C0C030 /* IMSharedUtilities.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = CD2782BB29527FE500C0C030 /* IMSharedUtilities.framework */; };
CD2782BF2952832B00C0C030 /* MBIMImageUtils.m in Sources */ = {isa = PBXBuildFile; fileRef = CD2782BE2952832B00C0C030 /* MBIMImageUtils.m */; };
CD2782FE2952875F00C0C030 /* CoreGraphics.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = CD2782FD2952875F00C0C030 /* CoreGraphics.framework */; };
CD2783002952876700C0C030 /* ImageIO.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = CD2782FF2952876700C0C030 /* ImageIO.framework */; };
CD2ECEC2269539100055E302 /* MBIMAuthenticateOperation.m in Sources */ = {isa = PBXBuildFile; fileRef = CD2ECEC1269539100055E302 /* MBIMAuthenticateOperation.m */; }; CD2ECEC2269539100055E302 /* MBIMAuthenticateOperation.m in Sources */ = {isa = PBXBuildFile; fileRef = CD2ECEC1269539100055E302 /* MBIMAuthenticateOperation.m */; };
CD2ECEC526953F2A0055E302 /* MBIMAuthToken.m in Sources */ = {isa = PBXBuildFile; fileRef = CD2ECEC426953F2A0055E302 /* MBIMAuthToken.m */; }; CD2ECEC526953F2A0055E302 /* MBIMAuthToken.m in Sources */ = {isa = PBXBuildFile; fileRef = CD2ECEC426953F2A0055E302 /* MBIMAuthToken.m */; };
CD602056219B5DFD0024D9C5 /* MBIMBridgeOperation.m in Sources */ = {isa = PBXBuildFile; fileRef = CD602055219B5DFD0024D9C5 /* MBIMBridgeOperation.m */; }; CD602056219B5DFD0024D9C5 /* MBIMBridgeOperation.m in Sources */ = {isa = PBXBuildFile; fileRef = CD602055219B5DFD0024D9C5 /* MBIMBridgeOperation.m */; };
@@ -223,6 +227,11 @@
CD14F1A9219FF3B800E7DD22 /* MBIMUpdateQueue.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = MBIMUpdateQueue.m; sourceTree = "<group>"; }; CD14F1A9219FF3B800E7DD22 /* MBIMUpdateQueue.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = MBIMUpdateQueue.m; sourceTree = "<group>"; };
CD14F1AB219FFAE100E7DD22 /* MBIMConcurrentHTTPServer.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MBIMConcurrentHTTPServer.h; sourceTree = "<group>"; }; CD14F1AB219FFAE100E7DD22 /* MBIMConcurrentHTTPServer.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MBIMConcurrentHTTPServer.h; sourceTree = "<group>"; };
CD14F1AC219FFAE100E7DD22 /* MBIMConcurrentHTTPServer.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = MBIMConcurrentHTTPServer.m; sourceTree = "<group>"; }; CD14F1AC219FFAE100E7DD22 /* MBIMConcurrentHTTPServer.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = MBIMConcurrentHTTPServer.m; sourceTree = "<group>"; };
CD2782BB29527FE500C0C030 /* IMSharedUtilities.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = IMSharedUtilities.framework; path = Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.0.Internal.sdk/System/Library/PrivateFrameworks/IMSharedUtilities.framework; sourceTree = DEVELOPER_DIR; };
CD2782BD2952832B00C0C030 /* MBIMImageUtils.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MBIMImageUtils.h; sourceTree = "<group>"; };
CD2782BE2952832B00C0C030 /* MBIMImageUtils.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = MBIMImageUtils.m; sourceTree = "<group>"; };
CD2782FD2952875F00C0C030 /* CoreGraphics.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreGraphics.framework; path = System/Library/Frameworks/CoreGraphics.framework; sourceTree = SDKROOT; };
CD2782FF2952876700C0C030 /* ImageIO.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = ImageIO.framework; path = System/Library/Frameworks/ImageIO.framework; sourceTree = SDKROOT; };
CD2ECEC0269539100055E302 /* MBIMAuthenticateOperation.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MBIMAuthenticateOperation.h; sourceTree = "<group>"; }; CD2ECEC0269539100055E302 /* MBIMAuthenticateOperation.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MBIMAuthenticateOperation.h; sourceTree = "<group>"; };
CD2ECEC1269539100055E302 /* MBIMAuthenticateOperation.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = MBIMAuthenticateOperation.m; sourceTree = "<group>"; }; CD2ECEC1269539100055E302 /* MBIMAuthenticateOperation.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = MBIMAuthenticateOperation.m; sourceTree = "<group>"; };
CD2ECEC326953F2A0055E302 /* MBIMAuthToken.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MBIMAuthToken.h; sourceTree = "<group>"; }; CD2ECEC326953F2A0055E302 /* MBIMAuthToken.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MBIMAuthToken.h; sourceTree = "<group>"; };
@@ -287,8 +296,11 @@
isa = PBXFrameworksBuildPhase; isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647; buildActionMask = 2147483647;
files = ( files = (
CD2782BC29527FE500C0C030 /* IMSharedUtilities.framework in Frameworks */,
1A257CCB23A8681200A4A2C8 /* Security.framework in Frameworks */, 1A257CCB23A8681200A4A2C8 /* Security.framework in Frameworks */,
CD2783002952876700C0C030 /* ImageIO.framework in Frameworks */,
1ACFCFDF219EB31400E2C237 /* CocoaHTTPServer.framework in Frameworks */, 1ACFCFDF219EB31400E2C237 /* CocoaHTTPServer.framework in Frameworks */,
CD2782FE2952875F00C0C030 /* CoreGraphics.framework in Frameworks */,
1A257CC923A867EF00A4A2C8 /* IMCore.framework in Frameworks */, 1A257CC923A867EF00A4A2C8 /* IMCore.framework in Frameworks */,
); );
runOnlyForDeploymentPostprocessing = 0; runOnlyForDeploymentPostprocessing = 0;
@@ -334,6 +346,9 @@
1A0C445E219A45B400F2AC00 /* Frameworks */ = { 1A0C445E219A45B400F2AC00 /* Frameworks */ = {
isa = PBXGroup; isa = PBXGroup;
children = ( children = (
CD2782FF2952876700C0C030 /* ImageIO.framework */,
CD2782FD2952875F00C0C030 /* CoreGraphics.framework */,
CD2782BB29527FE500C0C030 /* IMSharedUtilities.framework */,
1A257CCA23A8681200A4A2C8 /* Security.framework */, 1A257CCA23A8681200A4A2C8 /* Security.framework */,
1A257CC823A867EF00A4A2C8 /* IMCore.framework */, 1A257CC823A867EF00A4A2C8 /* IMCore.framework */,
); );
@@ -370,6 +385,8 @@
CD936A31289B353F0093A1AC /* MBIMErrorResponse.m */, CD936A31289B353F0093A1AC /* MBIMErrorResponse.m */,
1AA43E93219EC38E00EDF1A7 /* MBIMHTTPUtilities.h */, 1AA43E93219EC38E00EDF1A7 /* MBIMHTTPUtilities.h */,
1AA43E94219EC38E00EDF1A7 /* MBIMHTTPUtilities.m */, 1AA43E94219EC38E00EDF1A7 /* MBIMHTTPUtilities.m */,
CD2782BD2952832B00C0C030 /* MBIMImageUtils.h */,
CD2782BE2952832B00C0C030 /* MBIMImageUtils.m */,
); );
path = Utilities; path = Utilities;
sourceTree = "<group>"; sourceTree = "<group>";
@@ -893,6 +910,7 @@
CD602056219B5DFD0024D9C5 /* MBIMBridgeOperation.m in Sources */, CD602056219B5DFD0024D9C5 /* MBIMBridgeOperation.m in Sources */,
CD60205F219B674B0024D9C5 /* MBIMConversationListOperation.m in Sources */, CD60205F219B674B0024D9C5 /* MBIMConversationListOperation.m in Sources */,
CDE4556421A3578A0041F5DD /* IMChat+Encoded.m in Sources */, CDE4556421A3578A0041F5DD /* IMChat+Encoded.m in Sources */,
CD2782BF2952832B00C0C030 /* MBIMImageUtils.m in Sources */,
1AA43E8F219EBB2D00EDF1A7 /* MBIMJSONDataResponse.m in Sources */, 1AA43E8F219EBB2D00EDF1A7 /* MBIMJSONDataResponse.m in Sources */,
CD936A32289B353F0093A1AC /* MBIMErrorResponse.m in Sources */, CD936A32289B353F0093A1AC /* MBIMErrorResponse.m in Sources */,
CD2ECEC2269539100055E302 /* MBIMAuthenticateOperation.m in Sources */, CD2ECEC2269539100055E302 /* MBIMAuthenticateOperation.m in Sources */,

View File

@@ -8,8 +8,11 @@
#import "MBIMFetchAttachmentOperation.h" #import "MBIMFetchAttachmentOperation.h"
#import "MBIMDataResponse.h" #import "MBIMDataResponse.h"
#import "MBIMImageUtils.h"
#import "IMCore_ClassDump.h" #import "IMCore_ClassDump.h"
#import "IMSharedUtilities_ClassDump.h"
#import <CoreGraphics/CoreGraphics.h>
@implementation MBIMFetchAttachmentOperation @implementation MBIMFetchAttachmentOperation
@@ -24,8 +27,8 @@
{ {
NSObject<HTTPResponse> *response = nil; NSObject<HTTPResponse> *response = nil;
do { do {
BOOL preview = [[self valueForQueryItemWithName:@"preview"] boolValue];
NSString *guid = [self valueForQueryItemWithName:@"guid"]; NSString *guid = [self valueForQueryItemWithName:@"guid"];
if (!guid) { if (!guid) {
MBIMLogInfo(@"No query item provided"); MBIMLogInfo(@"No query item provided");
response = [[HTTPErrorResponse alloc] initWithErrorCode:500]; response = [[HTTPErrorResponse alloc] initWithErrorCode:500];
@@ -45,22 +48,65 @@
break; break;
} }
NSString *localPath = [transfer localPath]; NSData *responseData = nil;
NSData *responseData = [NSData dataWithContentsOfFile:localPath]; NSURL *localURL = [transfer localURL];
NSString *extension = [[localURL pathExtension] lowercaseString];
if (preview) {
NSURL *previewURL = IMAttachmentPreviewFileURL(localURL, extension, YES);
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];
CGImageRef previewImage = [generator newPreviewFromSourceURL:localURL withPreviewConstraints:constraints error:&error];
if (error) {
MBIMLogInfo(@"Unable to generate preview for attachment guid: %@", guid);
response = [[HTTPErrorResponse alloc] initWithErrorCode:500];
break;
}
responseData = MBIMCGImageJPEGRepresentation(previewImage);
// Persist JPEG preview to disk
[responseData writeToURL:previewURL atomically:YES];
} else {
// File exists
MBIMLogInfo(@"Using cached preview image for guid: %@ at %@", guid, [previewURL path]);
responseData = [NSData dataWithContentsOfURL:previewURL];
}
} else {
responseData = [NSData dataWithContentsOfURL:localURL];
}
if (!responseData) { if (!responseData) {
MBIMLogInfo(@"Wasn't able to load data from local path: %@", localPath); MBIMLogInfo(@"Wasn't able to load data for guid: %@", guid);
response = [[HTTPErrorResponse alloc] initWithErrorCode:404]; response = [[HTTPErrorResponse alloc] initWithErrorCode:404];
break; break;
} }
NSString *mimeType = [transfer mimeType]; NSString *mimeType = [transfer mimeType];
if ([mimeType isEqualToString:@"image/heic"]) {
// TODO: We should convert this to JPEG here. I don't want clients to have to deal with HEIC.
MBIMLogInfo(@"WARNING: Returning HEIC data for attachment %@", guid);
}
// It's unusual, but if this is nil, try to guess the MIME type based on the filename // It's unusual, but if this is nil, try to guess the MIME type based on the filename
if (!mimeType) { if (!mimeType) {
NSString *extension = [[localPath pathExtension] lowercaseString];
// XXX: REALLY hacky // XXX: REALLY hacky
mimeType = [NSString stringWithFormat:@"image/%@", extension]; mimeType = [NSString stringWithFormat:@"image/%@", extension];
} }
response = [[MBIMDataResponse alloc] initWithData:responseData contentType:mimeType]; response = [[MBIMDataResponse alloc] initWithData:responseData contentType:mimeType];
} while (0); } while (0);

View File

@@ -0,0 +1,12 @@
//
// MBIMImageUtils.h
// kordophoned
//
// Created by James Magahern on 12/20/22.
// Copyright © 2022 James Magahern. All rights reserved.
//
#import <Foundation/Foundation.h>
#import <CoreGraphics/CoreGraphics.h>
extern NSData* MBIMCGImageJPEGRepresentation(CGImageRef imageRef);

View File

@@ -0,0 +1,37 @@
//
// MBIMImageUtils.m
// kordophoned
//
// Created by James Magahern on 12/20/22.
// Copyright © 2022 James Magahern. All rights reserved.
//
#import "MBIMImageUtils.h"
#import <ImageIO/ImageIO.h>
NSData* MBIMCGImageJPEGRepresentation(CGImageRef imageRef)
{
if (imageRef == NULL) return nil;
NSNumber *const DPI = @72.0;
NSNumber *const compressionQuality = @0.9;
NSDictionary *properties = @{
(__bridge NSString *)kCGImagePropertyDPIWidth : DPI,
(__bridge NSString *)kCGImagePropertyDPIHeight : DPI,
(__bridge NSString *)kCGImageDestinationLossyCompressionQuality : compressionQuality,
};
bool success = false;
NSMutableData *data = [NSMutableData data];
if (data) {
CGImageDestinationRef imageDestination = CGImageDestinationCreateWithData((CFMutableDataRef)data, CFSTR("public.jpeg"), 1/*count*/, NULL/*options*/);
if (imageDestination != NULL) {
CGImageDestinationAddImage(imageDestination, imageRef, (__bridge CFDictionaryRef)properties);
success = CGImageDestinationFinalize(imageDestination);
CFRelease(imageDestination);
}
}
return success ? data : nil;
}

View File

@@ -21,16 +21,27 @@
if ([self fileTransferGUIDs]) { if ([self fileTransferGUIDs]) {
// Support only images right now // Support only images right now
NSMutableDictionary *attachmentMetadatas = [NSMutableDictionary dictionary];
NSMutableArray *filteredFileTransferGUIDs = [NSMutableArray array]; NSMutableArray *filteredFileTransferGUIDs = [NSMutableArray array];
for (NSString *guid in self.fileTransferGUIDs) { for (NSString *guid in self.fileTransferGUIDs) {
NSMutableDictionary *metadata = [NSMutableDictionary dictionary];
IMFileTransfer *transfer = [[IMFileTransferCenter sharedInstance] transferForGUID:guid]; IMFileTransfer *transfer = [[IMFileTransferCenter sharedInstance] transferForGUID:guid];
if ([[transfer mimeType] containsString:@"image"]) { if ([[transfer mimeType] containsString:@"image"]) {
[filteredFileTransferGUIDs addObject:guid]; [filteredFileTransferGUIDs addObject:guid];
if ([transfer attributionInfo] != nil) {
metadata[@"attributionInfo"] = [transfer attributionInfo];
}
}
if (metadata.count) {
attachmentMetadatas[guid] = metadata;
} }
} }
if ([filteredFileTransferGUIDs count]) { if ([filteredFileTransferGUIDs count]) {
messageDict[@"fileTransferGUIDs"] = filteredFileTransferGUIDs; messageDict[@"fileTransferGUIDs"] = filteredFileTransferGUIDs;
messageDict[@"attachmentMetadata"] = attachmentMetadatas;
} }
} }