Skip to content

Commit

Permalink
Merge pull request #1134 from substance/download-file
Browse files Browse the repository at this point in the history
Download file improvements
  • Loading branch information
oliver7654 authored Feb 5, 2019
2 parents 6cce387 + e062d42 commit c7ed26f
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 9 deletions.
14 changes: 13 additions & 1 deletion app/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const url = require('url')
const fsExtra = require('fs-extra')

const {
app, dialog, shell, protocol,
app, dialog, shell, protocol, session,
BrowserWindow, Menu, ipcMain
} = electron
const DEBUG = process.env.DEBUG
Expand Down Expand Up @@ -37,6 +37,18 @@ app.on('ready', () => {
if (error) console.error('Failed to register protocol')
})

// Download files
session.defaultSession.on('will-download', (event, item) => {
let location = dialog.showSaveDialog({ defaultPath: item.getFilename() })
// If there is no location came from dialog it means cancelation and
// we should prevent default action to close dialog without error
if (location) {
item.setSavePath(location)
} else {
event.preventDefault()
}
})

createMenu()

// look if there is a 'dar' file in the args that does exist
Expand Down
13 changes: 11 additions & 2 deletions src/article/editor/DownloadSupplementaryFileCommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,23 @@ import { Command } from 'substance'
*/
export default class DownloadSupplementaryFileCommand extends Command {
getCommandState (params, context) {
const xpath = params.selectionState.xpath
const selectionState = params.selectionState
const xpath = selectionState.xpath
if (xpath.length > 0) {
const selectedType = xpath[xpath.length - 1].type
return { disabled: selectedType !== 'supplementary-file' }
if (selectedType === 'supplementary-file') {
return {
disabled: false,
// leaving the node, so that the tool can apply different
// strategies for local vs remote files
node: selectionState.node
}
}
}
return { disabled: true }
}

execute (params, context) {
// Nothing: downloading is implemented via native download hooks
}
}
33 changes: 27 additions & 6 deletions src/article/editor/DownloadSupplementaryFileTool.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,24 @@ export default class DownloadSupplementaryFileTool extends Tool {
let link = $$('a').ref('link')
// ATTENTION: stop propagation, otherwise infinite loop
.on('click', domHelpers.stop)
// Note: in the browser version we want to open the download in a new tab
if (!platform.inElectron) {

// Downloading is a bit involved:
// In electron, everything can be done with one solution,
// handling a 'will-download' event, which is triggered when the `download`
// attribute is present.
// For the browser, the `download` attribute works only for files from the same
// origin. For remote files the best we can do at the moment, is opening
// a new tab, and let the browser deal with it.
// TODO: if this feature is important, one idea is that the DAR server could
// provide an end-point to provide download-urls, and act as a proxy to
// cirvumvent the CORS problem.
const isLocal = this._isLocal()
if (platform.inElectron || isLocal) {
link.attr('download', '')
} else {
link.attr('target', '_blank')
}

el.append(link)
return el
}
Expand All @@ -27,10 +41,8 @@ export default class DownloadSupplementaryFileTool extends Tool {

_triggerDownload () {
const archive = this.context.archive
const editorSession = this.context.editorSession
const selectionState = editorSession.getSelectionState()
const node = selectionState.node
const isLocal = !node.remote
const node = this._getNode()
const isLocal = this._isLocal()
let url = node.href
if (isLocal) {
url = archive.getDownloadLink(node.href)
Expand All @@ -42,4 +54,13 @@ export default class DownloadSupplementaryFileTool extends Tool {
this.refs.link.el.click()
}
}

_getNode () {
return this.props.commandState.node
}

_isLocal () {
let node = this._getNode()
return (!node || !node.remote)
}
}

0 comments on commit c7ed26f

Please sign in to comment.