mirror of
				https://github.com/zulip/zulip-desktop.git
				synced 2025-11-03 21:43:18 +00:00 
			
		
		
		
	
		
			
				
	
	
		
			71 lines
		
	
	
		
			2.4 KiB
		
	
	
	
		
			TypeScript
		
	
	
	
	
	
			
		
		
	
	
			71 lines
		
	
	
		
			2.4 KiB
		
	
	
	
		
			TypeScript
		
	
	
	
	
	
import {app, shell} from "@electron/remote";
 | 
						|
 | 
						|
import * as ConfigUtil from "../../../common/config-util";
 | 
						|
import {ipcRenderer} from "../typed-ipc-renderer";
 | 
						|
import * as LinkUtil from "../utils/link-util";
 | 
						|
 | 
						|
import type WebView from "./webview";
 | 
						|
 | 
						|
const dingSound = new Audio("../resources/sounds/ding.ogg");
 | 
						|
 | 
						|
export default function handleExternalLink(
 | 
						|
  this: WebView,
 | 
						|
  event: Electron.NewWindowEvent,
 | 
						|
): void {
 | 
						|
  event.preventDefault();
 | 
						|
 | 
						|
  const url = new URL(event.url);
 | 
						|
  const downloadPath = ConfigUtil.getConfigItem(
 | 
						|
    "downloadsPath",
 | 
						|
    `${app.getPath("downloads")}`,
 | 
						|
  );
 | 
						|
 | 
						|
  if (LinkUtil.isUploadsUrl(this.props.url, url)) {
 | 
						|
    ipcRenderer.send("downloadFile", url.href, downloadPath);
 | 
						|
    ipcRenderer.once(
 | 
						|
      "downloadFileCompleted",
 | 
						|
      async (_event: Event, filePath: string, fileName: string) => {
 | 
						|
        const downloadNotification = new Notification("Download Complete", {
 | 
						|
          body: `Click to show ${fileName} in folder`,
 | 
						|
          silent: true, // We'll play our own sound - ding.ogg
 | 
						|
        });
 | 
						|
 | 
						|
        downloadNotification.addEventListener("click", () => {
 | 
						|
          // Reveal file in download folder
 | 
						|
          shell.showItemInFolder(filePath);
 | 
						|
        });
 | 
						|
        ipcRenderer.removeAllListeners("downloadFileFailed");
 | 
						|
 | 
						|
        // Play sound to indicate download complete
 | 
						|
        if (!ConfigUtil.getConfigItem("silent", false)) {
 | 
						|
          await dingSound.play();
 | 
						|
        }
 | 
						|
      },
 | 
						|
    );
 | 
						|
 | 
						|
    ipcRenderer.once("downloadFileFailed", (_event: Event, state: string) => {
 | 
						|
      // Automatic download failed, so show save dialog prompt and download
 | 
						|
      // through webview
 | 
						|
      // Only do this if it is the automatic download, otherwise show an error (so we aren't showing two save
 | 
						|
      // prompts right after each other)
 | 
						|
      // Check that the download is not cancelled by user
 | 
						|
      if (state !== "cancelled") {
 | 
						|
        if (ConfigUtil.getConfigItem("promptDownload", false)) {
 | 
						|
          // We need to create a "new Notification" to display it, but just `Notification(...)` on its own
 | 
						|
          // doesn't work
 | 
						|
          // eslint-disable-next-line no-new
 | 
						|
          new Notification("Download Complete", {
 | 
						|
            body: "Download failed",
 | 
						|
          });
 | 
						|
        } else {
 | 
						|
          this.getWebContents().downloadURL(url.href);
 | 
						|
        }
 | 
						|
      }
 | 
						|
 | 
						|
      ipcRenderer.removeAllListeners("downloadFileCompleted");
 | 
						|
    });
 | 
						|
  } else {
 | 
						|
    (async () => LinkUtil.openBrowser(url))();
 | 
						|
  }
 | 
						|
}
 |