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

71 lines
2.1 KiB
Mathematica
Raw Normal View History

2018-11-21 01:38:43 -07:00
//
// 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"
2018-11-21 01:38:43 -07:00
@implementation MBIMFetchAttachmentOperation
+ (void)load { [super load]; }
+ (NSString *)endpointName
{
return @"attachment";
}
- (void)main
{
NSObject<HTTPResponse> *response = nil;
do {
NSString *guid = [self valueForQueryItemWithName:@"guid"];
2018-11-21 01:38:43 -07:00
if (!guid) {
2019-01-22 23:31:36 -08:00
MBIMLogInfo(@"No query item provided");
2018-11-21 01:38:43 -07:00
response = [[HTTPErrorResponse alloc] initWithErrorCode:500];
break;
}
IMFileTransfer *transfer = [[IMFileTransferCenter sharedInstance] transferForGUID:guid];
if (!transfer) {
2019-01-22 23:31:36 -08:00
MBIMLogInfo(@"No transfer found for guid: %@", guid);
2018-11-21 01:38:43 -07:00
response = [[HTTPErrorResponse alloc] initWithErrorCode:404];
break;
}
if (![transfer existsAtLocalPath]) {
2019-01-22 23:31:36 -08:00
MBIMLogInfo(@"We don't have the file for this yet (still downloading to server?)");
2018-11-21 01:38:43 -07:00
response = [[HTTPErrorResponse alloc] initWithErrorCode:404];
break;
}
NSString *localPath = [transfer localPath];
NSData *responseData = [NSData dataWithContentsOfFile:localPath];
if (!responseData) {
2019-01-22 23:31:36 -08:00
MBIMLogInfo(@"Wasn't able to load data from local path: %@", localPath);
2018-11-21 01:38:43 -07:00
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];
}
2018-11-21 01:38:43 -07:00
response = [[MBIMDataResponse alloc] initWithData:responseData contentType:mimeType];
} while (0);
self.serverCompletionBlock(response);
}
@end