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

Option to add a custom filename #53

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
17 changes: 13 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,21 +43,22 @@ function _registerListener(win, opts = {}) {
const listener = (e, item) => {

const itemUrl = decodeURIComponent(item.getURLChain()[0] || item.getURL())
const itemFilename = decodeURIComponent(item.getFilename());

let queueItem = _popQueueItem(itemUrl);
const itemFilename = queueItem.filename;

let ReceivedBytesArr = [];

if (queueItem) {
const folder = queueItem.downloadFolder || downloadFolder
const filePath = path.join(folder, queueItem.path, itemFilename);
const filePathTmp = path.join(folder, queueItem.path, "tmp-" + itemFilename);

const totalBytes = item.getTotalBytes();
let speedValue = 0;
let receivedBytes;
let PreviousReceivedBytes;

item.setSavePath(filePath);
item.setSavePath(filePathTmp);

// Resuming an interrupted download
if (item.getState() === 'interrupted') {
Expand Down Expand Up @@ -113,6 +114,11 @@ function _registerListener(win, opts = {}) {
// webContents.session.removeListener('will-download', listener);
// }

// move file from temporary name to desired name
try {
fs.renameSync(filePathTmp, filePath);
} catch {}

finishedDownloadCallback(null, { url: item.getURL(), filePath });

}
Expand All @@ -137,7 +143,10 @@ const download = (options, callback) => {

const request = net.request(options.url);

const filename = decodeURIComponent(path.basename(options.url));
var filename = decodeURIComponent(path.basename(options.url));
if (options.filename) {
filename = options.filename;
}
const url = decodeURIComponent(options.url);

const folder = options.downloadFolder || downloadFolder
Expand Down