Skip to content

Commit

Permalink
fix: google drive initial import if update is encoded
Browse files Browse the repository at this point in the history
  • Loading branch information
johannesjo committed Oct 8, 2019
1 parent 60510dc commit 37c8880
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 12 deletions.
5 changes: 3 additions & 2 deletions src/app/core/persistence/persistence.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -315,12 +315,13 @@ export class PersistenceService {
saveLastActive(date: string = new Date().toString()) {
// TODO refactor to timestamp
// console.log('Save LastAct', date);
saveToLs(LS_LAST_ACTIVE, date);
localStorage.setItem(LS_LAST_ACTIVE, date);
}

getLastActive(): string {
// TODO refactor to timestamp
return loadFromLs(LS_LAST_ACTIVE);
console.log(loadFromLs(LS_LAST_ACTIVE), localStorage.getItem(LS_LAST_ACTIVE));
return localStorage.getItem(LS_LAST_ACTIVE);
}

async loadBackup(): Promise<AppDataComplete> {
Expand Down
36 changes: 26 additions & 10 deletions src/app/features/google/store/google-drive-sync.effects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -301,10 +301,15 @@ export class GoogleDriveSyncEffects {
concatMap((isUpdated: boolean): Observable<any> => {
if (isUpdated) {
return this._loadFile().pipe(
concatMap((loadResponse: any): any => {
const backup: AppDataComplete = loadResponse.backup;
concatMap((loadRes) => {
return combineLatest([
of(loadRes),
this._decodeAppDataIfNeeded(loadRes.backup)
]);
}),
concatMap(([loadResponse, appData]): any => {
const lastActiveLocal = this._syncService.getLastActive();
const lastActiveRemote = backup.lastActiveTime;
const lastActiveRemote = appData.lastActiveTime;

// update but ask if remote data is not newer than the last local update
const isSkipConfirm = (lastActiveRemote && this._isNewerThan(lastActiveRemote, lastActiveLocal));
Expand Down Expand Up @@ -487,26 +492,31 @@ export class GoogleDriveSyncEffects {
}

private async _import(loadRes): Promise<string> {
const backupData: AppDataComplete = await this._decodeAppDataIfNeeded(loadRes.backup);

return from(this._syncService.loadCompleteSyncData(backupData))
.pipe(mapTo(loadRes.meta.modifiedDate))
.toPromise();
}

private async _decodeAppDataIfNeeded(backupStr: string | AppDataComplete): Promise<AppDataComplete> {
let backupData: AppDataComplete;

// we attempt this regardless of the option, because data might be compressed anyway
if (typeof loadRes.backup === 'string') {
if (typeof backupStr === 'string') {
try {
backupData = JSON.parse(loadRes.backup) as AppDataComplete;
backupData = JSON.parse(backupStr) as AppDataComplete;
} catch (e) {
try {
const decompressedData = await this._compressionService.decompressUTF16(loadRes.backup);
const decompressedData = await this._compressionService.decompressUTF16(backupStr);
backupData = JSON.parse(decompressedData) as AppDataComplete;
} catch (e) {
console.error('Drive Sync, invalid data');
console.warn(e);
}
}
}

return from(this._syncService.loadCompleteSyncData(backupData))
.pipe(mapTo(loadRes.meta.modifiedDate))
.toPromise();
return backupData || (backupStr as AppDataComplete);
}

private _updateConfig(data: Partial<GoogleDriveSyncConfig>, isSkipLastActive = false): UpdateGlobalConfigSection {
Expand All @@ -528,6 +538,12 @@ export class GoogleDriveSyncEffects {
return (d1.getTime() > d2.getTime());
}

private _isNewerThanOrEqual(strDate1, strDate2) {
const d1 = new Date(strDate1);
const d2 = new Date(strDate2);
return (d1.getTime() >= d2.getTime());
}

private _isEqual(strDate1, strDate2) {
const d1 = new Date(strDate1);
const d2 = new Date(strDate2);
Expand Down

0 comments on commit 37c8880

Please sign in to comment.