Attachments support!
This commit is contained in:
17
kordophone/Bridge/Operations/MBIMFetchAttachmentOperation.h
Normal file
17
kordophone/Bridge/Operations/MBIMFetchAttachmentOperation.h
Normal file
@@ -0,0 +1,17 @@
|
||||
//
|
||||
// MBIMFetchAttachmentOperation.h
|
||||
// kordophoned
|
||||
//
|
||||
// Created by James Magahern on 11/20/18.
|
||||
// Copyright © 2018 James Magahern. All rights reserved.
|
||||
//
|
||||
|
||||
#import "MBIMBridgeOperation.h"
|
||||
|
||||
NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
@interface MBIMFetchAttachmentOperation : MBIMBridgeOperation
|
||||
|
||||
@end
|
||||
|
||||
NS_ASSUME_NONNULL_END
|
||||
71
kordophone/Bridge/Operations/MBIMFetchAttachmentOperation.m
Normal file
71
kordophone/Bridge/Operations/MBIMFetchAttachmentOperation.m
Normal file
@@ -0,0 +1,71 @@
|
||||
//
|
||||
// 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
|
||||
18
kordophone/Bridge/Operations/Utilities/MBIMDataResponse.h
Normal file
18
kordophone/Bridge/Operations/Utilities/MBIMDataResponse.h
Normal file
@@ -0,0 +1,18 @@
|
||||
//
|
||||
// MBIMDataResponse.h
|
||||
// kordophoned
|
||||
//
|
||||
// Created by James Magahern on 11/20/18.
|
||||
// Copyright © 2018 James Magahern. All rights reserved.
|
||||
//
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
#import <CocoaHTTPServer/HTTPDataResponse.h>
|
||||
|
||||
NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
@interface MBIMDataResponse : HTTPDataResponse
|
||||
- (instancetype)initWithData:(NSData *)data contentType:(NSString *)contentType;
|
||||
@end
|
||||
|
||||
NS_ASSUME_NONNULL_END
|
||||
32
kordophone/Bridge/Operations/Utilities/MBIMDataResponse.m
Normal file
32
kordophone/Bridge/Operations/Utilities/MBIMDataResponse.m
Normal file
@@ -0,0 +1,32 @@
|
||||
//
|
||||
// MBIMDataResponse.m
|
||||
// kordophoned
|
||||
//
|
||||
// Created by James Magahern on 11/20/18.
|
||||
// Copyright © 2018 James Magahern. All rights reserved.
|
||||
//
|
||||
|
||||
#import "MBIMDataResponse.h"
|
||||
|
||||
@implementation MBIMDataResponse {
|
||||
NSString *_contentType;
|
||||
}
|
||||
|
||||
- (instancetype)initWithData:(NSData *)data contentType:(NSString *)contentType
|
||||
{
|
||||
self = [super initWithData:data];
|
||||
if (self) {
|
||||
_contentType = contentType;
|
||||
}
|
||||
|
||||
return self;
|
||||
}
|
||||
|
||||
- (NSDictionary *)httpHeaders
|
||||
{
|
||||
return @{
|
||||
@"Content-Type" : _contentType
|
||||
};
|
||||
}
|
||||
|
||||
@end
|
||||
@@ -7,9 +7,9 @@
|
||||
//
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
#import <CocoaHTTPServer/HTTPDataResponse.h>
|
||||
#import "MBIMDataResponse.h"
|
||||
|
||||
@interface MBIMJSONDataResponse : HTTPDataResponse
|
||||
@interface MBIMJSONDataResponse : MBIMDataResponse
|
||||
|
||||
+ (instancetype)responseWithJSONObject:(id)object;
|
||||
|
||||
|
||||
@@ -17,16 +17,8 @@
|
||||
return nil;
|
||||
}
|
||||
|
||||
MBIMJSONDataResponse *response = [[self alloc] initWithData:data];
|
||||
|
||||
MBIMJSONDataResponse *response = [[self alloc] initWithData:data contentType:@"application/json; charset=utf-8"];
|
||||
return response;
|
||||
}
|
||||
|
||||
- (NSDictionary *)httpHeaders
|
||||
{
|
||||
return @{
|
||||
@"Content-Type" : @"application/json; charset=utf-8"
|
||||
};
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
Reference in New Issue
Block a user