Skip to content

Commit

Permalink
add s3 for backup
Browse files Browse the repository at this point in the history
  • Loading branch information
troyeguo committed Jan 6, 2024
1 parent 9a61f3d commit 3fd9ac6
Show file tree
Hide file tree
Showing 9 changed files with 267 additions and 139 deletions.
173 changes: 158 additions & 15 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,15 +134,15 @@ const createMainWin = () => {
return path.join(dirPath, "tts", audioName);
});
ipcMain.handle("ftp-upload", async (event, config) => {
let { url, username, password, filename, dir, ssl } = config;
let { url, username, password, fileName, dir, ssl } = config;
const Client = require("ftp");
let c = new Client();
async function uploadFile() {
return new Promise((resolve, reject) => {
c.on("ready", function () {
c.put(
path.join(dirPath, filename),
dir + "/" + filename,
path.join(dirPath, fileName),
dir + "/" + fileName,
function (err) {
if (err) reject(err);
c.end();
Expand All @@ -168,20 +168,19 @@ const createMainWin = () => {
}
});
ipcMain.handle("ftp-download", async (event, config) => {
let { url, username, password, filename, dir, ssl } = config;
let { url, username, password, fileName, dir, ssl } = config;
const Client = require("ftp");
let c = new Client();
async function downloadFile() {
return new Promise((resolve, reject) => {
c.on("ready", function () {
c.get(dir + "/" + filename, function (err, stream) {
c.get(dir + "/" + fileName, function (err, stream) {
if (err) reject(err);
stream.once("close", function () {
c.end();
resolve(true);
});
console.log(path.join(dirPath, filename));
stream.pipe(fs.createWriteStream(path.join(dirPath, filename)));
stream.pipe(fs.createWriteStream(path.join(dirPath, fileName)));
});
});
c.connect({
Expand All @@ -202,14 +201,13 @@ const createMainWin = () => {
}
});
ipcMain.handle("sftp-upload", async (event, config) => {
let { url, username, password, filename, dir, port } = config;
let { url, username, password, fileName, dir, port } = config;
let Client = require("ssh2-sftp-client");
let sftp = new Client();
async function uploadFile() {
return new Promise((resolve, reject) => {
let data = fs.createReadStream(path.join(dirPath, filename));
let remote = "/" + dir + "/" + filename;
console.log(url, username, password, filename, dir, port);
let data = fs.createReadStream(path.join(dirPath, fileName));
let remote = "/" + dir + "/" + fileName;
sftp
.connect({
host: url,
Expand Down Expand Up @@ -238,14 +236,70 @@ const createMainWin = () => {
return false;
}
});
ipcMain.handle("webdav-download", async (event, config) => {
let { url, username, password, fileName } = config;
const { createClient } = require("webdav");
async function downloadFile() {
return new Promise(async (resolve, reject) => {
const client = createClient(url, {
username,
password,
});
if ((await client.exists("/KoodoReader/data.zip")) === false) {
resolve(false);
}
const buffer = await client.getFileContents("/KoodoReader/data.zip");
fs.writeFileSync(path.join(dirPath, fileName), buffer);
resolve(true);
});
}

try {
return await downloadFile();
} catch (err) {
console.error(err);
return false;
}
});
ipcMain.handle("webdav-upload", async (event, config) => {
let { url, username, password, fileName } = config;
const { createClient } = require("webdav");
async function uploadFile() {
return new Promise(async (resolve, reject) => {
const client = createClient(url, {
username,
password,
});
if ((await client.exists("/KoodoReader")) === false) {
await client.createDirectory("/KoodoReader");
}
let writeStream = client.createWriteStream("/KoodoReader/data.zip");
fs.createReadStream(path.join(dirPath, fileName)).pipe(writeStream);
writeStream.on("finish", () => {
resolve(true);
});
writeStream.on("error", (err) => {
console.log(error);
resolve(false);
});
});
}

try {
return await uploadFile();
} catch (err) {
console.error(err);
return false;
}
});
ipcMain.handle("sftp-download", async (event, config) => {
let { url, username, password, filename, dir, port } = config;
let { url, username, password, fileName, dir, port } = config;
let Client = require("ssh2-sftp-client");
let sftp = new Client();
async function downloadFile() {
return new Promise((resolve, reject) => {
let remotePath = "/" + dir + "/" + filename;
let dst = fs.createWriteStream(path.join(dirPath, filename));
let remotePath = "/" + dir + "/" + fileName;
let dst = fs.createWriteStream(path.join(dirPath, fileName));
sftp
.connect({
host: url,
Expand Down Expand Up @@ -274,6 +328,95 @@ const createMainWin = () => {
return false;
}
});
ipcMain.handle("s3-upload", async (event, config) => {
const { S3Client, PutObjectCommand } = require("@aws-sdk/client-s3");
let {
endpoint,
region,
bucketName,
accessKeyId,
secretAccessKey,
fileName,
} = config;
const s3 = new S3Client({
endpoint,
region,
credentials: {
accessKeyId,
secretAccessKey,
},
});
try {
await s3.send(
new PutObjectCommand({
Bucket: bucketName,
Key: fileName,
Body: fs.createReadStream(path.join(dirPath, fileName)),
})
);
return true;
} catch (err) {
console.log("Error: ", err);
return false;
}
});
ipcMain.handle("s3-download", async (event, config) => {
let {
endpoint,
region,
bucketName,
accessKeyId,
secretAccessKey,
fileName,
} = config;
const { S3Client, GetObjectCommand } = require("@aws-sdk/client-s3");
function getObject(s3, bucket, key, writable) {
return new Promise(async (resolve, reject) => {
const getObjectCommandOutput = await s3.send(
new GetObjectCommand({
Bucket: bucket,
Key: key,
})
);
if (getObjectCommandOutput.Body) {
getObjectCommandOutput.Body.pipe(writable);
writable.on("finish", (err) => {
if (err) reject(false);
resolve(true);
});
} else {
reject(false);
}
});
}
async function downloadFile() {
return new Promise((resolve, reject) => {
const s3 = new S3Client({
region,
endpoint,
credentials: {
accessKeyId,
secretAccessKey,
},
});
let writeStream = fs.createWriteStream(path.join(dirPath, fileName));
getObject(s3, bucketName, fileName, writeStream)
.then((data) => {
resolve(true);
})
.catch((err) => {
console.error(err);
resolve(false);
});
});
}
try {
return await downloadFile();
} catch (err) {
console.error(err);
return false;
}
});
ipcMain.handle("clear-tts", async (event, config) => {
if (!fs.existsSync(path.join(dirPath, "tts"))) {
return "pong";
Expand All @@ -296,6 +439,7 @@ const createMainWin = () => {
});
return path;
});

ipcMain.handle("get-url-content", async (event, config) => {
const axios = require("axios");
try {
Expand All @@ -313,7 +457,6 @@ const createMainWin = () => {
});
ipcMain.handle("wiki-index", async (event, config) => {
const wiki = require("wikijs").default;
console.log(config);
try {
let page = await wiki({
apiUrl: "https://" + config.code + ".wikipedia.org/w/api.php",
Expand Down
2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,10 @@
"adm-zip": "^0.5.2",
"axios": "^0.19.2",
"buffer": "^6.0.3",
"concat-stream": "^2.0.0",
"copy-text-to-clipboard": "^2.2.0",
"dompurify": "^3.0.1",
"electron-is-dev": "^1.1.0",
"electron-store": "^8.0.1",
"end-of-stream": "^1.4.4",
"epub-cfi-resolver": "^1.0.2",
"font-list": "^1.4.5",
"fs-extra": "^9.1.0",
Expand Down
2 changes: 2 additions & 0 deletions src/containers/header/component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ class Header extends React.Component<HeaderProps, HeaderState> {
};
}
async componentDidMount() {
// isElectron &&
// (await window.require("electron").ipcRenderer.invoke("s3-download"));
if (isElectron) {
const fs = window.require("fs");
const path = window.require("path");
Expand Down
18 changes: 9 additions & 9 deletions src/utils/syncUtils/ftp.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ class FtpUtil {
const { ipcRenderer } = window.require("electron");
const dirPath = ipcRenderer.sendSync("user-data", "ping");
const arrayBuffer = await blob.arrayBuffer();
const filename = "data.zip";
fs.writeFileSync(path.join(dirPath, filename), Buffer.from(arrayBuffer));
const fileName = "data.zip";
fs.writeFileSync(path.join(dirPath, fileName), Buffer.from(arrayBuffer));
resolve(
await ipcRenderer.invoke("ftp-upload", {
url,
username,
password,
filename,
fileName,
ssl,
dir,
})
Expand All @@ -28,7 +28,7 @@ class FtpUtil {
};
static DownloadFile = async () => {
return new Promise<boolean>(async (resolve, reject) => {
const filename = "data.zip";
const fileName = "data.zip";
const fs = window.require("fs");
const path = window.require("path");
const { ipcRenderer } = window.require("electron");
Expand All @@ -40,14 +40,14 @@ class FtpUtil {
url,
username,
password,
filename,
fileName,
ssl,
dir,
});
if (result) {
var data = fs.readFileSync(path.join(dirPath, filename));
var data = fs.readFileSync(path.join(dirPath, fileName));
let blobTemp: any = new Blob([data], { type: "application/zip" });
let fileTemp = new File([blobTemp], filename, {
let fileTemp = new File([blobTemp], fileName, {
lastModified: new Date().getTime(),
type: blobTemp.type,
});
Expand All @@ -57,12 +57,12 @@ class FtpUtil {
resolve(true);
try {
const fs_extra = window.require("fs-extra");
fs_extra.remove(path.join(dirPath, filename), (error: any) => {
fs_extra.remove(path.join(dirPath, fileName), (error: any) => {
if (error) resolve(false);
resolve(true);
});
} catch (e) {
console.log("error removing ", path.join(dirPath, filename));
console.log("error removing ", path.join(dirPath, fileName));
resolve(false);
}
});
Expand Down
8 changes: 4 additions & 4 deletions src/utils/syncUtils/googledrive.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const getData = (file) =>
const fr = new FileReader();
fr.onload = (f: any) =>
resolve({
filename: file.name,
fileName: file.name,
mimeType: file.type,
fileSize: file.size,
data: f.target.result,
Expand Down Expand Up @@ -42,7 +42,7 @@ class GoogleDriveUtil {
// 1. Create the session for the resumable upload.
const metadata = {
mimeType: fileObj.mimeType,
name: fileObj.filename,
name: fileObj.fileName,
parents: ["appDataFolder"],
};
try {
Expand Down Expand Up @@ -87,7 +87,7 @@ class GoogleDriveUtil {
});
const accessToken = res.data.access_token;
let fileId = StorageUtil.getReaderConfig("googleFileId");
const filename = "data.zip";
const fileName = "data.zip";
const url = `https://www.googleapis.com/drive/v3/files/${fileId}?alt=media`;

try {
Expand All @@ -103,7 +103,7 @@ class GoogleDriveUtil {
type: response.headers["content-type"],
});

let fileTemp = new File([blob], filename, {
let fileTemp = new File([blob], fileName, {
lastModified: new Date().getTime(),
type: blob.type,
});
Expand Down
6 changes: 3 additions & 3 deletions src/utils/syncUtils/onedrive.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,14 @@ class OneDriveUtil {
}
static DownloadFile() {
return new Promise<boolean>(async (resolve, reject) => {
const filename = "data.zip";
const fileName = "data.zip";
var refresh_token = StorageUtil.getReaderConfig("onedrive_token") || "";
let res = await axios.post(driveConfig.onedriveRefreshUrl, {
refresh_token,
redirect_uri: driveConfig.callbackUrl,
});
const accessToken = res.data.access_token; // 替换为实际的访问令牌
const downloadUrl = `https://graph.microsoft.com/v1.0/me/drive/special/approot:/${filename}:/content`;
const downloadUrl = `https://graph.microsoft.com/v1.0/me/drive/special/approot:/${fileName}:/content`;
try {
const response = await axios.get(downloadUrl, {
responseType: "blob",
Expand All @@ -67,7 +67,7 @@ class OneDriveUtil {
let blobTemp: any = new Blob([response.data], {
type: "application/zip",
});
let fileTemp = new File([blobTemp], filename, {
let fileTemp = new File([blobTemp], fileName, {
lastModified: new Date().getTime(),
type: blobTemp.type,
});
Expand Down
Loading

0 comments on commit 3fd9ac6

Please sign in to comment.