Skip to content

Commit 071902b

Browse files
author
Nathan Fallet
authored
Adding Custom Time to FirebaseStorage (#9576)
1 parent 8af6c20 commit 071902b

File tree

9 files changed

+54
-0
lines changed

9 files changed

+54
-0
lines changed

FirebaseStorage/Sources/StorageMetadata.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,18 @@ import FirebaseStorageInternal
9191
}
9292
}
9393

94+
/**
95+
* Custom-Time of the object data.
96+
*/
97+
@objc public var customTime: Date? {
98+
get {
99+
return impl.customTime
100+
}
101+
set(newValue) {
102+
impl.customTime = newValue
103+
}
104+
}
105+
94106
/**
95107
* MD5 hash of the data; encoded using base64.
96108
*/

FirebaseStorage/Tests/Integration/StorageAsyncAwait.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,12 +276,14 @@ import XCTest
276276

277277
private func assertMetadata(actualMetadata: StorageMetadata,
278278
expectedContentType: String,
279+
expectedCustomTime: Date,
279280
expectedCustomMetadata: [String: String]) {
280281
XCTAssertEqual(actualMetadata.cacheControl, "cache-control")
281282
XCTAssertEqual(actualMetadata.contentDisposition, "content-disposition")
282283
XCTAssertEqual(actualMetadata.contentEncoding, "gzip")
283284
XCTAssertEqual(actualMetadata.contentLanguage, "de")
284285
XCTAssertEqual(actualMetadata.contentType, expectedContentType)
286+
XCTAssertEqual(actualMetadata.customTime, expectedCustomTime)
285287
XCTAssertEqual(actualMetadata.md5Hash?.count, 24)
286288
for (key, value) in expectedCustomMetadata {
287289
XCTAssertEqual(actualMetadata.customMetadata![key], value)
@@ -294,6 +296,7 @@ import XCTest
294296
XCTAssertEqual(actualMetadata.contentEncoding, "identity")
295297
XCTAssertNil(actualMetadata.contentLanguage)
296298
XCTAssertNil(actualMetadata.contentType)
299+
XCTAssertNil(actualMetadata.customTime)
297300
XCTAssertEqual(actualMetadata.md5Hash?.count, 24)
298301
XCTAssertNil(actualMetadata.customMetadata)
299302
}
@@ -307,26 +310,31 @@ import XCTest
307310
metadata.contentEncoding = "gzip"
308311
metadata.contentLanguage = "de"
309312
metadata.contentType = "content-type-a"
313+
metadata.customTime = Date(timeIntervalSince1970: 0)
310314
metadata.customMetadata = ["a": "b"]
311315

312316
let updatedMetadata = try await ref.updateMetadata(metadata)
313317
assertMetadata(actualMetadata: updatedMetadata,
314318
expectedContentType: "content-type-a",
319+
expectedCustomTime: Date(timeIntervalSince1970: 0),
315320
expectedCustomMetadata: ["a": "b"])
316321

317322
let metadata2 = updatedMetadata
318323
metadata2.contentType = "content-type-b"
324+
metadata.customTime = Date(timeIntervalSince1970: 100)
319325
metadata2.customMetadata = ["a": "b", "c": "d"]
320326

321327
let metadata3 = try await ref.updateMetadata(metadata2)
322328
assertMetadata(actualMetadata: metadata3,
323329
expectedContentType: "content-type-b",
330+
expectedCustomTime: Date(timeIntervalSince1970: 100),
324331
expectedCustomMetadata: ["a": "b", "c": "d"])
325332
metadata.cacheControl = nil
326333
metadata.contentDisposition = nil
327334
metadata.contentEncoding = nil
328335
metadata.contentLanguage = nil
329336
metadata.contentType = nil
337+
metadata.customTime = nil
330338
metadata.customMetadata = nil
331339
let metadata4 = try await ref.updateMetadata(metadata)
332340
XCTAssertNotNil(metadata4)

FirebaseStorage/Tests/Integration/StorageIntegration.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,12 +417,14 @@ class StorageResultTests: StorageIntegrationCommon {
417417

418418
private func assertMetadata(actualMetadata: StorageMetadata,
419419
expectedContentType: String,
420+
expectedCustomTime: Date,
420421
expectedCustomMetadata: [String: String]) {
421422
XCTAssertEqual(actualMetadata.cacheControl, "cache-control")
422423
XCTAssertEqual(actualMetadata.contentDisposition, "content-disposition")
423424
XCTAssertEqual(actualMetadata.contentEncoding, "gzip")
424425
XCTAssertEqual(actualMetadata.contentLanguage, "de")
425426
XCTAssertEqual(actualMetadata.contentType, expectedContentType)
427+
XCTAssertEqual(actualMetadata.customTime, expectedCustomTime)
426428
XCTAssertEqual(actualMetadata.md5Hash?.count, 24)
427429
for (key, value) in expectedCustomMetadata {
428430
XCTAssertEqual(actualMetadata.customMetadata![key], value)
@@ -435,6 +437,7 @@ class StorageResultTests: StorageIntegrationCommon {
435437
XCTAssertEqual(actualMetadata.contentEncoding, "identity")
436438
XCTAssertNil(actualMetadata.contentLanguage)
437439
XCTAssertNil(actualMetadata.contentType)
440+
XCTAssertNil(actualMetadata.customTime)
438441
XCTAssertEqual(actualMetadata.md5Hash?.count, 24)
439442
XCTAssertNil(actualMetadata.customMetadata)
440443
}
@@ -449,6 +452,7 @@ class StorageResultTests: StorageIntegrationCommon {
449452
metadata.contentEncoding = "gzip"
450453
metadata.contentLanguage = "de"
451454
metadata.contentType = "content-type-a"
455+
metadata.customTime = Date(timeIntervalSince1970: 0)
452456
metadata.customMetadata = ["a": "b"]
453457

454458
ref.updateMetadata(metadata) { updatedMetadata, error in
@@ -460,23 +464,27 @@ class StorageResultTests: StorageIntegrationCommon {
460464
}
461465
self.assertMetadata(actualMetadata: updatedMetadata,
462466
expectedContentType: "content-type-a",
467+
expectedCustomTime: Date(timeIntervalSince1970: 0),
463468
expectedCustomMetadata: ["a": "b"])
464469

465470
let metadata = updatedMetadata
466471
metadata.contentType = "content-type-b"
472+
metadata.customTime = Date(timeIntervalSince1970: 100)
467473
metadata.customMetadata = ["a": "b", "c": "d"]
468474

469475
ref.updateMetadata(metadata) { result in
470476
switch result {
471477
case let .success(updatedMetadata):
472478
self.assertMetadata(actualMetadata: updatedMetadata,
473479
expectedContentType: "content-type-b",
480+
expectedCustomTime: Date(timeIntervalSince1970: 100),
474481
expectedCustomMetadata: ["a": "b", "c": "d"])
475482
metadata.cacheControl = nil
476483
metadata.contentDisposition = nil
477484
metadata.contentEncoding = nil
478485
metadata.contentLanguage = nil
479486
metadata.contentType = nil
487+
metadata.customTime = nil
480488
metadata.customMetadata = nil
481489
ref.updateMetadata(metadata) { result in
482490
self.assertResultSuccess(result)

FirebaseStorageInternal/Sources/FIRStorageConstants.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
NSString *const kFIRStorageMetadataContentEncoding = @"contentEncoding";
6565
NSString *const kFIRStorageMetadataContentLanguage = @"contentLanguage";
6666
NSString *const kFIRStorageMetadataContentType = @"contentType";
67+
NSString *const kFIRStorageMetadataCustomTime = @"customTime";
6768
NSString *const kFIRStorageMetadataCustomMetadata = @"metadata";
6869
NSString *const kFIRStorageMetadataSize = @"size";
6970
NSString *const kFIRStorageMetadataGeneration = @"generation";

FirebaseStorageInternal/Sources/FIRStorageConstants_Private.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ FOUNDATION_EXPORT NSString *const kFIRStorageMetadataContentDisposition;
5959
FOUNDATION_EXPORT NSString *const kFIRStorageMetadataContentEncoding;
6060
FOUNDATION_EXPORT NSString *const kFIRStorageMetadataContentLanguage;
6161
FOUNDATION_EXPORT NSString *const kFIRStorageMetadataContentType;
62+
FOUNDATION_EXPORT NSString *const kFIRStorageMetadataCustomTime;
6263
FOUNDATION_EXPORT NSString *const kFIRStorageMetadataCustomMetadata;
6364
FOUNDATION_EXPORT NSString *const kFIRStorageMetadataSize;
6465
FOUNDATION_EXPORT NSString *const kFIRStorageMetadataGeneration;

FirebaseStorageInternal/Sources/FIRStorageMetadata.m

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ - (instancetype)initWithDictionary:(NSDictionary *)dictionary {
4040
_contentEncoding = dictionary[kFIRStorageMetadataContentEncoding];
4141
_contentLanguage = dictionary[kFIRStorageMetadataContentLanguage];
4242
_contentType = dictionary[kFIRStorageMetadataContentType];
43+
_customTime = [self dateFromRFC3339String:dictionary[kFIRStorageMetadataCustomTime]];
4344
_customMetadata = dictionary[kFIRStorageMetadataCustomMetadata];
4445
_size = [dictionary[kFIRStorageMetadataSize] longLongValue];
4546
_generation = [dictionary[kFIRStorageMetadataGeneration] longLongValue];
@@ -119,6 +120,10 @@ - (NSDictionary *)dictionaryRepresentation {
119120
metadataDictionary[kFIRStorageMetadataContentType] = _contentType;
120121
}
121122

123+
if (_customTime) {
124+
metadataDictionary[kFIRStorageMetadataCustomTime] = [self RFC3339StringFromDate:_customTime];
125+
}
126+
122127
if (_md5Hash) {
123128
metadataDictionary[kFIRStorageMetadataMd5Hash] = _md5Hash;
124129
}

FirebaseStorageInternal/Sources/Public/FirebaseStorageInternal/FIRStorageMetadata.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,11 @@ NS_ASSUME_NONNULL_BEGIN
5959
*/
6060
@property(copy, nonatomic, nullable) NSString *contentType;
6161

62+
/**
63+
* Custom-Time of the object data.
64+
*/
65+
@property(copy, nonatomic, nullable) NSDate *customTime;
66+
6267
/**
6368
* MD5 hash of the data; encoded using base64.
6469
*/

FirebaseStorageInternal/Tests/Integration/FIRStorageIntegrationTests.m

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -580,12 +580,14 @@ - (void)testCancelDownload {
580580

581581
- (void)assertMetadata:(FIRStorageMetadata *)actualMetadata
582582
contentType:(NSString *)expectedContentType
583+
customTime:(NSDate *)expectedCustomTime
583584
customMetadata:(NSDictionary *)expectedCustomMetadata {
584585
XCTAssertEqualObjects(actualMetadata.cacheControl, @"cache-control");
585586
XCTAssertEqualObjects(actualMetadata.contentDisposition, @"content-disposition");
586587
XCTAssertEqualObjects(actualMetadata.contentEncoding, @"gzip");
587588
XCTAssertEqualObjects(actualMetadata.contentLanguage, @"de");
588589
XCTAssertEqualObjects(actualMetadata.contentType, expectedContentType);
590+
XCTAssertEqualObjects(actualMetadata.customTime, expectedCustomTime);
589591
XCTAssertTrue([actualMetadata.md5Hash length] == 24);
590592
for (NSString *key in expectedCustomMetadata) {
591593
XCTAssertEqualObjects([actualMetadata.customMetadata objectForKey:key],
@@ -599,6 +601,7 @@ - (void)assertMetadataNil:(FIRStorageMetadata *)actualMetadata {
599601
XCTAssertEqualObjects(actualMetadata.contentEncoding, @"identity");
600602
XCTAssertNil(actualMetadata.contentLanguage);
601603
XCTAssertNil(actualMetadata.contentType);
604+
XCTAssertNil(actualMetadata.customTime);
602605
XCTAssertTrue([actualMetadata.md5Hash length] == 24);
603606
XCTAssertNil(actualMetadata.customMetadata);
604607
}
@@ -615,25 +618,29 @@ - (void)testUpdateMetadata {
615618
metadata.contentEncoding = @"gzip";
616619
metadata.contentLanguage = @"de";
617620
metadata.contentType = @"content-type-a";
621+
metadata.customTime = [NSDate dateWithTimeIntervalSince1970:0];
618622
metadata.customMetadata = @{@"a" : @"b"};
619623

620624
[ref updateMetadata:metadata
621625
completion:^(FIRStorageMetadata *updatedMetadata, NSError *error) {
622626
XCTAssertNil(error);
623627
[self assertMetadata:updatedMetadata
624628
contentType:@"content-type-a"
629+
customTime:[NSDate dateWithTimeIntervalSince1970:0]
625630
customMetadata:@{@"a" : @"b"}];
626631

627632
// Update a subset of the metadata using the existing object.
628633
FIRStorageMetadata *metadata = updatedMetadata;
629634
metadata.contentType = @"content-type-b";
635+
metadata.customTime = [NSDate dateWithTimeIntervalSince1970:100];
630636
metadata.customMetadata = @{@"a" : @"b", @"c" : @"d"};
631637

632638
[ref updateMetadata:metadata
633639
completion:^(FIRStorageMetadata *updatedMetadata, NSError *error) {
634640
XCTAssertNil(error);
635641
[self assertMetadata:updatedMetadata
636642
contentType:@"content-type-b"
643+
customTime:[NSDate dateWithTimeIntervalSince1970:100]
637644
customMetadata:@{@"a" : @"b", @"c" : @"d"}];
638645

639646
// Clear all metadata.
@@ -643,6 +650,7 @@ - (void)testUpdateMetadata {
643650
metadata.contentEncoding = nil;
644651
metadata.contentLanguage = nil;
645652
metadata.contentType = nil;
653+
metadata.customTime = nil;
646654
metadata.customMetadata = [NSDictionary dictionary];
647655

648656
[ref updateMetadata:metadata

FirebaseStorageInternal/Tests/Unit/FIRStorageMetadataTests.m

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ - (void)testInitializeFullMetadata {
4242
kFIRStorageMetadataContentEncoding : @"gzip",
4343
kFIRStorageMetadataContentLanguage : @"en-us",
4444
kFIRStorageMetadataContentType : @"application/octet-stream",
45+
kFIRStorageMetadataCustomTime : @"2016-03-01T20:16:01.673Z",
4546
kFIRStorageMetadataCustomMetadata : @{@"foo" : @{@"bar" : @"baz"}},
4647
kFIRStorageMetadataGeneration : @"12345",
4748
kFIRStorageMetadataMetageneration : @"67890",
@@ -59,6 +60,8 @@ - (void)testInitializeFullMetadata {
5960
metaDict[kFIRStorageMetadataContentDisposition]);
6061
XCTAssertEqualObjects(metadata.contentEncoding, metaDict[kFIRStorageMetadataContentEncoding], );
6162
XCTAssertEqualObjects(metadata.contentType, metaDict[kFIRStorageMetadataContentType]);
63+
XCTAssertEqualObjects([metadata RFC3339StringFromDate:metadata.customTime],
64+
metaDict[kFIRStorageMetadataCustomTime]);
6265
XCTAssertEqualObjects(metadata.customMetadata, metaDict[kFIRStorageMetadataCustomMetadata]);
6366
XCTAssertEqualObjects(metadata.md5Hash, metaDict[kFIRStorageMetadataMd5Hash]);
6467
NSString *generation = [NSString stringWithFormat:@"%lld", metadata.generation];
@@ -82,6 +85,7 @@ - (void)testDictionaryRepresentation {
8285
kFIRStorageMetadataContentEncoding : @"gzip",
8386
kFIRStorageMetadataContentLanguage : @"en-us",
8487
kFIRStorageMetadataContentType : @"application/octet-stream",
88+
kFIRStorageMetadataCustomTime : @"2016-03-01T20:16:01.673Z",
8589
kFIRStorageMetadataCustomMetadata : @{@"foo" : @{@"bar" : @"baz"}},
8690
kFIRStorageMetadataGeneration : @"12345",
8791
kFIRStorageMetadataMetageneration : @"67890",
@@ -106,6 +110,8 @@ - (void)testDictionaryRepresentation {
106110
metaDict[kFIRStorageMetadataContentLanguage]);
107111
XCTAssertEqualObjects(dictRepresentation[kFIRStorageMetadataContentType],
108112
metaDict[kFIRStorageMetadataContentType]);
113+
XCTAssertEqualObjects(dictRepresentation[kFIRStorageMetadataCustomTime],
114+
metaDict[kFIRStorageMetadataCustomTime]);
109115
XCTAssertEqualObjects(dictRepresentation[kFIRStorageMetadataCustomMetadata],
110116
metaDict[kFIRStorageMetadataCustomMetadata]);
111117
XCTAssertEqualObjects(dictRepresentation[kFIRStorageMetadataDownloadTokens],

0 commit comments

Comments
 (0)