Skip to content

Commit

Permalink
add ftp and sftp for backup
Browse files Browse the repository at this point in the history
  • Loading branch information
troyeguo committed Dec 31, 2023
1 parent 75f334e commit 0db165f
Show file tree
Hide file tree
Showing 22 changed files with 607 additions and 148 deletions.
142 changes: 142 additions & 0 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ const createMainWin = () => {
});
return true;
});

ipcMain.handle("onedrive-download", async (event, config) => {
let { accessToken, filename } = config;
const oneDriveAPI = require("onedrive-api");
Expand All @@ -170,6 +171,147 @@ const createMainWin = () => {
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");
let c = new Client();
async function uploadFile() {
return new Promise((resolve, reject) => {
c.on("ready", function () {
c.put(
path.join(dirPath, filename),
dir + "/" + filename,
function (err) {
if (err) reject(err);
c.end();
resolve(true);
}
);
});
c.connect({
host: url,
user: username,
password: password,
secure: ssl === "1" ? true : false,
});
});
}

try {
await uploadFile();
return true;
} catch (err) {
console.error(err);
return false;
}
});
ipcMain.handle("ftp-download", async (event, 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) {
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)));
});
});
c.connect({
host: url,
user: username,
password: password,
secure: ssl === "1" ? true : false,
});
});
}

try {
await downloadFile();
return true;
} catch (err) {
console.error(err);
return false;
}
});
ipcMain.handle("sftp-upload", async (event, 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);
sftp
.connect({
host: url,
port: port,
username: username,
password: password,
})
.then(() => {
return sftp.put(data, remote);
})
.then(() => {
resolve(true);
return sftp.end();
})
.catch((err) => {
console.error(err.message);
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 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));
sftp
.connect({
host: url,
port: port,
username: username,
password: password,
})
.then(() => {
return sftp.get(remotePath, dst);
})
.then(() => {
resolve(true);
return sftp.end();
})
.catch((err) => {
console.error(err.message);
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 Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,14 @@
"epub-cfi-resolver": "^1.0.2",
"font-list": "^1.4.5",
"fs-extra": "^9.1.0",
"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",
Expand Down
5 changes: 4 additions & 1 deletion src/assets/locales/en/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -377,5 +377,8 @@
"Hide books which have been added to the shelf, so they won't show up in home page": "Hide books which have been added to the shelf, so they won't show up in home page",
"Delete book from shelf also deleting book itself": "Delete book from shelf also deleting book itself",
"When deleting book from shelf, the book will be deleted as well": "When deleting a book from the shelf, the book will be deleted as well",
"This action will permanently delete the selected books, together with their notes, bookmarks and digests": "This action will permanently delete the selected books, together with their notes, bookmarks and digests"
"This action will permanently delete the selected books, together with their notes, bookmarks and digests": "This action will permanently delete the selected books, together with their notes, bookmarks and digests",
"Server path": "Server path",
"Use SSL, 1 for use, 0 for not use": "Use SSL, 1 for use, 0 for not use",
"Server port": "Server port"
}
5 changes: 4 additions & 1 deletion src/assets/locales/zh-CN/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -381,5 +381,8 @@
"Hide books which have been added to the shelf, so they won't show up in home page": "当把图书添加到书架后,该图书就不会展示在主页书架中",
"Delete book from shelf also deleting book itself": "直接删除从书架中移除的图书",
"When deleting book from shelf, the book will be deleted as well": "在书架中执行删除操作时,不仅会从书架删除,还会直接将其删除到回收站",
"This action will permanently delete the selected books, together with their notes, bookmarks and digests": "此操作会彻底删除所有选中的图书,以及它们的书签,笔记和高亮"
"This action will permanently delete the selected books, together with their notes, bookmarks and digests": "此操作会彻底删除所有选中的图书,以及它们的书签,笔记和高亮",
"Server path": "服务器路径",
"Use SSL, 1 for use, 0 for not use": "是否使用 SSL, 1 表示使用, 0 表示不使用",
"Server port": "服务器端口"
}
Binary file modified src/assets/styles/fonts/icomoon.eot
Binary file not shown.
Loading

0 comments on commit 0db165f

Please sign in to comment.