Skip to content

Commit

Permalink
[INJIMOB-867] - remove all files including unsynced files and get lat…
Browse files Browse the repository at this point in the history
…est file based on date (#1281)

Signed-off-by: Sreenadh S <32409698+sree96@users.noreply.github.com>
  • Loading branch information
sree96 authored Feb 21, 2024
1 parent 854d8e2 commit f9e3e87
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions shared/CloudBackupAndRestoreUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class Cloud {
'https://www.googleapis.com/auth/drive.file',
];
private static readonly BACKUP_FILE_REG_EXP = /backup_[0-9]*.zip$/g;
private static readonly ALL_BACKUP_FILE_REG_EXP = /backup_[0-9]*.zip/g;
private static readonly UNSYNCED_BACKUP_FILE_REG_EXP =
/backup_[0-9]*.zip.icloud/g;
private static readonly RETRY_SLEEP_TIME = 5000;
Expand Down Expand Up @@ -64,6 +65,15 @@ class Cloud {
return await CloudStorage.readdir(`/`, CloudStorageScope.AppData);
};

private static getLatestFileName = (allFiles: string[]): string => {
const sortedFiles = allFiles.sort((a, b) => {
const dateA = new Date(Number(a.split('.')[0].split('_')[1]));
const dateB = new Date(Number(b.split('.')[0].split('_')[1]));
return dateB > dateA ? 1 : dateB < dateA ? -1 : 0;
});
return sortedFiles[0];
};

private static async syncBackupFiles() {
const isSyncDone = await this.downloadUnSyncedBackupFiles();
if (isSyncDone) return;
Expand Down Expand Up @@ -202,7 +212,7 @@ class Cloud {
if (availableBackupFilesInCloud.length === 0) {
throw new Error(this.NO_BACKUP_FILE);
}
cloudFileName = availableBackupFilesInCloud[0];
cloudFileName = this.getLatestFileName(availableBackupFilesInCloud);
}
const {birthtimeMs: creationTime, size} = await CloudStorage.stat(
cloudFileName,
Expand All @@ -218,7 +228,7 @@ class Cloud {
static async removeOldDriveBackupFiles(fileName: string) {
const toBeRemovedFiles = (await this.getBackupFilesList())
.filter(file => file !== fileName)
.filter(file => file.match(this.BACKUP_FILE_REG_EXP));
.filter(file => file.match(this.ALL_BACKUP_FILE_REG_EXP));
for (const oldFileName of toBeRemovedFiles) {
await CloudStorage.unlink(`/${oldFileName}`);
}
Expand Down Expand Up @@ -323,7 +333,9 @@ class Cloud {
throw new Error(Cloud.NO_BACKUP_FILE);
}

const fileName = `/${availableBackupFilesInCloud[0]}`;
const fileName = `/${this.getLatestFileName(
availableBackupFilesInCloud,
)}`;
const fileContent = await CloudStorage.readFile(
fileName,
CloudStorageScope.AppData,
Expand Down

0 comments on commit f9e3e87

Please sign in to comment.