Private
Public Access
1
0
Files
Kordophone/kordophone/Bridge/Operations/MBIMFetchAttachmentOperation.m
2018-11-21 01:38:43 -07:00

72 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/IMCore.h>
@implementation MBIMFetchAttachmentOperation
+ (void)load { [super load]; }
+ (NSString *)endpointName
{
return @"attachment";
}
- (void)main
{
NSObject<HTTPResponse> *response = nil;
do {
NSURLComponents *urlComponents = [NSURLComponents componentsWithURL:self.requestURL resolvingAgainstBaseURL:NO];
NSString *guid = nil;
for (NSURLQueryItem *queryItem in [urlComponents queryItems]) {
if ([[queryItem name] isEqualToString:@"guid"]) {
guid = [queryItem value];
break;
}
}
if (!guid) {
NSLog(@"No query item provided");
response = [[HTTPErrorResponse alloc] initWithErrorCode:500];
break;
}
IMFileTransfer *transfer = [[IMFileTransferCenter sharedInstance] transferForGUID:guid];
if (!transfer) {
NSLog(@"No transfer found for guid: %@", guid);
response = [[HTTPErrorResponse alloc] initWithErrorCode:404];
break;
}
if (![transfer existsAtLocalPath]) {
NSLog(@"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) {
NSLog(@"Wasn't able to load data from local path: %@", localPath);
response = [[HTTPErrorResponse alloc] initWithErrorCode:404];
break;
}
NSString *mimeType = [transfer mimeType];
response = [[MBIMDataResponse alloc] initWithData:responseData contentType:mimeType];
} while (0);
self.serverCompletionBlock(response);
}
@end