Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 additions & 2 deletions app/main/handle-external-link.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {shell} from "electron/common";
import type {
BrowserWindow,
HandlerDetails,
SaveDialogOptions,
WebContents,
Expand All @@ -20,18 +21,21 @@ function isUploadsUrl(server: string, url: URL): boolean {
function downloadFile({
contents,
url,
win,
downloadPath,
completed,
failed,
}: {
contents: WebContents;
url: string;
win: BrowserWindow;
downloadPath: string;
completed(filePath: string, fileName: string): Promise<void>;
failed(state: string): void;
}) {
contents.downloadURL(url);
contents.session.once("will-download", async (_event: Event, item) => {
const totalFileSize = item.getTotalBytes();
if (ConfigUtil.getConfigItem("promptDownload", false)) {
const showDialogOptions: SaveDialogOptions = {
defaultPath: path.join(downloadPath, item.getFilename()),
Expand Down Expand Up @@ -59,6 +63,11 @@ function downloadFile({
item.setSavePath(setFilePath);
}

let currProgress = 0;
const progressInterval = setInterval(() => {
win.setProgressBar(currProgress / totalFileSize);
}, 1000);

const updatedListener = (_event: Event, state: string): void => {
switch (state) {
case "interrupted": {
Expand All @@ -67,15 +76,18 @@ function downloadFile({
"Download interrupted, cancelling and fallback to dialog download.",
);
item.cancel();
win.setProgressBar(-1);
break;
}

case "progressing": {
if (item.isPaused()) {
item.cancel();
win.setProgressBar(-1);
break;
}

// This event can also be used to show progress in percentage in future.
currProgress = item.getReceivedBytes();
break;
}

Expand All @@ -89,11 +101,16 @@ function downloadFile({
item.once("done", async (_event: Event, state) => {
if (state === "completed") {
await completed(item.getSavePath(), path.basename(item.getSavePath()));
win.setProgressBar(1);
} else {
win.setProgressBar(-1);
clearInterval(progressInterval);
console.log("Download failed state:", state);

failed(state);
}

clearInterval(progressInterval);
// To stop item for listening to updated events of this file
item.removeListener("updated", updatedListener);
});
Expand All @@ -104,6 +121,7 @@ export default function handleExternalLink(
contents: WebContents,
details: HandlerDetails,
mainContents: WebContents,
win: BrowserWindow,
): void {
let url: URL;
try {
Expand All @@ -121,19 +139,24 @@ export default function handleExternalLink(
downloadFile({
contents,
url: url.href,
win,
downloadPath,
async completed(filePath: string, fileName: string) {
const downloadNotification = new Notification({
title: "Download Complete",
body: `Click to show ${fileName} in folder`,
silent: true, // We'll play our own sound - ding.ogg
});
shell.showItemInFolder(filePath);
downloadNotification.on("click", () => {
// Reveal file in download folder
shell.showItemInFolder(filePath);
});
downloadNotification.show();

setTimeout(() => {
downloadNotification.close();
win.setProgressBar(-1);
}, 3000);
// Play sound to indicate download complete
if (!ConfigUtil.getConfigItem("silent", false)) {
send(mainContents, "play-ding-sound");
Expand Down
2 changes: 1 addition & 1 deletion app/main/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ function createMainWindow(): BrowserWindow {

app.on("web-contents-created", (_event: Event, contents: WebContents) => {
contents.setWindowOpenHandler((details) => {
handleExternalLink(contents, details, page);
handleExternalLink(contents, details, page, mainWindow);
return {action: "deny"};
});
});
Expand Down
2 changes: 1 addition & 1 deletion app/renderer/js/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ export class ServerManagerView {
},
downloadsPath: `${app.getPath("downloads")}`,
quitOnClose: false,
promptDownload: false,
promptDownload: true,
};

// Platform specific settings
Expand Down