Skip to content

Commit

Permalink
Merge pull request #6629 from electron/download-item-get-save-path
Browse files Browse the repository at this point in the history
Set download item save path to selected path from dialog
  • Loading branch information
zcbenz committed Jul 28, 2016
2 parents bf20bbc + 55d6e0d commit e73bd00
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 6 deletions.
8 changes: 8 additions & 0 deletions atom/browser/atom_download_manager_delegate.cc
Expand Up @@ -85,6 +85,14 @@ void AtomDownloadManagerDelegate::OnDownloadPathGenerated(
download_manager_->GetBrowserContext());
browser_context->prefs()->SetFilePath(prefs::kDownloadDefaultDirectory,
path.DirName());

v8::Isolate* isolate = v8::Isolate::GetCurrent();
v8::Locker locker(isolate);
v8::HandleScope handle_scope(isolate);
api::DownloadItem* download_item = api::DownloadItem::FromWrappedClass(
isolate, item);
if (download_item)
download_item->SetSavePath(path);
}

// Running the DownloadTargetCallback with an empty FilePath signals that the
Expand Down
6 changes: 6 additions & 0 deletions docs/api/download-item.md
Expand Up @@ -80,6 +80,12 @@ The API is only available in session's `will-download` callback function.
If user doesn't set the save path via the API, Electron will use the original
routine to determine the save path(Usually prompts a save dialog).

### `downloadItem.getSavePath()`

Returns the save path of the download item. This will be either the path
set via `downloadItem.setSavePath(path)` or the path selected from the shown
save dialog.

### `downloadItem.pause()`

Pauses the download.
Expand Down
11 changes: 6 additions & 5 deletions spec/api-session-spec.js
Expand Up @@ -239,9 +239,10 @@ describe('session module', function () {
res.end(mockPDF)
downloadServer.close()
})
var assertDownload = function (event, state, url, mimeType, receivedBytes, totalBytes, disposition, filename, port) {
var assertDownload = function (event, state, url, mimeType, receivedBytes, totalBytes, disposition, filename, port, savePath) {
assert.equal(state, 'completed')
assert.equal(filename, 'mock.pdf')
assert.equal(savePath, path.join(__dirname, 'fixtures', 'mock.pdf'))
assert.equal(url, 'http://127.0.0.1:' + port + '/')
assert.equal(mimeType, 'application/pdf')
assert.equal(receivedBytes, mockPDF.length)
Expand All @@ -256,8 +257,8 @@ describe('session module', function () {
var port = downloadServer.address().port
ipcRenderer.sendSync('set-download-option', false, false)
w.loadURL(url + ':' + port)
ipcRenderer.once('download-done', function (event, state, url, mimeType, receivedBytes, totalBytes, disposition, filename) {
assertDownload(event, state, url, mimeType, receivedBytes, totalBytes, disposition, filename, port)
ipcRenderer.once('download-done', function (event, state, url, mimeType, receivedBytes, totalBytes, disposition, filename, savePath) {
assertDownload(event, state, url, mimeType, receivedBytes, totalBytes, disposition, filename, port, savePath)
done()
})
})
Expand All @@ -272,8 +273,8 @@ describe('session module', function () {
webview.addEventListener('did-finish-load', function () {
webview.downloadURL(url + ':' + port + '/')
})
ipcRenderer.once('download-done', function (event, state, url, mimeType, receivedBytes, totalBytes, disposition, filename) {
assertDownload(event, state, url, mimeType, receivedBytes, totalBytes, disposition, filename, port)
ipcRenderer.once('download-done', function (event, state, url, mimeType, receivedBytes, totalBytes, disposition, filename, savePath) {
assertDownload(event, state, url, mimeType, receivedBytes, totalBytes, disposition, filename, port, savePath)
document.body.removeChild(webview)
done()
})
Expand Down
3 changes: 2 additions & 1 deletion spec/static/main.js
Expand Up @@ -142,7 +142,8 @@ app.on('ready', function () {
item.getReceivedBytes(),
item.getTotalBytes(),
item.getContentDisposition(),
item.getFilename())
item.getFilename(),
item.getSavePath())
})
if (needCancel) item.cancel()
}
Expand Down

0 comments on commit e73bd00

Please sign in to comment.