refactor: make savePath a property on DownloadItem (#18677)
This commit is contained in:
parent
e95d2129be
commit
536327151d
4 changed files with 30 additions and 15 deletions
|
@ -80,7 +80,9 @@ The `downloadItem` object has the following methods:
|
||||||
|
|
||||||
The API is only available in session's `will-download` callback function.
|
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
|
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).
|
routine to determine the save path; this usually prompts a save dialog.
|
||||||
|
|
||||||
|
**[Deprecated](modernization/property-updates.md): use the `savePath` property instead.**
|
||||||
|
|
||||||
#### `downloadItem.getSavePath()`
|
#### `downloadItem.getSavePath()`
|
||||||
|
|
||||||
|
@ -88,6 +90,8 @@ Returns `String` - The save path of the download item. This will be either the p
|
||||||
set via `downloadItem.setSavePath(path)` or the path selected from the shown
|
set via `downloadItem.setSavePath(path)` or the path selected from the shown
|
||||||
save dialog.
|
save dialog.
|
||||||
|
|
||||||
|
**[Deprecated](modernization/property-updates.md): use the `savePath` property instead.**
|
||||||
|
|
||||||
#### `downloadItem.setSaveDialogOptions(options)`
|
#### `downloadItem.setSaveDialogOptions(options)`
|
||||||
|
|
||||||
* `options` SaveDialogOptions - Set the save file dialog options. This object has the same
|
* `options` SaveDialogOptions - Set the save file dialog options. This object has the same
|
||||||
|
@ -181,3 +185,13 @@ Returns `String` - ETag header value.
|
||||||
|
|
||||||
Returns `Double` - Number of seconds since the UNIX epoch when the download was
|
Returns `Double` - Number of seconds since the UNIX epoch when the download was
|
||||||
started.
|
started.
|
||||||
|
|
||||||
|
### Instance Properties
|
||||||
|
|
||||||
|
#### `downloadItem.savePath`
|
||||||
|
|
||||||
|
A `String` property that determines the save file path of the download item.
|
||||||
|
|
||||||
|
The property is only available in session's `will-download` callback function.
|
||||||
|
If user doesn't set the save path via the property, Electron will use the original
|
||||||
|
routine to determine the save path; this usually prompts a save dialog.
|
||||||
|
|
|
@ -20,9 +20,6 @@ The Electron team is currently undergoing an initiative to convert separate gett
|
||||||
* `visibleOnAllWorkspaces`
|
* `visibleOnAllWorkspaces`
|
||||||
* `crashReporter` module
|
* `crashReporter` module
|
||||||
* `uploadToServer`
|
* `uploadToServer`
|
||||||
* `DownloadItem` class
|
|
||||||
* `savePath`
|
|
||||||
* `paused`
|
|
||||||
* `Session` module
|
* `Session` module
|
||||||
* `preloads`
|
* `preloads`
|
||||||
* `webContents` module
|
* `webContents` module
|
||||||
|
@ -47,6 +44,8 @@ The Electron team is currently undergoing an initiative to convert separate gett
|
||||||
* `applicationMenu`
|
* `applicationMenu`
|
||||||
* `badgeCount`
|
* `badgeCount`
|
||||||
* `name`
|
* `name`
|
||||||
|
* `DownloadItem` class
|
||||||
|
* `savePath`
|
||||||
* `BrowserWindow` module
|
* `BrowserWindow` module
|
||||||
* `autohideMenuBar`
|
* `autohideMenuBar`
|
||||||
* `resizable`
|
* `resizable`
|
||||||
|
|
|
@ -209,6 +209,8 @@ void DownloadItem::BuildPrototype(v8::Isolate* isolate,
|
||||||
.SetMethod("isDone", &DownloadItem::IsDone)
|
.SetMethod("isDone", &DownloadItem::IsDone)
|
||||||
.SetMethod("setSavePath", &DownloadItem::SetSavePath)
|
.SetMethod("setSavePath", &DownloadItem::SetSavePath)
|
||||||
.SetMethod("getSavePath", &DownloadItem::GetSavePath)
|
.SetMethod("getSavePath", &DownloadItem::GetSavePath)
|
||||||
|
.SetProperty("savePath", &DownloadItem::GetSavePath,
|
||||||
|
&DownloadItem::SetSavePath)
|
||||||
.SetMethod("setSaveDialogOptions", &DownloadItem::SetSaveDialogOptions)
|
.SetMethod("setSaveDialogOptions", &DownloadItem::SetSaveDialogOptions)
|
||||||
.SetMethod("getSaveDialogOptions", &DownloadItem::GetSaveDialogOptions)
|
.SetMethod("getSaveDialogOptions", &DownloadItem::GetSaveDialogOptions)
|
||||||
.SetMethod("getLastModifiedTime", &DownloadItem::GetLastModifiedTime)
|
.SetMethod("getLastModifiedTime", &DownloadItem::GetLastModifiedTime)
|
||||||
|
|
|
@ -553,8 +553,8 @@ describe('session module', () => {
|
||||||
const assertDownload = (state, item, isCustom = false) => {
|
const assertDownload = (state, item, isCustom = false) => {
|
||||||
expect(state).to.equal('completed')
|
expect(state).to.equal('completed')
|
||||||
expect(item.getFilename()).to.equal('mock.pdf')
|
expect(item.getFilename()).to.equal('mock.pdf')
|
||||||
expect(path.isAbsolute(item.getSavePath())).to.equal(true)
|
expect(path.isAbsolute(item.savePath)).to.equal(true)
|
||||||
expect(isPathEqual(item.getSavePath(), downloadFilePath)).to.equal(true)
|
expect(isPathEqual(item.savePath, downloadFilePath)).to.equal(true)
|
||||||
if (isCustom) {
|
if (isCustom) {
|
||||||
expect(item.getURL()).to.equal(`${protocolName}://item`)
|
expect(item.getURL()).to.equal(`${protocolName}://item`)
|
||||||
} else {
|
} else {
|
||||||
|
@ -571,7 +571,7 @@ describe('session module', () => {
|
||||||
it('can download using WebContents.downloadURL', (done) => {
|
it('can download using WebContents.downloadURL', (done) => {
|
||||||
const port = downloadServer.address().port
|
const port = downloadServer.address().port
|
||||||
w.webContents.session.once('will-download', function (e, item) {
|
w.webContents.session.once('will-download', function (e, item) {
|
||||||
item.setSavePath(downloadFilePath)
|
item.savePath = downloadFilePath
|
||||||
item.on('done', function (e, state) {
|
item.on('done', function (e, state) {
|
||||||
assertDownload(state, item)
|
assertDownload(state, item)
|
||||||
done()
|
done()
|
||||||
|
@ -589,7 +589,7 @@ describe('session module', () => {
|
||||||
protocol.registerHttpProtocol(protocolName, handler, (error) => {
|
protocol.registerHttpProtocol(protocolName, handler, (error) => {
|
||||||
if (error) return done(error)
|
if (error) return done(error)
|
||||||
w.webContents.session.once('will-download', function (e, item) {
|
w.webContents.session.once('will-download', function (e, item) {
|
||||||
item.setSavePath(downloadFilePath)
|
item.savePath = downloadFilePath
|
||||||
item.on('done', function (e, state) {
|
item.on('done', function (e, state) {
|
||||||
assertDownload(state, item, true)
|
assertDownload(state, item, true)
|
||||||
done()
|
done()
|
||||||
|
@ -612,7 +612,7 @@ describe('session module', () => {
|
||||||
}
|
}
|
||||||
const done = new Promise(resolve => {
|
const done = new Promise(resolve => {
|
||||||
w.webContents.session.once('will-download', function (e, item) {
|
w.webContents.session.once('will-download', function (e, item) {
|
||||||
item.setSavePath(downloadFilePath)
|
item.savePath = downloadFilePath
|
||||||
item.on('done', function (e, state) {
|
item.on('done', function (e, state) {
|
||||||
resolve([state, item])
|
resolve([state, item])
|
||||||
})
|
})
|
||||||
|
@ -626,7 +626,7 @@ describe('session module', () => {
|
||||||
it('can cancel download', (done) => {
|
it('can cancel download', (done) => {
|
||||||
const port = downloadServer.address().port
|
const port = downloadServer.address().port
|
||||||
w.webContents.session.once('will-download', function (e, item) {
|
w.webContents.session.once('will-download', function (e, item) {
|
||||||
item.setSavePath(downloadFilePath)
|
item.savePath = downloadFilePath
|
||||||
item.on('done', function (e, state) {
|
item.on('done', function (e, state) {
|
||||||
expect(state).to.equal('cancelled')
|
expect(state).to.equal('cancelled')
|
||||||
expect(item.getFilename()).to.equal('mock.pdf')
|
expect(item.getFilename()).to.equal('mock.pdf')
|
||||||
|
@ -650,7 +650,7 @@ describe('session module', () => {
|
||||||
|
|
||||||
const port = downloadServer.address().port
|
const port = downloadServer.address().port
|
||||||
w.webContents.session.once('will-download', function (e, item) {
|
w.webContents.session.once('will-download', function (e, item) {
|
||||||
item.setSavePath(downloadFilePath)
|
item.savePath = downloadFilePath
|
||||||
item.on('done', function (e, state) {
|
item.on('done', function (e, state) {
|
||||||
expect(item.getFilename()).to.equal('download.pdf')
|
expect(item.getFilename()).to.equal('download.pdf')
|
||||||
done()
|
done()
|
||||||
|
@ -694,7 +694,7 @@ describe('session module', () => {
|
||||||
describe('when a save path is specified and the URL is unavailable', () => {
|
describe('when a save path is specified and the URL is unavailable', () => {
|
||||||
it('does not display a save dialog and reports the done state as interrupted', (done) => {
|
it('does not display a save dialog and reports the done state as interrupted', (done) => {
|
||||||
w.webContents.session.once('will-download', function (e, item) {
|
w.webContents.session.once('will-download', function (e, item) {
|
||||||
item.setSavePath(downloadFilePath)
|
item.savePath = downloadFilePath
|
||||||
if (item.getState() === 'interrupted') {
|
if (item.getState() === 'interrupted') {
|
||||||
item.resume()
|
item.resume()
|
||||||
}
|
}
|
||||||
|
@ -725,7 +725,7 @@ describe('session module', () => {
|
||||||
expect(item.getMimeType()).to.equal(options.mimeType)
|
expect(item.getMimeType()).to.equal(options.mimeType)
|
||||||
expect(item.getReceivedBytes()).to.equal(options.offset)
|
expect(item.getReceivedBytes()).to.equal(options.offset)
|
||||||
expect(item.getTotalBytes()).to.equal(options.length)
|
expect(item.getTotalBytes()).to.equal(options.length)
|
||||||
expect(item.getSavePath()).to.equal(downloadFilePath)
|
expect(item.savePath).to.equal(downloadFilePath)
|
||||||
done()
|
done()
|
||||||
})
|
})
|
||||||
w.webContents.session.createInterruptedDownload(options)
|
w.webContents.session.createInterruptedDownload(options)
|
||||||
|
@ -756,7 +756,7 @@ describe('session module', () => {
|
||||||
expect(item.getState()).to.equal('cancelled')
|
expect(item.getState()).to.equal('cancelled')
|
||||||
|
|
||||||
const options = {
|
const options = {
|
||||||
path: item.getSavePath(),
|
path: item.savePath,
|
||||||
urlChain: item.getURLChain(),
|
urlChain: item.getURLChain(),
|
||||||
mimeType: item.getMimeType(),
|
mimeType: item.getMimeType(),
|
||||||
offset: item.getReceivedBytes(),
|
offset: item.getReceivedBytes(),
|
||||||
|
@ -778,7 +778,7 @@ describe('session module', () => {
|
||||||
const completedItem = await downloadResumed
|
const completedItem = await downloadResumed
|
||||||
expect(completedItem.getState()).to.equal('completed')
|
expect(completedItem.getState()).to.equal('completed')
|
||||||
expect(completedItem.getFilename()).to.equal('logo.png')
|
expect(completedItem.getFilename()).to.equal('logo.png')
|
||||||
expect(completedItem.getSavePath()).to.equal(downloadFilePath)
|
expect(completedItem.savePath).to.equal(downloadFilePath)
|
||||||
expect(completedItem.getURL()).to.equal(downloadUrl)
|
expect(completedItem.getURL()).to.equal(downloadUrl)
|
||||||
expect(completedItem.getMimeType()).to.equal('image/png')
|
expect(completedItem.getMimeType()).to.equal('image/png')
|
||||||
expect(completedItem.getReceivedBytes()).to.equal(14022)
|
expect(completedItem.getReceivedBytes()).to.equal(14022)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue