Attachment uploading support
This commit is contained in:
@@ -20,7 +20,7 @@
|
||||
return @"sendMessage";
|
||||
}
|
||||
|
||||
- (BOOL)_sendMessage:(NSString *)messageBody toChatWithGUID:(NSString *)chatGUID
|
||||
- (BOOL)_sendMessage:(NSString *)messageBody toChatWithGUID:(NSString *)chatGUID attachmentGUIDs:(NSArray<NSString *> *)guids
|
||||
{
|
||||
__block BOOL result = YES;
|
||||
|
||||
@@ -32,7 +32,16 @@
|
||||
IMHandle *senderHandle = [iMessageAccount loginIMHandle];
|
||||
|
||||
NSAttributedString *replyAttrString = [[NSAttributedString alloc] initWithString:messageBody];
|
||||
IMMessage *reply = [IMMessage fromMeIMHandle:senderHandle withText:replyAttrString fileTransferGUIDs:@[] flags:kIMMessageFinished];
|
||||
NSAttributedString *attrStringWithFileTransfers = IMCreateSuperFormatStringWithAppendedFileTransfers(replyAttrString, guids);
|
||||
|
||||
IMMessage *reply = [IMMessage fromMeIMHandle:senderHandle
|
||||
withText:attrStringWithFileTransfers
|
||||
fileTransferGUIDs:guids
|
||||
flags:(kIMMessageFinished | kIMMessageIsFromMe)];
|
||||
|
||||
for (NSString *guid in [reply fileTransferGUIDs]) {
|
||||
[[IMFileTransferCenter sharedInstance] assignTransfer:guid toHandle:chat.recipient];
|
||||
}
|
||||
|
||||
if (!chat) {
|
||||
NSLog(@"Chat does not exist: %@", chatGUID);
|
||||
@@ -63,7 +72,12 @@
|
||||
return;
|
||||
}
|
||||
|
||||
BOOL result = [self _sendMessage:messageBody toChatWithGUID:guid];
|
||||
NSArray *transferGUIDs = [args objectForKey:@"fileTransferGUIDs"];
|
||||
if (!transferGUIDs) {
|
||||
transferGUIDs = @[];
|
||||
}
|
||||
|
||||
BOOL result = [self _sendMessage:messageBody toChatWithGUID:guid attachmentGUIDs:transferGUIDs];
|
||||
if (result) {
|
||||
response = [[HTTPErrorResponse alloc] initWithErrorCode:200];
|
||||
}
|
||||
|
||||
17
kordophone/Bridge/Operations/MBIMUploadAttachmentOperation.h
Normal file
17
kordophone/Bridge/Operations/MBIMUploadAttachmentOperation.h
Normal file
@@ -0,0 +1,17 @@
|
||||
//
|
||||
// MBIMUploadAttachmentOperation.h
|
||||
// kordophoned
|
||||
//
|
||||
// Created by James Magahern on 1/16/19.
|
||||
// Copyright © 2019 James Magahern. All rights reserved.
|
||||
//
|
||||
|
||||
#import "MBIMBridgeOperation.h"
|
||||
|
||||
NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
@interface MBIMUploadAttachmentOperation : MBIMBridgeOperation
|
||||
|
||||
@end
|
||||
|
||||
NS_ASSUME_NONNULL_END
|
||||
66
kordophone/Bridge/Operations/MBIMUploadAttachmentOperation.m
Normal file
66
kordophone/Bridge/Operations/MBIMUploadAttachmentOperation.m
Normal file
@@ -0,0 +1,66 @@
|
||||
//
|
||||
// MBIMUploadAttachmentOperation.m
|
||||
// kordophoned
|
||||
//
|
||||
// Created by James Magahern on 1/16/19.
|
||||
// Copyright © 2019 James Magahern. All rights reserved.
|
||||
//
|
||||
|
||||
#import "MBIMUploadAttachmentOperation.h"
|
||||
#import "MBIMDataResponse.h"
|
||||
|
||||
#import <IMCore/IMCore.h>
|
||||
|
||||
@implementation MBIMUploadAttachmentOperation
|
||||
|
||||
+ (void)load { [super load]; }
|
||||
|
||||
+ (NSString *)endpointName
|
||||
{
|
||||
return @"uploadAttachment";
|
||||
}
|
||||
|
||||
- (void)main
|
||||
{
|
||||
NSObject<HTTPResponse> *response = nil;
|
||||
do {
|
||||
NSString *filename = [self valueForQueryItemWithName:@"filename"];
|
||||
if ([filename length] == 0) {
|
||||
NSLog(@"No filename provided");
|
||||
response = [[HTTPErrorResponse alloc] initWithErrorCode:500];
|
||||
break;
|
||||
}
|
||||
|
||||
NSData *attachmentData = self.requestBodyData;
|
||||
if ([attachmentData length] == 0) {
|
||||
NSLog(@"No attachment data in request");
|
||||
response = [[HTTPErrorResponse alloc] initWithErrorCode:500];
|
||||
break;
|
||||
}
|
||||
|
||||
NSString *localPath = [NSTemporaryDirectory() stringByAppendingPathComponent:filename];
|
||||
NSURL *localURL = [NSURL fileURLWithPath:localPath];
|
||||
BOOL success = [attachmentData writeToURL:localURL atomically:NO];
|
||||
if (!success) {
|
||||
NSLog(@"Error writing attachment to temporary directory");
|
||||
response = [[HTTPErrorResponse alloc] initWithErrorCode:500];
|
||||
break;
|
||||
}
|
||||
|
||||
NSString *guid = [[IMFileTransferCenter sharedInstance] guidForNewOutgoingTransferWithLocalURL:localURL];
|
||||
if (!guid) {
|
||||
NSLog(@"There was some problem shuttling the file to IMCore");
|
||||
response = [[HTTPErrorResponse alloc] initWithErrorCode:500];
|
||||
break;
|
||||
}
|
||||
|
||||
NSDictionary *responseDict = @{
|
||||
@"fileTransferGUID" : guid
|
||||
};
|
||||
response = [MBIMJSONDataResponse responseWithJSONObject:responseDict];
|
||||
} while (0);
|
||||
|
||||
self.serverCompletionBlock(response);
|
||||
}
|
||||
|
||||
@end
|
||||
Reference in New Issue
Block a user