// // 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 @implementation MBIMFetchAttachmentOperation + (void)load { [super load]; } + (NSString *)endpointName { return @"attachment"; } - (void)main { NSObject *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]; response = [[MBIMDataResponse alloc] initWithData:responseData contentType:mimeType]; } while (0); self.serverCompletionBlock(response); } @end