Skip to content

Commit

Permalink
added auto download option
Browse files Browse the repository at this point in the history
  • Loading branch information
mienaiyami committed Sep 25, 2023
1 parent bfba00d commit 925bb43
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 55 deletions.
6 changes: 3 additions & 3 deletions src/electron/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -423,11 +423,11 @@ const registerListener = () => {
});
}

ipcMain.on("checkForUpdate:response", (e, res, windowId, skipMinor) => {
ipcMain.on("checkForUpdate:response", (e, res, windowId, skipMinor, autoDownload) => {
if (res) {
checkForUpdate(windowId, skipMinor);
checkForUpdate(windowId, skipMinor, false, autoDownload);
setInterval(() => {
checkForUpdate(windowId, skipMinor);
checkForUpdate(windowId, skipMinor, false, autoDownload);
}, 1000 * 60 * 60 * 1);
}
});
Expand Down
121 changes: 70 additions & 51 deletions src/electron/updater.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@ const downloadLink = "https://github.com/mienaiyami/yomikiru/releases/download/v
* @param windowId id of window in which message box should be shown
* @param promptAfterCheck (false by default) Show message box if current version is same as latest version.
*/
const checkForUpdate = async (windowId: number, skipMinor = false, promptAfterCheck = false) => {
const checkForUpdate = async (
windowId: number,
skipMinor = false,
promptAfterCheck = false,
autoDownload = false
) => {
const rawdata = await fetch("https://api.github.com/repos/mienaiyami/yomikiru/releases").then((data) =>
data.json()
);
Expand Down Expand Up @@ -47,29 +52,32 @@ const checkForUpdate = async (windowId: number, skipMinor = false, promptAfterCh
latestVersion[1] === currentAppVersion[1] &&
latestVersion[2] > currentAppVersion[2])
) {
dialog
.showMessageBox(window, {
type: "info",
title: "New Version Available",
message:
`Current Version : ${currentAppVersion.join(".")}\n` +
`Latest Version : ${latestVersion.join(".")}` +
(latestVersion[0] === currentAppVersion[0] && latestVersion[1] === currentAppVersion[1]
? `\n\nTo skip check for minor updates, enable "skip minor update" in settings`
: ""),
buttons: ["Download Now", "Download and show Changelog", "Show Changelog", "Download Later"],
cancelId: 3,
})
.then((response) => {
if (response.response === 0) downloadUpdates(latestVersion.join("."), windowId);
if (response.response === 1) {
downloadUpdates(latestVersion.join("."), windowId);
shell.openExternal("https://github.com/mienaiyami/yomikiru/releases");
}
if (response.response === 2) {
shell.openExternal("https://github.com/mienaiyami/yomikiru/releases");
}
});
if (autoDownload) {
downloadUpdates(latestVersion.join("."), windowId, true);
} else
dialog
.showMessageBox(window, {
type: "info",
title: "New Version Available",
message:
`Current Version : ${currentAppVersion.join(".")}\n` +
`Latest Version : ${latestVersion.join(".")}` +
(latestVersion[0] === currentAppVersion[0] && latestVersion[1] === currentAppVersion[1]
? `\n\nTo skip check for minor updates, enable "skip minor update" in settings`
: ""),
buttons: ["Download Now", "Download and show Changelog", "Show Changelog", "Download Later"],
cancelId: 3,
})
.then((response) => {
if (response.response === 0) downloadUpdates(latestVersion.join("."), windowId);
if (response.response === 1) {
downloadUpdates(latestVersion.join("."), windowId);
shell.openExternal("https://github.com/mienaiyami/yomikiru/releases");
}
if (response.response === 2) {
shell.openExternal("https://github.com/mienaiyami/yomikiru/releases");
}
});
return;
}
logger.log("Running latest version.");
Expand All @@ -87,48 +95,59 @@ const checkForUpdate = async (windowId: number, skipMinor = false, promptAfterCh
* @param latestVersion latest version ex. "2.3.8"
* @param windowId id of window in which message box should be shown
*/
const downloadUpdates = (latestVersion: string, windowId: number) => {
const newWindow = new BrowserWindow({
width: 560,
height: 160,
resizable: false,
backgroundColor: "#272727",
webPreferences: {
nodeIntegration: true,
contextIsolation: false,
// enableRemoteModule: true,
webSecurity: app.isPackaged,
safeDialogs: true,
},
maximizable: false,
});
newWindow.loadURL(DOWNLOAD_PROGRESS_WEBPACK_ENTRY);
newWindow.setMenuBarVisibility(false);
newWindow.webContents.once("dom-ready", () => {
newWindow.webContents.send("version", latestVersion);
});
const downloadUpdates = (latestVersion: string, windowId: number, silent = false) => {
const newWindow =
!silent &&
new BrowserWindow({
width: 560,
height: 160,
resizable: false,
backgroundColor: "#272727",
webPreferences: {
nodeIntegration: true,
contextIsolation: false,
// enableRemoteModule: true,
webSecurity: app.isPackaged,
safeDialogs: true,
},
maximizable: false,
});
if (newWindow) {
newWindow.loadURL(DOWNLOAD_PROGRESS_WEBPACK_ENTRY);
newWindow.setMenuBarVisibility(false);
newWindow.webContents.once("dom-ready", () => {
newWindow.webContents.send("version", latestVersion);
});
}

const window = BrowserWindow.fromId(windowId ?? 1)!;
const tempPath = path.join(app.getPath("temp"), "yomikiru updates " + new Date().toDateString());
if (fs.existsSync(tempPath)) fs.rmSync(tempPath, { recursive: true, force: true });
fs.mkdirSync(tempPath);
const promptInstall = () => {
newWindow.close();
newWindow && newWindow.close();
const buttons = ["Install Now", "Install on Quit"];
if (silent) buttons.push("Install and Show Changelog");
dialog
.showMessageBox(window, {
type: "info",
title: "Updates downloaded",
message: "Updates downloaded.",
buttons: ["Install Now", "Install on Quit"],
buttons,
cancelId: 1,
})
.then((res) => {
if (res.response === 0) {
app.quit();
}
if (res.response === 2) shell.openExternal("https://github.com/mienaiyami/yomikiru/releases");
});
};
const downloadFile = (dl: string, webContents: Electron.WebContents, callback: (file: File) => void) => {
const downloadFile = (
dl: string,
webContents: Electron.WebContents | false,
callback: (file: File) => void
) => {
download(window, dl, {
directory: tempPath,
onStarted: () => {
Expand All @@ -140,7 +159,7 @@ const downloadUpdates = (latestVersion: string, windowId: number) => {
},
onCompleted: (file) => callback(file),
onProgress: (progress) => {
webContents.send("progress", progress);
webContents && webContents.send("progress", progress);
},
}).catch((reason) => {
dialog.showMessageBox(window, {
Expand All @@ -160,7 +179,7 @@ const downloadUpdates = (latestVersion: string, windowId: number) => {
const extractPath = path.join(tempPath, "updates");
if (!fs.existsSync(extractPath)) fs.mkdirSync(extractPath);

downloadFile(dl, newWindow.webContents, (file) => {
downloadFile(dl, newWindow && newWindow.webContents, (file) => {
logger.log(`${file.filename} downloaded.`);
crossZip.unzip(file.path, extractPath, (err) => {
if (err) return logger.error(err);
Expand Down Expand Up @@ -190,7 +209,7 @@ const downloadUpdates = (latestVersion: string, windowId: number) => {
process.arch === "ia32"
? downloadLink + latestVersion + "/" + `Yomikiru-v${latestVersion}-Setup.exe`
: downloadLink + latestVersion + "/" + `Yomikiru-v${latestVersion}-Setup-x64.exe`;
downloadFile(dl, newWindow.webContents, (file) => {
downloadFile(dl, newWindow && newWindow.webContents, (file) => {
logger.log(`${file.filename} downloaded.`);
app.on("quit", () => {
logger.log("Installing updates...");
Expand All @@ -208,7 +227,7 @@ const downloadUpdates = (latestVersion: string, windowId: number) => {
}
else if (process.platform === "linux") {
const dl = downloadLink + latestVersion + "/" + `Yomikiru-v${latestVersion}-amd64.deb`;
downloadFile(dl, newWindow.webContents, (file) => {
downloadFile(dl, newWindow && newWindow.webContents, (file) => {
logger.log(`${file.filename} downloaded.`);
dialog
.showMessageBox(window, {
Expand Down
3 changes: 2 additions & 1 deletion src/renderer/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,8 @@ const App = (): ReactElement => {
"checkForUpdate:response",
appSettings.updateCheckerEnabled,
window.electron.getCurrentWindow().id,
appSettings.skipMinorUpdate
appSettings.skipMinorUpdate,
appSettings.autoDownloadUpdate
);
});
window.electron.ipcRenderer.on("askBeforeClose:query", () => {
Expand Down
10 changes: 10 additions & 0 deletions src/renderer/Components/Settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1573,6 +1573,16 @@ const Settings = (): ReactElement => {
title="Mostly just frequent updates rather than minor."
paraAfter="Skip minor updates"
/>
<InputCheckbox
checked={appSettings.autoDownloadUpdate}
className="noBG"
onChange={(e) => {
dispatch(
setAppSettings({ autoDownloadUpdate: e.currentTarget.checked })
);
}}
paraAfter="Auto download updates"
/>
</div>
<div className="main row">
<button
Expand Down
2 changes: 2 additions & 0 deletions src/renderer/MainImports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ export const settingValidatorData = {
updateCheckerEnabled: false,
askBeforeClosing: false,
skipMinorUpdate: false,
autoDownloadUpdate: false,
/**
* Open chapter in reader directly, one folder inside of base manga dir.
*/
Expand Down Expand Up @@ -1291,6 +1292,7 @@ const defaultSettings: AppSettings = {
updateCheckerEnabled: true,
askBeforeClosing: false,
skipMinorUpdate: false,
autoDownloadUpdate: false,
openDirectlyFromManga: false,
showTabs: {
bookmark: true,
Expand Down

0 comments on commit 925bb43

Please sign in to comment.