Ver Fonte

Merge remote-tracking branch 'origin/master' into pb-merge-firebase7

Paul Beusterien há 5 anos atrás
pai
commit
fa5ff62138

+ 1 - 0
FirebaseMessaging/CHANGELOG.md

@@ -1,5 +1,6 @@
 # unreleased -- v.7.0.0
 - [changed] Remove the deprecated FCM direct channel API and Upstream send API. (#6430)
+- [fixed] Fixed an issue that downloading an image failed when there's no extension in the file name but MIME type is set. (#6590)
 
 # 2020-09 -- v.4.7.1
 - [added] InstanceID is deprecated, add macro to suppress deprecation warning. (#6585)

+ 15 - 2
FirebaseMessaging/Sources/FIRMessagingExtensionHelper.m

@@ -21,6 +21,8 @@
 
 static NSString *const kPayloadOptionsName = @"fcm_options";
 static NSString *const kPayloadOptionsImageURLName = @"image";
+static NSString *const kNoExtension = @"";
+static NSString *const kImagePathPrefix = @"image/";
 
 @interface FIRMessagingExtensionHelper ()
 @property(nonatomic, strong) void (^contentHandler)(UNNotificationContent *contentToDeliver);
@@ -62,6 +64,18 @@ static NSString *const kPayloadOptionsImageURLName = @"image";
 }
 
 #if TARGET_OS_IOS || TARGET_OS_OSX || TARGET_OS_WATCH
+- (NSString *)fileExtensionForResponse:(NSURLResponse *)response {
+  NSString *suggestedPathExtension = [response.suggestedFilename pathExtension];
+  if (suggestedPathExtension.length > 0) {
+    return [NSString stringWithFormat:@".%@", suggestedPathExtension];
+  }
+  if ([response.MIMEType containsString:kImagePathPrefix]) {
+    return [response.MIMEType stringByReplacingOccurrencesOfString:kImagePathPrefix
+                                                        withString:@"."];
+  }
+  return kNoExtension;
+}
+
 - (void)loadAttachmentForURL:(NSURL *)attachmentURL
            completionHandler:(void (^)(UNNotificationAttachment *))completionHandler {
   __block UNNotificationAttachment *attachment = nil;
@@ -80,8 +94,7 @@ static NSString *const kPayloadOptionsImageURLName = @"image";
           }
 
           NSFileManager *fileManager = [NSFileManager defaultManager];
-          NSString *fileExtension =
-              [NSString stringWithFormat:@".%@", [response.suggestedFilename pathExtension]];
+          NSString *fileExtension = [self fileExtensionForResponse:response];
           NSURL *localURL = [NSURL
               fileURLWithPath:[temporaryFileLocation.path stringByAppendingString:fileExtension]];
           [fileManager moveItemAtURL:temporaryFileLocation toURL:localURL error:&error];

+ 36 - 0
FirebaseMessaging/Tests/UnitTests/FIRMessagingExtensionHelperTest.m

@@ -34,10 +34,12 @@ static NSString *const kValidImageURL =
 
 - (void)loadAttachmentForURL:(NSURL *)attachmentURL
            completionHandler:(void (^)(UNNotificationAttachment *))completionHandler;
+- (NSString *)fileExtensionForResponse:(NSURLResponse *)response;
 @end
 
 @interface FIRMessagingExtensionHelperTest : XCTestCase {
   id _mockExtensionHelper;
+  id _mockURLResponse;
 }
 @end
 
@@ -48,6 +50,7 @@ static NSString *const kValidImageURL =
   if (@available(macOS 10.14, iOS 10.0, *)) {
     FIRMessagingExtensionHelper *extensionHelper = [FIRMessaging extensionHelper];
     _mockExtensionHelper = OCMPartialMock(extensionHelper);
+    _mockURLResponse = OCMClassMock([NSURLResponse class]);
   } else {
     // Fallback on earlier versions
   }
@@ -55,6 +58,7 @@ static NSString *const kValidImageURL =
 
 - (void)tearDown {
   [_mockExtensionHelper stopMocking];
+  [_mockURLResponse stopMocking];
 }
 
 #ifdef COCOAPODS
@@ -112,5 +116,37 @@ static NSString *const kValidImageURL =
   }
 }
 
+- (void)testModifyNotificationWithValidPayloadDataNoMimeType {
+  if (@available(macOS 10.14, iOS 10.0, *)) {
+    NSString *const kValidTestURL = @"test.jpg";
+    NSString *const kValidTestExtension = @".jpg";
+    OCMStub([_mockURLResponse suggestedFilename]).andReturn(kValidTestURL);
+    NSString *const extension = [_mockExtensionHelper fileExtensionForResponse:_mockURLResponse];
+    XCTAssertTrue([extension isEqualToString:kValidTestExtension]);
+  }
+}
+
+- (void)testModifyNotificationWithInvalidPayloadDataInvalidMimeType {
+  if (@available(macOS 10.14, iOS 10.0, *)) {
+    NSString *const kInvalidTestURL = @"test";
+    NSString *const kInvalidTestExtension = @"";
+    OCMStub([_mockURLResponse suggestedFilename]).andReturn(kInvalidTestURL);
+    OCMStub([_mockURLResponse MIMEType]).andReturn(nil);
+    NSString *const extension = [_mockExtensionHelper fileExtensionForResponse:_mockURLResponse];
+    XCTAssertTrue([extension isEqualToString:kInvalidTestExtension]);
+  }
+}
+
+- (void)testModifyNotificationWithInvalidPayloadDataValidMimeType {
+  if (@available(macOS 10.14, iOS 10.0, *)) {
+    NSString *const kValidMIMETypeTestURL = @"test";
+    NSString *const kValidMIMETypeTestMIMEType = @"image/jpeg";
+    NSString *const kValidMIMETypeTestExtension = @".jpeg";
+    OCMStub([_mockURLResponse suggestedFilename]).andReturn(kValidMIMETypeTestURL);
+    OCMStub([_mockURLResponse MIMEType]).andReturn(kValidMIMETypeTestMIMEType);
+    NSString *const extension = [_mockExtensionHelper fileExtensionForResponse:_mockURLResponse];
+    XCTAssertTrue([extension isEqualToString:kValidMIMETypeTestExtension]);
+  }
+}
 @end
 #endif