Skip to content

Commit

Permalink
fix google backup
Browse files Browse the repository at this point in the history
  • Loading branch information
nialexsan committed Feb 12, 2025
1 parent 645d711 commit eebe5a8
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 57 deletions.
43 changes: 7 additions & 36 deletions src/background/service/googleDrive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@ interface DriveItem {
uid: string | null;
time: string | null;
}
interface DriveData {
address: string;
data: string;
}

// https://developers.google.com/drive/api/v3/reference/files/list
class GoogleDriveService {
Expand Down Expand Up @@ -46,6 +42,7 @@ class GoogleDriveService {
const token = await this.getAuthTokenWrapper(false);
return token !== undefined && token !== null;
} catch (err) {
console.error(err);
return false;
}
};
Expand Down Expand Up @@ -102,11 +99,12 @@ class GoogleDriveService {
uid: string,
password: string
) => {
// TODO: FIX this
const item = this.encodeToDriveItem(mnemonic, username, uid, password);
const files = await this.listFiles();
if (!files) {
return [];
const newContent = this.encrypt(JSON.stringify([item]), this.AES_KEY);
const file = await this.createFile(newContent);
return [file];
}
const fileId = files.id;
this.fileId = fileId;
Expand Down Expand Up @@ -180,7 +178,7 @@ class GoogleDriveService {
const { files } = await this.sendRequest('drive/v3/files/', 'GET', {
spaces: 'appDataFolder',
}).then((response) => response.json());
const firstOutblockBackup = files.find((file) => file.name === 'outblock_backup');
const firstOutblockBackup = files.find((file) => file.name === this.backupName);
return firstOutblockBackup;
};

Expand Down Expand Up @@ -279,13 +277,13 @@ class GoogleDriveService {
) => {
const token = await this.getAuthTokenWrapper();
const init = {
method: method,
method,
async: true,
headers: {
Authorization: 'Bearer ' + token,
Accept: '*/*',
},
contentType: 'json',
contentType: 'application/json',
};

if (method.toUpperCase() !== 'GET') {
Expand Down Expand Up @@ -333,33 +331,6 @@ class GoogleDriveService {
return decryptedText.trim();
};

// makeXMLRequest = async (method, url, data) => {
// const token = await this.getAuthTokenWrapper();
// return new Promise(function (resolve, reject) {
// const xhr = new XMLHttpRequest();
// xhr.open(method, url);
// xhr.setRequestHeader('Authorization', 'Bearer ' + token);
// xhr.responseType = 'json';
// xhr.onload = function () {
// if (this.status >= 200 && this.status < 300) {
// resolve(xhr.response);
// } else {
// reject({
// status: this.status,
// statusText: xhr.statusText
// });
// }
// };
// xhr.onerror = function () {
// reject({
// status: this.status,
// statusText: xhr.statusText
// });
// };
// xhr.send(data);
// })
// }

getAuthTokenWrapper = async (interactive = true) => {
return new Promise(function (resolve, reject) {
const detail = {
Expand Down
52 changes: 31 additions & 21 deletions src/ui/views/Setting/Backups/index.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import React, { useState, useEffect } from 'react';
import { useHistory } from 'react-router-dom';
import { makeStyles } from '@mui/styles';
import { Box, Typography, IconButton, Button } from '@mui/material';
import { useWallet } from 'ui/utils';
import { makeStyles } from '@mui/styles';
import React, { useState, useEffect } from 'react';

import { LLHeader, LLSpinner } from '@/ui/FRWComponent';
import IconGoogleDrive from '../../../../components/iconfont/IconGoogleDrive';
import CheckCircleIcon from '../../../../components/iconfont/IconCheckmark';
import { LLDeleteBackupPopup } from '@/ui/FRWComponent/LLDeleteBackupPopup';
import { useWallet } from 'ui/utils';

import CheckCircleIcon from '../../../../components/iconfont/IconCheckmark';
import IconGoogleDrive from '../../../../components/iconfont/IconGoogleDrive';

const useStyles = makeStyles(() => ({
arrowback: {
Expand Down Expand Up @@ -56,27 +57,16 @@ const useStyles = makeStyles(() => ({
},
}));

const orange = {
500: '#41CC5D',
};

const grey = {
400: '#BABABA',
500: '#787878',
600: '#5E5E5E',
};

const ManageBackups = () => {
const wallet = useWallet();
const classes = useStyles();
const history = useHistory();
const [hasPermission, setHasPermission] = useState(false);
const [hasBackup, setHasBackup] = useState(false);
const [loading, setLoading] = useState(true);
const [deleteBackupPop, setDeleteBackupPop] = useState(false);
const [deleteAllBackupPop, setDeleteAllBackupPop] = useState(false);

const checkPresmissions = async () => {
const checkPermissions = async () => {
const permissions = await wallet.hasGooglePremission();
setHasPermission(permissions);
if (permissions) {
Expand All @@ -91,17 +81,19 @@ const ManageBackups = () => {
setHasBackup(hasBackup);
setLoading(false);
} catch (e) {
console.error(e);
setLoading(false);
}
};

const syncBackup = async () => {
try {
setLoading(true);
const hasBackup = await wallet.syncBackup();
await wallet.syncBackup();
await checkBackup();
setLoading(false);
} catch (e) {
console.error(e);
setLoading(false);
}
};
Expand All @@ -113,6 +105,7 @@ const ManageBackups = () => {
await checkBackup();
setLoading(false);
} catch (e) {
console.error(e);
setLoading(false);
}
};
Expand All @@ -124,12 +117,27 @@ const ManageBackups = () => {
await checkBackup();
setLoading(false);
} catch (e) {
console.error(e);
setLoading(false);
}
};

const getGoogle = async () => {
setLoading(true);

try {
const accounts = await wallet.loadBackupAccounts();

localStorage.setItem('backupAccounts', JSON.stringify(accounts));
} catch (e) {
console.error(e);
} finally {
setLoading(false);
}
};

useEffect(() => {
checkPresmissions();
checkPermissions();
}, []);

return (
Expand All @@ -154,7 +162,9 @@ const ManageBackups = () => {
</Button>
)
) : (
<Button variant="text">{chrome.i18n.getMessage('Link')}</Button>
<Button variant="text" onClick={getGoogle}>
{chrome.i18n.getMessage('Link')}
</Button>
)}
</Box>

Expand Down

0 comments on commit eebe5a8

Please sign in to comment.