diff --git a/app/index.js b/app/index.js index 82115dd8..d2a3fb4d 100644 --- a/app/index.js +++ b/app/index.js @@ -792,6 +792,7 @@ const dlsFile = joinPath(app.getPath("appData"), "dls") * cleardownloadsoncompleted?: boolean * }} */ let downloadSettings = {"downloadpath": app.getPath("downloads")} +let currentDownloadPath = "" /** @typedef {{ * current: number, * date: Date, @@ -836,6 +837,9 @@ ipcMain.on("set-redirects", (_, rdr) => { const updateRequestHeaders = (_, headers) => { requestHeaders = headers.split(",").filter(h => h) } +ipcMain.on("update-current-download-path", (_, path) => { + currentDownloadPath = path +}) ipcMain.on("update-request-headers", updateRequestHeaders) ipcMain.on("open-download", (_, location) => shell.openPath(location)) /** @@ -1054,12 +1058,25 @@ ipcMain.on("create-session", (_, name, adblock, cache) => { return callback({"cancel": false, "requestHeaders": headers}) }) newSess.on("will-download", (e, item) => { - if (downloadSettings.downloadmethod === "block" || !mainWindow) { + if ((downloadSettings.downloadmethod === "block" + && !currentDownloadPath) || !mainWindow) { e.preventDefault() return } - const filename = item.getFilename() - let save = joinPath(downloadSettings.downloadpath, filename) + let filename = item.getFilename() + let save = "" + let customPath = "" + if (currentDownloadPath) { + const pathComponents = currentDownloadPath.split("/") + const customFilename = pathComponents.at(-1) + if (customFilename) { + filename = customFilename + } + customPath = pathComponents.slice(0, -1).join("/") + save = joinPath(customPath, filename) + } else { + save = joinPath(downloadSettings.downloadpath, filename) + } let duplicateNumber = 0 let newFilename = item.getFilename() while (isFile(save)) { @@ -1074,9 +1091,13 @@ ipcMain.on("create-session", (_, name, adblock, cache) => { newFilename = `${filename.substring(0, extStart)} (${ duplicateNumber}).${filename.substring(extStart + 1)}` } - save = joinPath(downloadSettings.downloadpath, newFilename) + if (customPath) { + save = joinPath(customPath, newFilename) + } else { + save = joinPath(downloadSettings.downloadpath, newFilename) + } } - if (downloadSettings.downloadmethod !== "ask") { + if (downloadSettings.downloadmethod !== "ask" || currentDownloadPath) { item.setSavePath(save) } if (downloadSettings.downloadmethod === "confirm") { @@ -1111,6 +1132,7 @@ ipcMain.on("create-session", (_, name, adblock, cache) => { return } } + currentDownloadPath = "" const info = { "current": 0, "date": new Date(), diff --git a/app/renderer/command.js b/app/renderer/command.js index 0bce96cb..c1570858 100644 --- a/app/renderer/command.js +++ b/app/renderer/command.js @@ -586,6 +586,10 @@ const resolveFileArg = (locationArg, type, customPage = null) => { } if (locationArg.endsWith("/") || locationArg.endsWith("\\")) { file = joinPath(`${file}${pathSep}`, name) + } else { + const pathComponents = file.split("/") + file = joinPath(`${pathComponents.slice(0, -1) + .join("/")}${pathSep}`, pathComponents.pop()) } if (!isDir(dirname(file))) { notify(`Folder '${dirname(file)}' does not exist!`, "warn") @@ -623,12 +627,42 @@ const write = (args, range) => { /** * Write the html of a page to disk based on tab index or current. + * When the page contains browser viewable files, allows you to write the file + * with a custom location. * @param {string|null} customLoc * @param {number|null} tabIdx */ const writePage = (customLoc = null, tabIdx = null) => { /** @type {Electron.WebviewTag|HTMLDivElement|null} */ + const downloadableFileTypes + = [ + "png", + "jpg", + "jpeg", + "gif", + "webp", + "bmp", + "ogg", + "mp3", + "wav", + "mp4", + "pdf", + "webm" + ] let page = currentPage() + let checkedTypes = 0 + if (downloadableFileTypes.some(type => { + checkedTypes += 1 + return page.getURL().endsWith(`.${type}`) + })) { + const loc = resolveFileArg(customLoc, + downloadableFileTypes[checkedTypes - 1]) + if (loc) { + ipcRenderer.send("update-current-download-path", loc) + currentPage()?.downloadURL(stringToUrl(page.getURL())) + } + return + } if (tabIdx !== null) { page = pageForTab(listTabs()[tabIdx]) ?? null }