summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorhmhealey <harrisonmhealey@gmail.com>2016-01-21 13:55:47 -0500
committerhmhealey <harrisonmhealey@gmail.com>2016-01-21 13:55:47 -0500
commit261b38be45420b45f97842ec55247ea7b47a527e (patch)
treec4cc8ed77bc6e0d60c80676c5e59a3821b576e11
parent302f210d66fd0850852aa2e636df14509688ee18 (diff)
downloadchat-261b38be45420b45f97842ec55247ea7b47a527e.tar.gz
chat-261b38be45420b45f97842ec55247ea7b47a527e.tar.bz2
chat-261b38be45420b45f97842ec55247ea7b47a527e.zip
Added download param to getFile api call
-rw-r--r--api/file.go19
-rw-r--r--web/react/components/view_image.jsx3
-rw-r--r--web/react/utils/utils.jsx5
3 files changed, 16 insertions, 11 deletions
diff --git a/api/file.go b/api/file.go
index 46e81691e..bfaca915f 100644
--- a/api/file.go
+++ b/api/file.go
@@ -379,6 +379,7 @@ func getFile(c *Context, w http.ResponseWriter, r *http.Request) {
hash := r.URL.Query().Get("h")
data := r.URL.Query().Get("d")
teamId := r.URL.Query().Get("t")
+ isDownload := r.URL.Query().Get("download") == "1"
cchan := Srv.Store.Channel().CheckPermissionsTo(c.Session.TeamId, channelId, c.Session.UserId)
@@ -420,16 +421,18 @@ func getFile(c *Context, w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Length", strconv.Itoa(len(f)))
w.Header().Del("Content-Type") // Content-Type will be set automatically by the http writer
- // attach extra headers to trigger a download on IE, Edge, and Safari
- ua := user_agent.New(r.UserAgent())
- bname, _ := ua.Browser()
+ if isDownload {
+ // attach extra headers to trigger a download on IE, Edge, and Safari
+ ua := user_agent.New(r.UserAgent())
+ bname, _ := ua.Browser()
- if bname == "Edge" || bname == "Internet Explorer" || bname == "Safari" {
- // trim off anything before the final / so we just get the file's name
- parts := strings.Split(filename, "/")
+ if bname == "Edge" || bname == "Internet Explorer" || bname == "Safari" {
+ // trim off anything before the final / so we just get the file's name
+ parts := strings.Split(filename, "/")
- w.Header().Set("Content-Type", "application/octet-stream")
- w.Header().Set("Content-Disposition", "attachment;filename=\""+parts[len(parts)-1]+"\"")
+ w.Header().Set("Content-Type", "application/octet-stream")
+ w.Header().Set("Content-Disposition", "attachment;filename=\""+parts[len(parts)-1]+"\"")
+ }
}
w.Write(f)
diff --git a/web/react/components/view_image.jsx b/web/react/components/view_image.jsx
index 31ec91248..d11f8a21c 100644
--- a/web/react/components/view_image.jsx
+++ b/web/react/components/view_image.jsx
@@ -211,7 +211,7 @@ export default class ViewImageModal extends React.Component {
}
const filename = this.props.filenames[this.state.imgId];
- const fileUrl = Utils.getFileUrl(filename);
+ const fileUrl = Utils.getFileUrl(filename, true);
var content;
if (this.state.loaded[this.state.imgId]) {
@@ -377,6 +377,7 @@ function ImagePreview({filename, fileUrl, fileInfo, maxHeight}) {
<a
href={fileUrl}
target='_blank'
+ download={true}
>
<img
style={{maxHeight}}
diff --git a/web/react/utils/utils.jsx b/web/react/utils/utils.jsx
index 71fd0852b..1b31adf69 100644
--- a/web/react/utils/utils.jsx
+++ b/web/react/utils/utils.jsx
@@ -1091,8 +1091,9 @@ export function fileSizeToString(bytes) {
}
// Converts a filename (like those attached to Post objects) to a url that can be used to retrieve attachments from the server.
-export function getFileUrl(filename) {
- return getWindowLocationOrigin() + '/api/v1/files/get' + filename + '?' + getSessionIndex();
+export function getFileUrl(filename, isDownload) {
+ const downloadParam = isDownload ? '&download=1' : '';
+ return getWindowLocationOrigin() + '/api/v1/files/get' + filename + '?' + getSessionIndex() + downloadParam;
}
// Gets the name of a file (including extension) from a given url or file path.