Skip to content

Commit

Permalink
add google drive
Browse files Browse the repository at this point in the history
  • Loading branch information
troyeguo committed Jan 1, 2024
1 parent 8353e3c commit a2e0ed9
Show file tree
Hide file tree
Showing 10 changed files with 266 additions and 522 deletions.
38 changes: 0 additions & 38 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,44 +133,6 @@ const createMainWin = () => {

return path.join(dirPath, "tts", audioName);
});
ipcMain.handle("onedrive-upload", async (event, config) => {
let { accessToken, filename } = config;
const { size } = fs.statSync(path.join(dirPath, filename));
const oneDriveAPI = require("onedrive-api");
let result = await oneDriveAPI.items.uploadSession({
accessToken: accessToken,
filename: filename,
fileSize: size,
parentPath: "/Apps/KoodoReader/",
readableStream: fs.createReadStream(path.join(dirPath, filename)),
conflictBehavior: "replace",
});
return true;
});

ipcMain.handle("onedrive-download", async (event, config) => {
let { accessToken, filename } = config;
const oneDriveAPI = require("onedrive-api");
var fileStream = await oneDriveAPI.items.download({
accessToken: accessToken,
itemPath: "/Apps/KoodoReader/" + filename,
});
const writeStream = fs.createWriteStream(path.join(dirPath, filename));
fileStream.pipe(writeStream);
async function closeWriteStream(writeStream) {
return new Promise((resolve) => {
writeStream.on("close", () => {
resolve(true);
});
writeStream.on("error", () => {
console.log("error");
resolve(false);
});
});
}
const result = await closeWriteStream(writeStream);
return result;
});
ipcMain.handle("ftp-upload", async (event, config) => {
let { url, username, password, filename, dir, ssl } = config;
const Client = require("ftp");
Expand Down
4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,12 @@
"ftp": "^0.3.10",
"howler": "^2.2.3",
"iconv-lite": "^0.6.3",
"onedrive-api": "^1.1.1",
"qs": "^6.11.2",
"react-hot-toast": "^2.1.1",
"request": "^2.88.2",
"ssh2-sftp-client": "^9.1.0",
"uuid": "^9.0.1",
"webdav": "^3.6.2",
"webdav-fs": "^3.0.0",
"webdav-fs": "3.0.0",
"wink-lemmatizer": "^3.0.4",
"ws": "^8.13.0",
"zip-a-folder": "^0.0.12"
Expand Down
53 changes: 29 additions & 24 deletions src/components/dialogs/backupDialog/component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { restore } from "../../../utils/syncUtils/restoreUtil";
import { Trans } from "react-i18next";
import DropboxUtil from "../../../utils/syncUtils/dropbox";
import OneDriveUtil from "../../../utils/syncUtils/onedrive";
import GoogleDriveUtil from "../../../utils/syncUtils/googledrive";
import WebdavUtil from "../../../utils/syncUtils/webdav";
import FtpUtil from "../../../utils/syncUtils/ftp";
import SFtpUtil from "../../../utils/syncUtils/sftp";
Expand Down Expand Up @@ -35,7 +36,7 @@ class BackupDialog extends React.Component<
this.state = {
currentStep: 0,
isBackup: "",
currentDrive: "Local",
currentDrive: "local",
};
}
handleClose = () => {
Expand Down Expand Up @@ -68,7 +69,7 @@ class BackupDialog extends React.Component<
day = new Date().getDate();
this.setState({ currentDrive: name }, async () => {
switch (name) {
case "Local":
case "local":
let blob: Blob | boolean = await backup(
this.props.books,
this.props.notes,
Expand All @@ -86,23 +87,26 @@ class BackupDialog extends React.Component<
);
this.handleFinish();
break;
case "Dropbox":
case "WebDAV":
case "OneDrive":
case "FTP":
case "SFTP":
if (!StorageUtil.getReaderConfig(name.toLowerCase() + "_token")) {
case "dropbox":
case "webdav":
case "onedrive":
case "googledrive":
case "ftp":
case "sftp":
if (!StorageUtil.getReaderConfig(name + "_token")) {
this.props.handleTokenDialog(true);
break;
}
let DriveUtil =
name === "Dropbox"
name === "dropbox"
? DropboxUtil
: name === "FTP"
: name === "ftp"
? FtpUtil
: name === "OneDrive"
: name === "onedrive"
? OneDriveUtil
: name === "SFTP"
: name === "googledrive"
? GoogleDriveUtil
: name === "sftp"
? SFtpUtil
: WebdavUtil;
if (this.state.isBackup === "yes") {
Expand Down Expand Up @@ -154,10 +158,10 @@ class BackupDialog extends React.Component<
onClick={() => {
//webdav is avavilible on desktop
if (
(item.name === "WebDAV" ||
item.name === "OneDrive" ||
item.name === "FTP" ||
item.name === "SFTP") &&
(item.icon === "webdav" ||
item.icon === "onedrive" ||
item.icon === "ftp" ||
item.icon === "sftp") &&
!isElectron
) {
toast(
Expand All @@ -167,7 +171,7 @@ class BackupDialog extends React.Component<
);
return;
}
this.handleDrive(item.name);
this.handleDrive(item.icon);
}}
>
<div className="backup-page-list-item-container">
Expand Down Expand Up @@ -197,17 +201,18 @@ class BackupDialog extends React.Component<
};

const dialogProps = {
driveName:
driveList[
window._.findLastIndex(driveList, {
name: this.state.currentDrive,
})
].name,
driveName: this.state.currentDrive,
url: driveList[
window._.findLastIndex(driveList, {
name: this.state.currentDrive,
icon: this.state.currentDrive,
})
].url,
title:
driveList[
window._.findLastIndex(driveList, {
icon: this.state.currentDrive,
})
].name,
};

return (
Expand Down
43 changes: 29 additions & 14 deletions src/components/dialogs/tokenDialog/component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,7 @@ class TokenDialog extends Component<TokenDialogProps, TokenDialogState> {
let token: string = (
document.querySelector("#token-dialog-token-box") as HTMLTextAreaElement
).value;
StorageUtil.setReaderConfig(
`${this.props.driveName.toLowerCase()}_token`,
token
);
StorageUtil.setReaderConfig(`${this.props.driveName}_token`, token);
this.props.handleTokenDialog(false);
toast.success(this.props.t("Add Successfully"));
};
Expand All @@ -36,7 +33,23 @@ class TokenDialog extends Component<TokenDialogProps, TokenDialogState> {
redirect_uri: driveConfig.callbackUrl,
});
StorageUtil.setReaderConfig(
`${this.props.driveName.toLowerCase()}_token`,
`${this.props.driveName}_token`,
res.data.refresh_token
);
this.props.handleTokenDialog(false);
toast.success(this.props.t("Add Successfully"));
};
handleGoogleDriveComfirm = async () => {
let code: string = (
document.querySelector("#token-dialog-token-box") as HTMLTextAreaElement
).value;
let res = await axios.post(driveConfig.googleAuthUrl, {
code,
redirect_uri: driveConfig.callbackUrl,
scope: driveConfig.googleScope,
});
StorageUtil.setReaderConfig(
`${this.props.driveName}_token`,
res.data.refresh_token
);
this.props.handleTokenDialog(false);
Expand All @@ -57,7 +70,7 @@ class TokenDialog extends Component<TokenDialogProps, TokenDialogState> {
) as HTMLTextAreaElement
).value;
StorageUtil.setReaderConfig(
`${this.props.driveName.toLowerCase()}_token`,
`${this.props.driveName}_token`,
JSON.stringify({ url, username, password })
);
this.props.handleTokenDialog(false);
Expand All @@ -84,7 +97,7 @@ class TokenDialog extends Component<TokenDialogProps, TokenDialogState> {
document.querySelector("#token-dialog-ssl-box") as HTMLTextAreaElement
).value;
StorageUtil.setReaderConfig(
`${this.props.driveName.toLowerCase()}_token`,
`${this.props.driveName}_token`,
JSON.stringify({ url, username, password, dir, ssl })
);
this.props.handleTokenDialog(false);
Expand All @@ -111,7 +124,7 @@ class TokenDialog extends Component<TokenDialogProps, TokenDialogState> {
document.querySelector("#token-dialog-port-box") as HTMLTextAreaElement
).value;
StorageUtil.setReaderConfig(
`${this.props.driveName.toLowerCase()}_token`,
`${this.props.driveName}_token`,
JSON.stringify({ url, username, password, dir, port })
);
this.props.handleTokenDialog(false);
Expand All @@ -127,7 +140,7 @@ class TokenDialog extends Component<TokenDialogProps, TokenDialogState> {
<div className="token-dialog-title">
<Trans>Authorize</Trans>
&nbsp;
{this.props.driveName}&nbsp;
{this.props.title}&nbsp;
<Trans>Token</Trans>
</div>
{this.props.driveName === "WebDAV" ? (
Expand Down Expand Up @@ -281,15 +294,17 @@ class TokenDialog extends Component<TokenDialogProps, TokenDialogState> {
<div
className="token-dialog-comfirm"
onClick={() => {
if (this.props.driveName === "WebDAV") {
if (this.props.driveName === "webdav") {
this.handleDavComfirm();
} else if (this.props.driveName === "FTP") {
} else if (this.props.driveName === "ftp") {
this.handleFTPComfirm();
} else if (this.props.driveName === "SFTP") {
} else if (this.props.driveName === "sftp") {
this.handleSFTPComfirm();
} else if (this.props.driveName === "Dropbox") {
} else if (this.props.driveName === "dropbox") {
this.handleDropboxComfirm();
} else {
} else if (this.props.driveName === "googledrive") {
this.handleGoogleDriveComfirm();
} else if (this.props.driveName === "onedrive") {
this.handleOneDriveComfirm();
}
}}
Expand Down
1 change: 1 addition & 0 deletions src/components/dialogs/tokenDialog/interface.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export interface TokenDialogProps {
handleTokenDialog: (isShow: boolean) => void;
currentBook: BookModel;
driveName: string;
title: string;
url: string;
t: (title: string) => string;
}
Expand Down
11 changes: 11 additions & 0 deletions src/constants/driveList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,13 @@ export const driveConfig = {
: "http://localhost:3000",
onedriveAuthUrl: "https://koodo.960960.xyz/api/onedrive_auth",
onedriveRefreshUrl: "https://koodo.960960.xyz/api/onedrive_refresh",
googleAuthUrl: "https://koodo.960960.xyz/api/google_auth",
googleRefreshUrl: "https://koodo.960960.xyz/api/google_refresh",
googleScope: "https://www.googleapis.com/auth/drive.appdata",
dropboxClientId: "vnc67byrssocvy1",
onedriveClientId: "506df58a-29ab-4020-afc5-6f423dc80f35",
googleClientId:
"1051055003225-ph1f5fvh328dhv7bco5jitlnfhg6ks2t.apps.googleusercontent.com",
};
export const driveList = [
{
Expand Down Expand Up @@ -45,4 +50,10 @@ export const driveList = [
icon: "sftp",
url: "",
},
{
id: 7,
name: "Google Drive",
icon: "googledrive",
url: `https://accounts.google.com/o/oauth2/v2/auth?redirect_uri=${driveConfig.callbackUrl}&prompt=consent&response_type=code&client_id=${driveConfig.googleClientId}&scope=${driveConfig.googleScope}&access_type=offline`,
},
];
Loading

0 comments on commit a2e0ed9

Please sign in to comment.