Added download param to getFile api call

Этот коммит содержится в:
hmhealey
2016-01-21 13:55:47 -05:00
родитель 302f210d66
Коммит 261b38be45
3 изменённых файлов: 16 добавлений и 11 удалений

Просмотреть файл

@@ -379,6 +379,7 @@ func getFile(c *Context, w http.ResponseWriter, r *http.Request) {
hash := r.URL.Query().Get("h") hash := r.URL.Query().Get("h")
data := r.URL.Query().Get("d") data := r.URL.Query().Get("d")
teamId := r.URL.Query().Get("t") 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) 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().Set("Content-Length", strconv.Itoa(len(f)))
w.Header().Del("Content-Type") // Content-Type will be set automatically by the http writer 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 if isDownload {
ua := user_agent.New(r.UserAgent()) // attach extra headers to trigger a download on IE, Edge, and Safari
bname, _ := ua.Browser() ua := user_agent.New(r.UserAgent())
bname, _ := ua.Browser()
if bname == "Edge" || bname == "Internet Explorer" || bname == "Safari" { if bname == "Edge" || bname == "Internet Explorer" || bname == "Safari" {
// trim off anything before the final / so we just get the file's name // trim off anything before the final / so we just get the file's name
parts := strings.Split(filename, "/") parts := strings.Split(filename, "/")
w.Header().Set("Content-Type", "application/octet-stream") w.Header().Set("Content-Type", "application/octet-stream")
w.Header().Set("Content-Disposition", "attachment;filename=\""+parts[len(parts)-1]+"\"") w.Header().Set("Content-Disposition", "attachment;filename=\""+parts[len(parts)-1]+"\"")
}
} }
w.Write(f) w.Write(f)

Просмотреть файл

@@ -211,7 +211,7 @@ export default class ViewImageModal extends React.Component {
} }
const filename = this.props.filenames[this.state.imgId]; const filename = this.props.filenames[this.state.imgId];
const fileUrl = Utils.getFileUrl(filename); const fileUrl = Utils.getFileUrl(filename, true);
var content; var content;
if (this.state.loaded[this.state.imgId]) { if (this.state.loaded[this.state.imgId]) {
@@ -377,6 +377,7 @@ function ImagePreview({filename, fileUrl, fileInfo, maxHeight}) {
<a <a
href={fileUrl} href={fileUrl}
target='_blank' target='_blank'
download={true}
> >
<img <img
style={{maxHeight}} style={{maxHeight}}

Просмотреть файл

@@ -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. // 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) { export function getFileUrl(filename, isDownload) {
return getWindowLocationOrigin() + '/api/v1/files/get' + filename + '?' + getSessionIndex(); 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. // Gets the name of a file (including extension) from a given url or file path.