Skip to content

Commit

Permalink
[FIX] Attachment download button behavior (#15172)
Browse files Browse the repository at this point in the history
  • Loading branch information
tassoevan authored and sampaiodiego committed Aug 13, 2019
1 parent 045826b commit 6f30e70
Show file tree
Hide file tree
Showing 7 changed files with 3,023 additions and 3,008 deletions.
28 changes: 13 additions & 15 deletions app/file-upload/server/config/AmazonS3.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,21 @@ import { FileUploadClass, FileUpload } from '../lib/FileUpload';
import '../../ufs/AmazonS3/server.js';

const get = function(file, req, res) {
const fileUrl = this.store.getRedirectURL(file);
const forceDownload = typeof req.query.download !== 'undefined';
const fileUrl = this.store.getRedirectURL(file, forceDownload);

if (fileUrl) {
const storeType = file.store.split(':').pop();
if (settings.get(`FileUpload_S3_Proxy_${ storeType }`)) {
const request = /^https:/.test(fileUrl) ? https : http;
request.get(fileUrl, (fileRes) => fileRes.pipe(res));
} else {
res.removeHeader('Content-Length');
res.removeHeader('Cache-Control');
res.setHeader('Location', fileUrl);
res.writeHead(302);
res.end();
}
} else {
res.end();
if (!fileUrl) {
return res.end();
}

const storeType = file.store.split(':').pop();
if (settings.get(`FileUpload_S3_Proxy_${ storeType }`)) {
const request = /^https:/.test(fileUrl) ? https : http;

return FileUpload.proxyFile(file.name, fileUrl, forceDownload, request, req, res);
}

return FileUpload.redirectToFile(fileUrl, req, res);
};

const copy = function(file, out) {
Expand Down
33 changes: 16 additions & 17 deletions app/file-upload/server/config/GoogleStorage.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,30 @@ import { settings } from '../../../settings';
import '../../ufs/GoogleStorage/server.js';

const get = function(file, req, res) {
this.store.getRedirectURL(file, (err, fileUrl) => {
const forceDownload = typeof req.query.download !== 'undefined';

this.store.getRedirectURL(file, forceDownload, (err, fileUrl) => {
if (err) {
console.error(err);
return console.error(err);
}

if (fileUrl) {
const storeType = file.store.split(':').pop();
if (settings.get(`FileUpload_GoogleStorage_Proxy_${ storeType }`)) {
const request = /^https:/.test(fileUrl) ? https : http;
request.get(fileUrl, (fileRes) => fileRes.pipe(res));
} else {
res.removeHeader('Content-Length');
res.removeHeader('Cache-Control');
res.setHeader('Location', fileUrl);
res.writeHead(302);
res.end();
}
} else {
res.end();
if (!fileUrl) {
return res.end();
}

const storeType = file.store.split(':').pop();
if (settings.get(`FileUpload_GoogleStorage_Proxy_${ storeType }`)) {
const request = /^https:/.test(fileUrl) ? https : http;

return FileUpload.proxyFile(file.name, fileUrl, forceDownload, request, req, res);
}

return FileUpload.redirectToFile(fileUrl, req, res);
});
};

const copy = function(file, out) {
this.store.getRedirectURL(file, (err, fileUrl) => {
this.store.getRedirectURL(file, false, (err, fileUrl) => {
if (err) {
console.error(err);
}
Expand Down
14 changes: 14 additions & 0 deletions app/file-upload/server/lib/FileUpload.js
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,20 @@ export const FileUpload = {

return false;
},

redirectToFile(fileUrl, req, res) {
res.removeHeader('Content-Length');
res.removeHeader('Cache-Control');
res.setHeader('Location', fileUrl);
res.writeHead(302);
res.end();
},

proxyFile(fileName, fileUrl, forceDownload, request, req, res) {
res.setHeader('Content-Disposition', `${ forceDownload ? 'attachment' : 'inline' }; filename="${ encodeURI(fileName) }"`);

request.get(fileUrl, (fileRes) => fileRes.pipe(res));
},
};

export class FileUploadClass {
Expand Down
4 changes: 2 additions & 2 deletions app/file-upload/ufs/AmazonS3/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,11 @@ export class AmazonS3Store extends UploadFS.Store {
}
};

this.getRedirectURL = function(file) {
this.getRedirectURL = function(file, forceDownload = false) {
const params = {
Key: this.getPath(file),
Expires: classOptions.URLExpiryTimeSpan,
ResponseContentDisposition: `${ forceDownload ? 'attachment' : 'inline' }; filename="${ encodeURI(file.name) }"`,
};

return s3.getSignedUrl('getObject', params);
Expand Down Expand Up @@ -140,7 +141,6 @@ export class AmazonS3Store extends UploadFS.Store {
Key: this.getPath(file),
Body: writeStream,
ContentType: file.type,
ContentDisposition: `inline; filename="${ encodeURI(file.name) }"`,

}, (error) => {
if (error) {
Expand Down
4 changes: 2 additions & 2 deletions app/file-upload/ufs/GoogleStorage/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ export class GoogleStorageStore extends UploadFS.Store {
}
};

this.getRedirectURL = function(file, callback) {
this.getRedirectURL = function(file, forceDownload = false, callback) {
const params = {
action: 'read',
responseDisposition: 'inline',
responseDisposition: forceDownload ? 'attachment' : 'inline',
expires: Date.now() + this.options.URLExpiryTimeSpan * 1000,
};

Expand Down
2 changes: 1 addition & 1 deletion app/message-attachments/client/messageAttachment.html
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
{{#if title_link}}
<a href="{{getURL title_link}}" target="_blank" rel="noopener noreferrer">{{title}}</a>
{{#if title_link_download}}
<a class="attachment-download-icon rc-tooltip" aria-label="{{_ 'Download'}}" href="{{getURL title_link}}" target="_blank" download="" rel="noopener noreferrer">{{> icon icon="download"}}</a>
<a class="attachment-download-icon rc-tooltip" aria-label="{{_ 'Download'}}" href="{{getURL title_link}}?download" target="_blank" download="{{title}}" rel="noopener noreferrer">{{> icon icon="download"}}</a>
{{/if}}
{{else}}
{{title}}
Expand Down
Loading

0 comments on commit 6f30e70

Please sign in to comment.