Private
Public Access
1
0
Files
Kordophone/kordophone/Bridge/Operations/MBIMFetchAttachmentOperation.m
James Magahern 4f7a6d1b87 Move off of macosxinternal sdk
This moves kordophone off of using the internal SDK and switches to using class dumped headers instead.
2019-12-16 17:29:53 -08:00

71 lines
2.1 KiB
Objective-C

//
// MBIMFetchAttachmentOperation.m
// kordophoned
//
// Created by James Magahern on 11/20/18.
// Copyright © 2018 James Magahern. All rights reserved.
//
#import "MBIMFetchAttachmentOperation.h"
#import "MBIMDataResponse.h"
#import "IMCore_ClassDump.h"
@implementation MBIMFetchAttachmentOperation
+ (void)load { [super load]; }
+ (NSString *)endpointName
{
return @"attachment";
}
- (void)main
{
NSObject<HTTPResponse> *response = nil;
do {
NSString *guid = [self valueForQueryItemWithName:@"guid"];
if (!guid) {
MBIMLogInfo(@"No query item provided");
response = [[HTTPErrorResponse alloc] initWithErrorCode:500];
break;
}
IMFileTransfer *transfer = [[IMFileTransferCenter sharedInstance] transferForGUID:guid];
if (!transfer) {
MBIMLogInfo(@"No transfer found for guid: %@", guid);
response = [[HTTPErrorResponse alloc] initWithErrorCode:404];
break;
}
if (![transfer existsAtLocalPath]) {
MBIMLogInfo(@"We don't have the file for this yet (still downloading to server?)");
response = [[HTTPErrorResponse alloc] initWithErrorCode:404];
break;
}
NSString *localPath = [transfer localPath];
NSData *responseData = [NSData dataWithContentsOfFile:localPath];
if (!responseData) {
MBIMLogInfo(@"Wasn't able to load data from local path: %@", localPath);
response = [[HTTPErrorResponse alloc] initWithErrorCode:404];
break;
}
NSString *mimeType = [transfer mimeType];
// It's unusual, but if this is nil, try to guess the MIME type based on the filename
if (!mimeType) {
NSString *extension = [[localPath pathExtension] lowercaseString];
// XXX: REALLY hacky
mimeType = [NSString stringWithFormat:@"image/%@", extension];
}
response = [[MBIMDataResponse alloc] initWithData:responseData contentType:mimeType];
} while (0);
self.serverCompletionBlock(response);
}
@end