Private
Public Access
1
0
Files
Kordophone/server/kordophone/Bridge/Operations/MBIMUploadAttachmentOperation.m
James Magahern 7fe2701272 Add 'server/' from commit '800090542d91beae40bc81fc41b67ba61c47da77'
git-subtree-dir: server
git-subtree-mainline: 6a4054c15a
git-subtree-split: 800090542d
2025-09-06 19:36:27 -07:00

74 lines
2.4 KiB
Objective-C

//
// MBIMUploadAttachmentOperation.m
// kordophoned
//
// Created by James Magahern on 1/16/19.
// Copyright © 2019 James Magahern. All rights reserved.
//
#import "MBIMUploadAttachmentOperation.h"
#import "MBIMDataResponse.h"
#import "IMCore_ClassDump.h"
@implementation MBIMUploadAttachmentOperation
+ (void)load { [super load]; }
+ (NSString *)endpointName
{
return @"uploadAttachment";
}
- (void)main
{
NSObject<HTTPResponse> *response = nil;
do {
NSString *filename = [self valueForQueryItemWithName:@"filename"];
if ([filename length] == 0) {
MBIMLogInfo(@"No filename provided");
response = [[HTTPErrorResponse alloc] initWithErrorCode:500];
break;
}
NSData *attachmentData = self.requestBodyData;
if ([attachmentData length] == 0) {
MBIMLogInfo(@"No attachment data in request");
response = [[HTTPErrorResponse alloc] initWithErrorCode:500];
break;
}
// Sanitize filename
NSCharacterSet *dotCharacter = [NSCharacterSet characterSetWithCharactersInString:@"."];
NSCharacterSet *illegalFileNameCharacters = [NSCharacterSet characterSetWithCharactersInString:@"/\\?%*|\"<>"];
NSString *sanitizedFilename = [[[filename componentsSeparatedByCharactersInSet:illegalFileNameCharacters]
componentsJoinedByString:@"-"]
stringByTrimmingCharactersInSet:dotCharacter];
NSString *localPath = [NSTemporaryDirectory() stringByAppendingPathComponent:sanitizedFilename];
NSURL *localURL = [NSURL fileURLWithPath:localPath];
BOOL success = [attachmentData writeToURL:localURL atomically:NO];
if (!success) {
MBIMLogInfo(@"Error writing attachment to temporary directory");
response = [[HTTPErrorResponse alloc] initWithErrorCode:500];
break;
}
NSString *guid = [[IMFileTransferCenter sharedInstance] guidForNewOutgoingTransferWithLocalURL:localURL];
if (!guid) {
MBIMLogInfo(@"There was some problem shuttling the file to IMCore");
response = [[HTTPErrorResponse alloc] initWithErrorCode:500];
break;
}
NSDictionary *responseDict = @{
@"fileTransferGUID" : guid
};
response = [MBIMJSONDataResponse responseWithJSONObject:responseDict];
} while (0);
self.serverCompletionBlock(response);
}
@end