Skip to content

Commit

Permalink
- Fixes files expiry time not updated when it is reused for PE File v…
Browse files Browse the repository at this point in the history
…ars.

- Fixes expired files were not deleted when downloadFiles is not called.
  • Loading branch information
nishant-clevertap committed Jul 12, 2024
1 parent 4378abe commit addab94
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 17 deletions.
2 changes: 1 addition & 1 deletion CleverTapSDK/FileDownload/CTFileDownloader.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ NS_ASSUME_NONNULL_BEGIN
- (instancetype)init NS_UNAVAILABLE;
- (instancetype)initWithConfig:(CleverTapInstanceConfig *)config;
- (void)downloadFiles:(NSArray<NSString *> *)fileURLs withCompletionBlock:(void (^ _Nullable)(NSDictionary<NSString *, NSNumber *> *status))completion;
- (BOOL)isFileAlreadyPresent:(NSString *)url;
- (BOOL)isFileAlreadyPresent:(NSString *)url andUpdateExpiryTime:(BOOL)updateExpiryTime;
- (void)clearFileAssets:(BOOL)expiredOnly;
- (nullable NSString *)fileDownloadPath:(NSString *)url;
- (nullable UIImage *)loadImageFromDisk:(NSString *)imageURL;
Expand Down
16 changes: 14 additions & 2 deletions CleverTapSDK/FileDownload/CTFileDownloader.m
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,17 @@ - (void)downloadFiles:(NSArray<NSString *> *)fileURLs withCompletionBlock:(void
}];
}

- (BOOL)isFileAlreadyPresent:(NSString *)url {
- (BOOL)isFileAlreadyPresent:(NSString *)url
andUpdateExpiryTime:(BOOL)updateExpiryTime {
NSURL *fileUrl = [NSURL URLWithString:url];
BOOL fileExists = [self.fileDownloadManager isFileAlreadyPresent:fileUrl];

// For PE File vars when fileExists returns true, we are not calling downloadFiles for existing files
// so we need to update file expiry time for those existing files.
if (fileExists && updateExpiryTime) {
[self updateFilesExpiry:@{url:@1}];
[self updateFilesExpiryInPreference];
}
return fileExists;
}

Expand All @@ -66,7 +74,7 @@ - (void)clearFileAssets:(BOOL)expiredOnly {
}

- (nullable NSString *)fileDownloadPath:(NSString *)url {
if ([self isFileAlreadyPresent:url]) {
if ([self isFileAlreadyPresent:url andUpdateExpiryTime:NO]) {
NSURL *fileURL = [NSURL URLWithString:url];
return [self.fileDownloadManager filePath:fileURL];
} else {
Expand Down Expand Up @@ -103,6 +111,10 @@ - (void)setup {
self.urlsExpiry = [NSMutableDictionary new];
}
}

// Check for expired files and delete them at every start of session.
long lastDeletedTime = [self lastDeletedTimestamp];
[self removeInactiveExpiredAssets:lastDeletedTime];
}

- (void)removeInactiveExpiredAssets:(long)lastDeletedTime {
Expand Down
6 changes: 3 additions & 3 deletions CleverTapSDK/ProductExperiences/CTVarCache.m
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ - (nullable NSString *)fileDownloadPath:(NSString *)fileURL {
}

- (BOOL)isFileAlreadyPresent:(NSString *)fileURL {
return [self.fileDownloader isFileAlreadyPresent:fileURL];
return [self.fileDownloader isFileAlreadyPresent:fileURL andUpdateExpiryTime:YES];
}

- (NSMutableArray<NSString *> *)fileURLs:(NSArray *)fileVars {
Expand All @@ -323,7 +323,7 @@ - (BOOL)isFileAlreadyPresent:(NSString *)fileURL {
if (var.fileURL) {
// If file is already present, call fileIsReady
// else download the file and call fileIsReady when downloaded
if ([self.fileDownloader isFileAlreadyPresent:var.fileURL]) {
if ([self isFileAlreadyPresent:var.fileURL]) {
[var triggerFileIsReady];
} else {
[downloadURLs addObject:var.fileURL];
Expand Down Expand Up @@ -362,7 +362,7 @@ - (void)fileVarUpdated:(CTVar *)fileVar {
return;
}

if ([self.fileDownloader isFileAlreadyPresent:url]) {
if ([self isFileAlreadyPresent:url]) {
[fileVar triggerFileIsReady];
} else {
[self.fileDownloader downloadFiles:@[url] withCompletionBlock:^(NSDictionary<NSString *,NSNumber *> * _Nonnull status) {
Expand Down
62 changes: 51 additions & 11 deletions CleverTapSDKTests/FileDownload/CTFileDownloaderTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -131,13 +131,13 @@ - (void)testDefaultExpiryTime {

- (void)testFileAlreadyPresent {
NSArray *urls = [self.helper generateFileURLStrings:2];
XCTAssertFalse([self.fileDownloader isFileAlreadyPresent:urls[0]]);
XCTAssertFalse([self.fileDownloader isFileAlreadyPresent:urls[1]]);
XCTAssertFalse([self.fileDownloader isFileAlreadyPresent:urls[0] andUpdateExpiryTime:NO]);
XCTAssertFalse([self.fileDownloader isFileAlreadyPresent:urls[1] andUpdateExpiryTime:NO]);

[self downloadFiles:@[urls[0]]];

XCTAssertTrue([self.fileDownloader isFileAlreadyPresent:urls[0]]);
XCTAssertFalse([self.fileDownloader isFileAlreadyPresent:urls[1]]);
XCTAssertTrue([self.fileDownloader isFileAlreadyPresent:urls[0] andUpdateExpiryTime:NO]);
XCTAssertFalse([self.fileDownloader isFileAlreadyPresent:urls[1] andUpdateExpiryTime:NO]);
}

- (void)testImageLoadedFromDisk {
Expand Down Expand Up @@ -424,7 +424,7 @@ - (void)testDownloadAndClearAllFileAssets {
NSMutableArray *paths = [NSMutableArray array];
for (NSString *url in urls) {
// Assert the files are downloaded
XCTAssertTrue([self.fileDownloader isFileAlreadyPresent:url]);
XCTAssertTrue([self.fileDownloader isFileAlreadyPresent:url andUpdateExpiryTime:NO]);
[paths addObject:[self.fileDownloader fileDownloadPath:url]];
}

Expand All @@ -441,7 +441,7 @@ - (void)testDownloadAndClearAllFileAssets {
}
// Assert the files no longer exist
for (NSString *url in urls) {
XCTAssertFalse([weakSelf.fileDownloader isFileAlreadyPresent:url]);
XCTAssertFalse([weakSelf.fileDownloader isFileAlreadyPresent:url andUpdateExpiryTime:NO]);
}
// Assert urlsExpiry is cleared
XCTAssertEqual(0, weakSelf.fileDownloader.urlsExpiry.count);
Expand All @@ -464,19 +464,59 @@ - (void)testFileDownloadCallbacksWhenFileIsAlreadyDownloading {

NSArray *urls = [self.helper generateFileURLStrings:3];
[self.fileDownloader downloadFiles:@[urls[0], urls[1], urls[2]] withCompletionBlock:^(NSDictionary<NSString *,id> * _Nullable status) {
XCTAssertTrue([self.fileDownloader isFileAlreadyPresent:urls[0]]);
XCTAssertTrue([self.fileDownloader isFileAlreadyPresent:urls[1]]);
XCTAssertTrue([self.fileDownloader isFileAlreadyPresent:urls[2]]);
XCTAssertTrue([self.fileDownloader isFileAlreadyPresent:urls[0] andUpdateExpiryTime:NO]);
XCTAssertTrue([self.fileDownloader isFileAlreadyPresent:urls[1] andUpdateExpiryTime:NO]);
XCTAssertTrue([self.fileDownloader isFileAlreadyPresent:urls[2] andUpdateExpiryTime:NO]);
[expectation1 fulfill];
}];
[self.fileDownloader downloadFiles:@[urls[0], urls[1]] withCompletionBlock:^(NSDictionary<NSString *,id> * _Nullable status) {
XCTAssertTrue([self.fileDownloader isFileAlreadyPresent:urls[0]]);
XCTAssertTrue([self.fileDownloader isFileAlreadyPresent:urls[1]]);
XCTAssertTrue([self.fileDownloader isFileAlreadyPresent:urls[0] andUpdateExpiryTime:NO]);
XCTAssertTrue([self.fileDownloader isFileAlreadyPresent:urls[1] andUpdateExpiryTime:NO]);
[expectation2 fulfill];
}];
[self waitForExpectations:@[expectation2, expectation1] timeout:2.0 enforceOrder:YES];
}

- (void)testFileAlreadyPresentUpdatesFileExpiryTime {
// This test checks that file expiry time is updated when file is already present
// and `isFileAlreadyPresent:` method is called with andUpdateExpiryTime YES.

// Mock currentTimeInterval
long ts = (long)[[NSDate date] timeIntervalSince1970];
self.fileDownloader.mockCurrentTimeInterval = ts;

NSString *url = [self.helper generateFileURLStrings:1][0];
[self downloadFiles:@[url]];
long expiryDate = ts + self.fileDownloader.fileExpiryTime;
// Ensure url has correct expiry set
XCTAssertEqualObjects(@(expiryDate), self.fileDownloader.urlsExpiry[url]);

self.fileDownloader.mockCurrentTimeInterval = ts + 100;
[self.fileDownloader isFileAlreadyPresent:url andUpdateExpiryTime:YES];
// Ensure url expiry is updated
XCTAssertEqualObjects(@(expiryDate + 100), self.fileDownloader.urlsExpiry[url]);
}

- (void)testFileAlreadyPresentDoesNotUpdatesFileExpiryTime {
// This test checks that file expiry time is not updated when file is already present
// and `isFileAlreadyPresent:` method is called with andUpdateExpiryTime NO.

// Mock currentTimeInterval
long ts = (long)[[NSDate date] timeIntervalSince1970];
self.fileDownloader.mockCurrentTimeInterval = ts;

NSString *url = [self.helper generateFileURLStrings:1][0];
[self downloadFiles:@[url]];
long expiryDate = ts + self.fileDownloader.fileExpiryTime;
// Ensure url has correct expiry set
XCTAssertEqualObjects(@(expiryDate), self.fileDownloader.urlsExpiry[url]);

self.fileDownloader.mockCurrentTimeInterval = ts + 100;
[self.fileDownloader isFileAlreadyPresent:url andUpdateExpiryTime:NO];
// Ensure url expiry is not updated
XCTAssertEqualObjects(@(expiryDate), self.fileDownloader.urlsExpiry[url]);
}

#pragma mark Private methods

- (void)downloadFiles:(NSArray *)urls {
Expand Down

0 comments on commit addab94

Please sign in to comment.