Merge pull request #519 from yuya-oc/download-public-file

Don't show an extra window when clicking public file link
This commit is contained in:
Yuya Ochiai
2017-04-28 21:16:42 +09:00
committed by GitHub
4 changed files with 74 additions and 2 deletions

View File

@@ -24,6 +24,8 @@ Release date: TBD
#### All Platforms
- Fixed an issue where an unexpected row appeared after switching channels with `CTRL+K` shortcut. [#426](https://github.com/mattermost/desktop/issues/426)
- Fixed an issue where an unexpected extra window opened when clicking the public link for an uploaded file.
[#390](https://github.com/mattermost/desktop/issues/390)
#### Windows
- Fixed an issue where the main window still has focus and exists

View File

@@ -66,9 +66,14 @@ const MattermostView = React.createClass({
ipcRenderer.send('confirm-protocol', destURL.protocol, e.url);
return;
}
if (currentURL.host === destURL.host) {
if (destURL.path.match(/^\/api\/v[3-4]\/public\/files\//)) {
ipcRenderer.send('download-url', e.url);
} else {
// New window should disable nodeIntergration.
window.open(e.url, 'Mattermost', 'nodeIntegration=no, show=yes');
}
} else {
// if the link is external, use default browser.
shell.openExternal(e.url);

View File

@@ -61,6 +61,7 @@ var certificateStore = require('./main/certificateStore').load(path.resolve(app.
const {createMainWindow} = require('./main/mainWindow');
const appMenu = require('./main/menus/app');
const trayMenu = require('./main/menus/tray');
const downloadURL = require('./main/downloadURL');
const allowProtocolDialog = require('./main/allowProtocolDialog');
const SpellChecker = require('./main/SpellChecker');
@@ -327,6 +328,18 @@ app.on('login', (event, webContents, request, authInfo, callback) => {
allowProtocolDialog.init(mainWindow);
ipcMain.on('download-url', (event, URL) => {
downloadURL(mainWindow, URL, (err) => {
if (err) {
dialog.showMessageBox(mainWindow, {
type: 'error',
message: err.toString()
});
console.log(err);
}
});
});
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
app.on('ready', () => {

52
src/main/downloadURL.js Normal file
View File

@@ -0,0 +1,52 @@
const fs = require('fs');
const path = require('path');
const zlib = require('zlib');
const electron = require('electron');
const {app, dialog} = electron;
function downloadURL(browserWindow, URL, callback) {
const {net} = electron;
const request = net.request(URL);
request.setHeader('Accept-Encoding', 'gzip,deflate');
request.on('response', (response) => {
const file = getAttachmentName(response.headers);
const dialogOptions = {
defaultPath: path.join(app.getPath('downloads'), file)
};
dialog.showSaveDialog(browserWindow, dialogOptions, (filename) => {
if (filename) {
saveResponseBody(response, filename, callback);
}
});
}).on('error', callback);
request.end();
}
function getAttachmentName(headers) {
if (headers['content-disposition']) {
const contentDisposition = headers['content-disposition'][0];
const matched = contentDisposition.match(/filename="(.*)"/);
if (matched) {
return path.basename(matched[1]);
}
}
return '';
}
function saveResponseBody(response, filename, callback) {
const output = fs.createWriteStream(filename);
output.on('close', callback);
switch (response.headers['content-encoding']) {
case 'gzip':
response.pipe(zlib.createGunzip()).pipe(output).on('error', callback);
break;
case 'deflate':
response.pipe(zlib.createInflate()).pipe(output).on('error', callback);
break;
default:
response.pipe(output).on('error', callback);
break;
}
}
module.exports = downloadURL;