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"
|
|
|
|
|
|
2019-12-16 17:29:53 -08:00
|
|
|
#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 {
|
2018-11-21 15:51:51 -07:00
|
|
|
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];
|
2019-03-03 14:23:54 -08:00
|
|
|
// 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
|