Skip to content

Commit 0a50537

Browse files
committed
Pulling out extension logic and cleanup
Pulling out extension logic from downloadMediaAndSaveInBundle into it's own method. Cleaning up typos Creating MAX_NOTIFICATION_MEDIA_SIZE_BYTES in OneSignalCommonDefines Renaming supportedFileExtension to supportedFileExtensionFromQueryItems
1 parent 4c10985 commit 0a50537

File tree

6 files changed

+65
-45
lines changed

6 files changed

+65
-45
lines changed

iOS_SDK/OneSignalDevApp/OneSignalDevApp.xcodeproj/xcshareddata/xcschemes/OneSignalDevApp.xcscheme

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,7 @@
2626
buildConfiguration = "Debug"
2727
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
2828
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
29-
language = ""
3029
shouldUseLaunchSchemeArgsEnv = "YES">
31-
<Testables>
32-
</Testables>
3330
<MacroExpansion>
3431
<BuildableReference
3532
BuildableIdentifier = "primary"
@@ -46,12 +43,23 @@
4643
isEnabled = "YES">
4744
</AdditionalOption>
4845
</AdditionalOptions>
46+
<Testables>
47+
<TestableReference
48+
skipped = "NO">
49+
<BuildableReference
50+
BuildableIdentifier = "primary"
51+
BlueprintIdentifier = "911E2CB91E398AB3003112A4"
52+
BuildableName = "UnitTests.xctest"
53+
BlueprintName = "UnitTests"
54+
ReferencedContainer = "container:../OneSignalSDK/OneSignal.xcodeproj">
55+
</BuildableReference>
56+
</TestableReference>
57+
</Testables>
4958
</TestAction>
5059
<LaunchAction
5160
buildConfiguration = "Debug"
5261
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
5362
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
54-
language = ""
5563
launchStyle = "0"
5664
useCustomWorkingDirectory = "NO"
5765
ignoresPersistentStateOnLaunch = "NO"

iOS_SDK/OneSignalSDK/Source/NSURL+OneSignal.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,6 @@
3131

3232
- (NSString *)valueFromQueryParameter:(NSString *)parameter;
3333

34-
- (NSString*)supportedFileExtension;
34+
- (NSString *)supportedFileExtensionFromQueryItems;
3535

3636
@end

iOS_SDK/OneSignalSDK/Source/NSURL+OneSignal.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ - (NSString *)valueFromQueryParameter:(NSString *)parameter {
3939
return nil;
4040
}
4141

42-
- (NSString*)supportedFileExtension {
42+
- (NSString *)supportedFileExtensionFromQueryItems {
4343
NSURLComponents *components = [NSURLComponents componentsWithURL:self resolvingAgainstBaseURL:false];
4444

4545
for (NSURLQueryItem *item in [components.queryItems reverseObjectEnumerator]) {

iOS_SDK/OneSignalSDK/Source/OneSignalCommonDefines.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,4 +238,6 @@ typedef enum {GET, POST, HEAD, PUT, DELETE, OPTIONS, CONNECT, TRACE} HTTPMethod;
238238
// variance and floating-point error.
239239
#define OS_ROUGHLY_EQUAL(left, right) (fabs(left - right) < 0.03)
240240

241+
#define MAX_NOTIFICATION_MEDIA_SIZE_BYTES 50000000
242+
241243
#endif /* OneSignalCommonDefines_h */

iOS_SDK/OneSignalSDK/Source/OneSignalHelper.m

Lines changed: 48 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,10 @@ -(void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataT
7979
[outputHandle writeData:data];
8080
}
8181

82-
-(void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveResponse:(NSURLResponse *)aResponse completionHandler:(void (^)(NSURLSessionResponseDisposition))completionHandler {
82+
- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveResponse:(NSURLResponse *)aResponse completionHandler:(void (^)(NSURLSessionResponseDisposition))completionHandler {
8383
response = aResponse;
8484
long long expectedLength = response.expectedContentLength;
85-
if (expectedLength > 50000000) { //Enforcing 50 mb limit on media before downloading
85+
if (expectedLength > MAX_NOTIFICATION_MEDIA_SIZE_BYTES) { //Enforcing 50 mb limit on media before downloading
8686
completionHandler(NSURLSessionResponseCancel);
8787
return;
8888
}
@@ -782,24 +782,14 @@ + (void)addNotificationRequest:(OSNotificationPayload*)payload
782782
/*
783783
Synchroneously downloads an attachment
784784
On success returns bundle resource name, otherwise returns nil
785-
The preference order for file type determination is as follows:
786-
1. File extension in the actual URL
787-
2. MIME type
788-
3. URL Query parameter called 'filename', such as test.jpg. The SDK will extract the file extension from it
789785
*/
790-
+ (NSString*)downloadMediaAndSaveInBundle:(NSString*)urlString {
786+
+ (NSString *)downloadMediaAndSaveInBundle:(NSString*)urlString {
791787

792788
let url = [NSURL URLWithString:urlString];
793789

794-
//Try to get extension from the filname parameter
795-
NSString* extension = [[[NSURL URLWithString:urlString] valueFromQueryParameter:@"filename"]
796-
supportedFileExtension];
797-
798790
//Download the file
799791
var name = [self randomStringWithLength:10];
800792

801-
if (extension)
802-
name = [name stringByAppendingString:[NSString stringWithFormat:@".%@", extension]];
803793
NSArray* paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
804794
NSString* filePath = [paths[0] stringByAppendingPathComponent:name];
805795

@@ -813,31 +803,16 @@ + (NSString*)downloadMediaAndSaveInBundle:(NSString*)urlString {
813803
[OneSignal onesignal_Log:ONE_S_LL_ERROR message:[NSString stringWithFormat:@"Encountered an error while attempting to download file with URL: %@", error]];
814804
return nil;
815805
}
816-
if (!extension) {
817-
NSString *newExtension;
818-
//Use the MIME type for the extension if one wasn't provided in the filename parameter
819-
if (mimeType != nil && ![mimeType isEqualToString:@""]) {
820-
newExtension = mimeType.fileExtensionForMimeType;
821-
}
822-
823-
//Try using url.pathExtension
824-
if (!newExtension || ![ONESIGNAL_SUPPORTED_ATTACHMENT_TYPES containsObject:newExtension]) {
825-
newExtension = url.pathExtension;
826-
}
827-
828-
//Try getting an extension from the query
829-
if (!newExtension || ![ONESIGNAL_SUPPORTED_ATTACHMENT_TYPES containsObject:newExtension]) {
830-
newExtension = url.supportedFileExtension;
831-
if (!newExtension || [newExtension isEqualToString:@""])
832-
return nil;
833-
}
834-
835-
name = [NSString stringWithFormat:@"%@.%@", name, newExtension];
836-
837-
let newPath = [paths[0] stringByAppendingPathComponent:[NSString stringWithFormat:@"%@", name]];
838-
839-
[[NSFileManager defaultManager] moveItemAtPath:filePath toPath:newPath error:&error];
840-
}
806+
807+
NSString *extension = [OneSignalHelper getSupportedFileExtensionFromURL:url mimeType:mimeType];
808+
if (!extension || [extension isEqualToString:@""])
809+
return nil;
810+
811+
name = [NSString stringWithFormat:@"%@.%@", name, extension];
812+
813+
let newPath = [paths[0] stringByAppendingPathComponent:[NSString stringWithFormat:@"%@", name]];
814+
815+
[[NSFileManager defaultManager] moveItemAtPath:filePath toPath:newPath error:&error];
841816

842817
if (error) {
843818
[OneSignal onesignal_Log:ONE_S_LL_ERROR message:[NSString stringWithFormat:@"Encountered an error while attempting to download file with URL: %@", error]];
@@ -864,6 +839,41 @@ + (NSString*)downloadMediaAndSaveInBundle:(NSString*)urlString {
864839
}
865840
}
866841

842+
843+
/*
844+
The preference order for file type determination is as follows:
845+
1. URL Query parameter called 'filename', such as test.jpg. The SDK will extract the file extension from it
846+
2. MIME type
847+
3. File extension in the actual URL
848+
4. A file extension extracted by searching through all URL Query parameters
849+
*/
850+
+ (NSString *)getSupportedFileExtensionFromURL:(NSURL *)url mimeType:(NSString *)mimeType {
851+
//Try to get extension from the filename parameter
852+
NSString* extension = [[url valueFromQueryParameter:@"filename"]
853+
supportedFileExtension];
854+
if (extension && [ONESIGNAL_SUPPORTED_ATTACHMENT_TYPES containsObject:extension]) {
855+
return extension;
856+
}
857+
//Use the MIME type for the extension
858+
if (mimeType != nil && ![mimeType isEqualToString:@""]) {
859+
extension = mimeType.fileExtensionForMimeType;
860+
if (extension && [ONESIGNAL_SUPPORTED_ATTACHMENT_TYPES containsObject:extension]) {
861+
return extension;
862+
}
863+
}
864+
//Try using url.pathExtension
865+
extension = url.pathExtension;
866+
if (extension && [ONESIGNAL_SUPPORTED_ATTACHMENT_TYPES containsObject:extension]) {
867+
return extension;
868+
}
869+
//Try getting an extension from the query
870+
extension = url.supportedFileExtensionFromQueryItems;
871+
if (extension && [ONESIGNAL_SUPPORTED_ATTACHMENT_TYPES containsObject:extension]) {
872+
return extension;
873+
}
874+
return nil;
875+
}
876+
867877
// TODO: Add back after testing
868878
+ (void)clearCachedMedia {
869879
/*

iOS_SDK/OneSignalSDK/UnitTests/UnitTests.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2165,7 +2165,7 @@ - (void)testFileExtensionPrioritizesFileNameParameter {
21652165
XCTAssertTrue([downloadedGifFilename.supportedFileExtension isEqualToString:@"png"]);
21662166
}
21672167

2168-
- (void)testExtractFileExtensionFromAnyParamter {
2168+
- (void)testExtractFileExtensionFromAnyParameter {
21692169
//test to make sure the fallback of parsing all parameters for a file type works correctly
21702170
//NSURLSessionOverrider returns an unallowed extension (heic) for this URL to test the fallback
21712171
id pngFormat = [self exampleNotificationJSONWithMediaURL:@"http://domain.com/secondFile?file=test.png&media=image&type=.fakeextension"];

0 commit comments

Comments
 (0)