Former-commit-id: 7e383da329449e501eca9d562d422efdd18f341e
This commit is contained in:
troyeguo
2021-02-14 22:38:35 +08:00
parent 97b48e86b6
commit e9580e496d
5 changed files with 80 additions and 74 deletions

65
main.js
View File

@@ -14,7 +14,7 @@ let splash;
app.disableHardwareAcceleration();
const port = 3366;
const configDir = (app || remote.app).getPath("userData");
const dirPath = path.join(configDir, "uploads\\");
const dirPath = path.join(configDir, "uploads");
app.on("ready", () => {
const { width, height } = screen.getPrimaryDisplay().workAreaSize;
@@ -91,10 +91,10 @@ app.on("ready", () => {
});
ipcMain.on("start-server", (event, arg) => {
startExpress();
event.returnValue = dirPath + "data";
event.returnValue = path.join(dirPath, "data");
});
ipcMain.on("storage-location", (event, arg) => {
event.returnValue = dirPath + "data";
event.returnValue = path.join(dirPath, "data");
});
ipcMain.on("get-file-data", function (event) {
var data = null;
@@ -123,6 +123,7 @@ const startExpress = () => {
const bodyParser = require("body-parser");
const fileUpload = require("express-fileupload");
const fs = require("fs");
const path = require("path");
const chardet = require("chardet");
const { readFileSync } = require("fs");
const iconv = require("iconv-lite");
@@ -172,8 +173,8 @@ const startExpress = () => {
server.use(cors());
server.use(bodyParser.json());
server.use(bodyParser.urlencoded({ extended: true }));
if (!fs.existsSync(dirPath + `/splash.png`)) {
let stream = fs.createWriteStream(dirPath + `/splash.png`);
if (!fs.existsSync(path.join(dirPath, `splash.png`))) {
let stream = fs.createWriteStream(path.join(dirPath, `splash.png`));
request(`https://koodo.960960.xyz/images/splash.png`)
.pipe(stream)
.on("close", function (err) {
@@ -199,7 +200,7 @@ const startExpress = () => {
name.length - (bookExtension !== "txt" ? 8 : 3) - 1
);
file.mv(dirPath + `/${file.name}`, () => {
file.mv(path.join(dirPath, file.name), () => {
var metadata = {
id: new Date().getTime(),
title: bookName,
@@ -214,15 +215,15 @@ const startExpress = () => {
description: "A book generated by Koodo Reader",
contents: "Content",
source: "https://koodo.960960.xyz",
images: [dirPath + `/splash.png`],
images: [path.join(dirPath, `splash.png`)],
};
// Set up the EPUB basics.
var epub = nodepub.document(metadata, dirPath + `/splash.png`);
var epub = nodepub.document(metadata, path.join(dirPath, `splash.png`));
let content = [];
let contentFilter = [];
const analyzeChapter = (isSuccess) => {
const data = readFileSync(dirPath + `/${file.name}`, {
const data = readFileSync(path.join(dirPath, file.name), {
encoding: "binary",
});
const buf = new Buffer(data, "binary");
@@ -315,14 +316,14 @@ const startExpress = () => {
dirPath,
bookName,
function () {
res.sendFile(dirPath + `/${bookName}.epub`);
res.sendFile(path.join(dirPath, `${bookName}.epub`));
res.on("finish", function () {
try {
fs.unlink(dirPath + `/${bookName}.epub`, (err) => {
fs.unlink(path.join(dirPath, `${bookName}.epub`), (err) => {
if (err) throw err;
console.log("successfully epub deleted");
});
fs.unlink(dirPath + `/${file.name}`, (err) => {
fs.unlink(path.join(dirPath + `/${file.name}`), (err) => {
if (err) throw err;
console.log("successfully file deleted");
});
@@ -341,7 +342,7 @@ const startExpress = () => {
username,
password,
});
file.mv(dirPath + `/${file.name}`, async () => {
file.mv(path.join(dirPath + `${file.name}`), async () => {
if ((await client.exists("/KoodoReader")) === false) {
await client.createDirectory("/KoodoReader");
}
@@ -352,7 +353,9 @@ const startExpress = () => {
"/KoodoReader/data.zip",
{},
() => {
fs.createReadStream(dirPath + `/${file.name}`).pipe(historystream);
fs.createReadStream(path.join(dirPath, `${file.name}`)).pipe(
historystream
);
}
);
let historystream = client.createWriteStream(
@@ -362,7 +365,7 @@ const startExpress = () => {
}.zip`,
{},
() => {
fs.unlink(dirPath + `/${file.name}`, (err) => {
fs.unlink(path.join(dirPath, `${file.name}`), (err) => {
if (err)
res.status(400).send({
message: "This is an error!",
@@ -372,7 +375,7 @@ const startExpress = () => {
res.send("success");
}
);
fs.createReadStream(dirPath + `/${file.name}`).pipe(Datastream);
fs.createReadStream(path.join(dirPath, `${file.name}`)).pipe(Datastream);
});
});
server.post("/webdav_download", async (req, res) => {
@@ -386,26 +389,26 @@ const startExpress = () => {
message: "This is an error!",
});
}
let stream = fs.createWriteStream(dirPath + `/data.zip`);
let stream = fs.createWriteStream(path.join(dirPath, `data.zip`));
client.createReadStream("/KoodoReader/data.zip").pipe(stream);
stream.on("close", () => {
res.sendFile(dirPath + `/data.zip`, function () {
res.sendFile(path.join(dirPath, `data.zip`), function () {
try {
fs.unlink(dirPath + `/data.zip`);
fs.unlink(path.join(dirPath, `data.zip`));
} catch (e) {
console.log("error removing ", dirPath + `/data.zip`);
console.log("error removing ", path.join(dirPath, `data.zip`));
}
});
});
});
server.post("/move_data", async (req, res) => {
const { file } = req.files;
const { path } = req.body;
file.mv(dirPath + `/${file.name}`, async () => {
var zip = new AdmZip(dirPath + `/${file.name}`);
zip.extractAllTo(/*target path*/ path, /*overwrite*/ true);
const { dataPath } = req.body;
file.mv(path.join(dirPath, file.name), async () => {
var zip = new AdmZip(path.join(dirPath, file.name));
zip.extractAllTo(/*target path*/ dataPath, /*overwrite*/ true);
try {
fs.unlink(dirPath + `/${file.name}`, (err) => {
fs.unlink(path.join(dirPath, file.name), (err) => {
if (err) console.log(err);
console.log("successfully data deleted");
res.send("success");
@@ -432,17 +435,17 @@ const startExpress = () => {
});
server.post("/add_book", async (req, res) => {
const { file } = req.files;
const { key, path } = req.body;
file.mv(path + `/book/${key}`, (err) => {
const { key, dataPath } = req.body;
file.mv(path.join(dataPath, `book`, key), (err) => {
if (err) return console.error(err);
res.send("success");
});
// fs.move(oldPath, newPath, console.error);
});
server.post("/delete_book", async (req, res) => {
const { key, path } = req.body;
const { key, dataPath } = req.body;
try {
fs.unlink(path + `/book/${key}`, (err) => {
fs.unlink(path.join(dataPath, `book`, key), (err) => {
if (err) throw err;
res.send("success");
});
@@ -455,8 +458,8 @@ const startExpress = () => {
// fs.move(oldPath, newPath, console.error);
});
server.post("/fetch_book", async (req, res) => {
const { key, path } = req.body;
var data = fs.readFileSync(path + `\\book\\${key}`);
const { key, dataPath } = req.body;
var data = fs.readFileSync(path.join(dataPath + `book` + key));
res.send(data);
});
async function start() {

View File

@@ -222,38 +222,29 @@ class BookList extends React.Component<BookListProps, BookListState> {
<>
{this.state.isOpenDelete && <DeletePopup {...deletePopupProps} />}
<ViewMode />
{this.props.mode === "trash" ? (
<div
className="booklist-delete-container"
onClick={() => {
this.props.handleDeleteDialog(true);
<div className="booklist-shelf-container">
<p className="general-setting-title" style={{ display: "inline" }}>
<Trans>My Shelves</Trans>
</p>
<select
className="booklist-shelf-list"
onChange={(event: React.ChangeEvent<HTMLSelectElement>) => {
this.handleShelfItem(event);
}}
>
<Trans>Delete All Books</Trans>
</div>
) : (
<div className="booklist-shelf-container">
<p className="general-setting-title" style={{ display: "inline" }}>
<Trans>My Shelves</Trans>
</p>
<select
className="booklist-shelf-list"
onChange={(event: React.ChangeEvent<HTMLSelectElement>) => {
this.handleShelfItem(event);
{this.renderShelfList()}
</select>
{this.state.shelfIndex > 0 ? (
<span
className="icon-trash delete-shelf-icon"
onClick={() => {
this.handleDeletePopup(true);
}}
>
{this.renderShelfList()}
</select>
{this.state.shelfIndex > 0 ? (
<span
className="icon-trash delete-shelf-icon"
onClick={() => {
this.handleDeletePopup(true);
}}
></span>
) : null}
</div>
)}
></span>
) : null}
</div>
<div className="book-list-container-parent">
<div className="book-list-container">
<ul className="book-list-item-box">{this.renderBookList()}</ul>

View File

@@ -30,8 +30,7 @@ class Header extends React.Component<HeaderProps, HeaderState> {
};
async componentDidMount() {
if (isElectron()) {
var remote = window.require("electron").remote;
var fs = remote.require("fs");
const fs = window.require("fs");
const { zip } = window.require("zip-a-folder");
let storageLocation = OtherUtil.getReaderConfig("storageLocation")
? OtherUtil.getReaderConfig("storageLocation")

View File

@@ -13,7 +13,7 @@ class BookUtil {
formData.append("file", new Blob([buffer]));
formData.append("key", key);
formData.append(
"path",
"dataPath",
OtherUtil.getReaderConfig("storageLocation")
? OtherUtil.getReaderConfig("storageLocation")
: window
@@ -46,7 +46,7 @@ class BookUtil {
axios
.post(`${config.token_url}/delete_book`, {
key,
path: OtherUtil.getReaderConfig("storageLocation")
dataPath: OtherUtil.getReaderConfig("storageLocation")
? OtherUtil.getReaderConfig("storageLocation")
: window
.require("electron")
@@ -73,7 +73,7 @@ class BookUtil {
// `${config.token_url}/fetch_book`,
// {
// key,
// path: OtherUtil.getReaderConfig("storageLocation")
// dataPath: OtherUtil.getReaderConfig("storageLocation")
// ? OtherUtil.getReaderConfig("storageLocation")
// : window
// .require("electron")
@@ -98,15 +98,28 @@ class BookUtil {
// console.error(error, "删除失败");
// reject();
// });
var remote = window.require("electron").remote;
var fs = remote.require("fs");
var fs = window.require("fs");
var path = window.require("path");
var data = fs.readFileSync(
(OtherUtil.getReaderConfig("storageLocation")
? OtherUtil.getReaderConfig("storageLocation")
: window
.require("electron")
.ipcRenderer.sendSync("storage-location", "ping")) +
`\\book\\${key}`
path.join(
OtherUtil.getReaderConfig("storageLocation")
? OtherUtil.getReaderConfig("storageLocation")
: window
.require("electron")
.ipcRenderer.sendSync("storage-location", "ping"),
`book`,
key
)
);
console.log(
path.join(
(OtherUtil.getReaderConfig("storageLocation")
? OtherUtil.getReaderConfig("storageLocation")
: window
.require("electron")
.ipcRenderer.sendSync("storage-location", "ping")) + "book",
key
)
);
let blobTemp = new Blob([data], { type: "application/epub+zip" });
let fileTemp = new File([blobTemp], "data.epub", {

View File

@@ -25,7 +25,7 @@ export const moveData = (blob, driveIndex, books: BookModel[] = []) => {
let formData = new FormData();
formData.append("file", file);
formData.append(
"path",
"dataPath",
OtherUtil.getReaderConfig("storageLocation")
? OtherUtil.getReaderConfig("storageLocation")
: window