Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Write command for other file types, and using custom path. #505

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
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
32 changes: 27 additions & 5 deletions app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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))
/**
Expand Down Expand Up @@ -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)) {
Expand All @@ -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") {
Expand Down Expand Up @@ -1111,6 +1132,7 @@ ipcMain.on("create-session", (_, name, adblock, cache) => {
return
}
}
currentDownloadPath = ""
const info = {
"current": 0,
"date": new Date(),
Expand Down
34 changes: 34 additions & 0 deletions app/renderer/command.js
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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
}
Expand Down