Skip to content

Commit ab83e54

Browse files
authored
Fixed issue where metadata wasn't properly initialized (#10356)
1 parent e190b63 commit ab83e54

File tree

3 files changed

+49
-9
lines changed

3 files changed

+49
-9
lines changed

FirebaseStorage/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# 10.0.1
2+
- [fixed] Fixed an issue where metadata passed to `putFile` was not properly initialized.
3+
14
# 10.0.0
25
- [changed] FirebaseStorage is now completely implemented in Swift. Swift-specific API improvements
36
are planned for subsequent releases. (#9963)

FirebaseStorage/Sources/StorageReference.swift

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -212,15 +212,10 @@ import Foundation
212212
open func putFile(from fileURL: URL,
213213
metadata: StorageMetadata? = nil,
214214
completion: ((_: StorageMetadata?, _: Error?) -> Void)?) -> StorageUploadTask {
215-
var putMetadata: StorageMetadata
216-
if metadata == nil {
217-
putMetadata = StorageMetadata()
218-
if let path = path.object {
219-
putMetadata.path = path
220-
putMetadata.name = (path as NSString).lastPathComponent as String
221-
}
222-
} else {
223-
putMetadata = metadata!
215+
let putMetadata: StorageMetadata = metadata ?? StorageMetadata()
216+
if let path = path.object {
217+
putMetadata.path = path
218+
putMetadata.name = (path as NSString).lastPathComponent as String
224219
}
225220
let fetcherService = storage.fetcherServiceForApp
226221
let task = StorageUploadTask(reference: self,

FirebaseStorage/Tests/ObjCIntegration/FIRStorageIntegrationTests.m

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,48 @@ - (void)testPutFile {
344344
[self waitForExpectations];
345345
}
346346

347+
- (void)testPutFileWithMetadata {
348+
XCTestExpectation *expectation = [self expectationWithDescription:@"testPutFileWithMetaData"];
349+
350+
FIRStorageReference *ref =
351+
[self.storage referenceWithPath:@"ios/public/testUnauthenticatedSimplePutFile"];
352+
353+
NSData *data = [@"Hello World" dataUsingEncoding:NSUTF8StringEncoding];
354+
NSURL *tmpDirURL = [NSURL fileURLWithPath:NSTemporaryDirectory()];
355+
NSURL *fileURL =
356+
[[tmpDirURL URLByAppendingPathComponent:@"hello"] URLByAppendingPathExtension:@"txt"];
357+
[data writeToURL:fileURL atomically:YES];
358+
359+
FIRStorageMetadata *putMetadata = [[FIRStorageMetadata alloc] init];
360+
putMetadata.contentType = @"text/plain";
361+
362+
FIRStorageUploadTask *task = [ref putFile:fileURL
363+
metadata:putMetadata
364+
completion:^(FIRStorageMetadata *metadata, NSError *error) {
365+
XCTAssertNotNil(metadata, "Metadata should not be nil");
366+
XCTAssertNil(error, "Error should be nil");
367+
}];
368+
369+
__block long uploadedBytes = -1;
370+
371+
[task observeStatus:FIRStorageTaskStatusSuccess
372+
handler:^(FIRStorageTaskSnapshot *snapshot) {
373+
XCTAssertEqualObjects([snapshot description], @"<State: Success>");
374+
[expectation fulfill];
375+
}];
376+
377+
[task observeStatus:FIRStorageTaskStatusProgress
378+
handler:^(FIRStorageTaskSnapshot *_Nonnull snapshot) {
379+
XCTAssertTrue([[snapshot description] containsString:@"State: Progress"] ||
380+
[[snapshot description] containsString:@"State: Resume"]);
381+
NSProgress *progress = snapshot.progress;
382+
XCTAssertGreaterThanOrEqual(progress.completedUnitCount, uploadedBytes);
383+
uploadedBytes = (long)progress.completedUnitCount;
384+
}];
385+
386+
[self waitForExpectations];
387+
}
388+
347389
- (void)testPutFileWithSpecialCharacters {
348390
XCTestExpectation *expectation =
349391
[self expectationWithDescription:@"testPutFileWithSpecialCharacters"];

0 commit comments

Comments
 (0)