fix handleDownloads regression
This commit is contained in:
parent
46620001f5
commit
fe666d2e16
|
@ -9,6 +9,7 @@ import electronReload from 'electron-reload';
|
|||
import { PROD_HOST_URL, RENDERER_OUTPUT_DIR } from './config';
|
||||
import { isDev } from './utils/common';
|
||||
import serveNextAt from 'next-electron-server';
|
||||
import { existsSync } from 'fs';
|
||||
|
||||
if (isDev) {
|
||||
electronReload(__dirname, {});
|
||||
|
@ -71,6 +72,20 @@ if (!gotTheLock) {
|
|||
if (BrowserWindow.getAllWindows().length === 0) createWindow();
|
||||
});
|
||||
|
||||
setupTrayItem();
|
||||
setupIpcComs(tray, mainWindow);
|
||||
handleUpdates();
|
||||
handleDownloads();
|
||||
});
|
||||
}
|
||||
|
||||
function handleUpdates() {
|
||||
if (!isDev) {
|
||||
AppUpdater.checkForUpdate(tray, mainWindow);
|
||||
}
|
||||
}
|
||||
|
||||
function setupTrayItem() {
|
||||
const trayImgPath = isDev
|
||||
? 'build/taskbar-icon.png'
|
||||
: path.join(process.resourcesPath, 'taskbar-icon.png');
|
||||
|
@ -78,10 +93,32 @@ if (!gotTheLock) {
|
|||
tray = new Tray(trayIcon);
|
||||
tray.setToolTip('ente');
|
||||
tray.setContextMenu(buildContextMenu(mainWindow));
|
||||
|
||||
setupIpcComs(tray, mainWindow);
|
||||
if (!isDev) {
|
||||
AppUpdater.checkForUpdate(tray, mainWindow);
|
||||
}
|
||||
|
||||
function handleDownloads() {
|
||||
mainWindow.webContents.session.on('will-download', (event, item) => {
|
||||
item.setSavePath(
|
||||
getUniqueSavePath(item.getFilename(), app.getPath('downloads'))
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
function getUniqueSavePath(filename: string, directory: string): string {
|
||||
let uniqueFileSavePath = path.join(directory, filename);
|
||||
const { name: filenameWithoutExtension, ext: extension } =
|
||||
path.parse(filename);
|
||||
let n = 0;
|
||||
while (existsSync(uniqueFileSavePath)) {
|
||||
n++;
|
||||
// filter need to remove undefined extension from the array
|
||||
// else [`${fileName}`, undefined].join(".") will lead to `${fileName}.` as joined string
|
||||
const fileNameWithNumberedSuffix = [
|
||||
`${filenameWithoutExtension}(${n})`,
|
||||
extension,
|
||||
]
|
||||
.filter((x) => x) // filters out undefined/null values
|
||||
.join('.');
|
||||
uniqueFileSavePath = path.join(directory, fileNameWithNumberedSuffix);
|
||||
}
|
||||
return uniqueFileSavePath;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue