From 77e384bb7941cd6f089b361200ebca449e859841 Mon Sep 17 00:00:00 2001 From: troyeguo <13820674+troyeguo@users.noreply.github.com> Date: Mon, 14 Jun 2021 13:02:30 +0800 Subject: [PATCH] fix bug Former-commit-id: 2fbb67d39db7aba0f55de5790bbb01dce3285e19 --- src/containers/header/component.tsx | 56 +++++++++++++++++++---------- src/utils/syncUtils/common.tsx | 41 ++++++++++++--------- 2 files changed, 63 insertions(+), 34 deletions(-) diff --git a/src/containers/header/component.tsx b/src/containers/header/component.tsx index abbe079e..20b6e78c 100644 --- a/src/containers/header/component.tsx +++ b/src/containers/header/component.tsx @@ -166,24 +166,44 @@ class Header extends React.Component { .ipcRenderer.sendSync("storage-location", "ping"); let sourcePath = path.join(storageLocation, "config", "readerConfig.json"); let readerConfig: any; - try { - readerConfig = JSON.parse( - fs.readFileSync(sourcePath, { encoding: "utf8", flag: "r" }) - ); - } catch (error) { - BackupUtil.backup( - this.props.books, - this.props.notes, - this.props.bookmarks, - () => { - this.props.handleMessage("Sync Successfully"); - this.props.handleMessageBox(true); - }, - 5, - () => {} - ); - return; - } + fs.readFile(sourcePath, "utf8", (err, data) => { + if (err) { + BackupUtil.backup( + this.props.books, + this.props.notes, + this.props.bookmarks, + () => { + this.props.handleMessage("Sync Successfully"); + this.props.handleMessageBox(true); + }, + 5, + () => {} + ); + return; + } + const readerConfig = JSON.parse(data); + if ( + readerConfig && + localStorage.getItem("lastSyncTime") && + parseInt(readerConfig.lastSyncTime) > + parseInt(localStorage.getItem("lastSyncTime")!) + ) { + this.syncFromLocation(); + } else { + //否则就把Koodo中数据同步到同步文件夹 + BackupUtil.backup( + this.props.books, + this.props.notes, + this.props.bookmarks, + () => { + this.props.handleMessage("Sync Successfully"); + this.props.handleMessageBox(true); + }, + 5, + () => {} + ); + } + }); //如果同步文件夹的记录较新,就从同步文件夹同步数据到Koodo if ( diff --git a/src/utils/syncUtils/common.tsx b/src/utils/syncUtils/common.tsx index 03ffbfb9..1a4dba2d 100644 --- a/src/utils/syncUtils/common.tsx +++ b/src/utils/syncUtils/common.tsx @@ -45,24 +45,33 @@ export const moveData = ( ); var zip = new AdmZip(path.join(dirPath, file.name)); zip.extractAllTo(/*target path*/ dataPath, /*overwrite*/ true); - try { - const fs = window.require("fs-extra"); - - fs.remove(path.join(dirPath, file.name), async (err) => { - if (err) console.log(err); - if (driveIndex === 4) { - let deleteBooks = books.map((item) => { - return localforage.removeItem(item.key); - }); - await Promise.all(deleteBooks); - } - if (driveIndex === 5) { - handleFinish(); - } + if (driveIndex === 4) { + let deleteBooks = books.map((item) => { + return localforage.removeItem(item.key); }); - } catch (e) { - console.error(e, "移动失败"); + await Promise.all(deleteBooks); } + if (driveIndex === 5) { + handleFinish(); + } + // try { + // const fs = window.require("fs-extra"); + + // fs.remove(path.join(dirPath, file.name), async (err) => { + // if (err) console.log(err); + // if (driveIndex === 4) { + // let deleteBooks = books.map((item) => { + // return localforage.removeItem(item.key); + // }); + // await Promise.all(deleteBooks); + // } + // if (driveIndex === 5) { + // handleFinish(); + // } + // }); + // } catch (e) { + // console.error(e, "移动失败"); + // } }; }; class SyncUtil {