Private
Public Access
1
0
Files
Kordophone/kordophone/Bridge/Operations/MBIMUploadAttachmentOperation.m

67 lines
2.0 KiB
Mathematica
Raw Normal View History

2019-01-16 14:17:31 -08:00
//
// 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) {
2019-01-22 23:31:36 -08:00
MBIMLogInfo(@"No filename provided");
2019-01-16 14:17:31 -08:00
response = [[HTTPErrorResponse alloc] initWithErrorCode:500];
break;
}
NSData *attachmentData = self.requestBodyData;
if ([attachmentData length] == 0) {
2019-01-22 23:31:36 -08:00
MBIMLogInfo(@"No attachment data in request");
2019-01-16 14:17:31 -08:00
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) {
2019-01-22 23:31:36 -08:00
MBIMLogInfo(@"Error writing attachment to temporary directory");
2019-01-16 14:17:31 -08:00
response = [[HTTPErrorResponse alloc] initWithErrorCode:500];
break;
}
NSString *guid = [[IMFileTransferCenter sharedInstance] guidForNewOutgoingTransferWithLocalURL:localURL];
if (!guid) {
2019-01-22 23:31:36 -08:00
MBIMLogInfo(@"There was some problem shuttling the file to IMCore");
2019-01-16 14:17:31 -08:00
response = [[HTTPErrorResponse alloc] initWithErrorCode:500];
break;
}
NSDictionary *responseDict = @{
@"fileTransferGUID" : guid
};
response = [MBIMJSONDataResponse responseWithJSONObject:responseDict];
} while (0);
self.serverCompletionBlock(response);
}
@end