Changed getFile api call to always attach headers

Этот коммит содержится в:
Harrison Healey
2016-04-08 18:06:35 -04:00
родитель df77179ecc
Коммит 3803750fb1
5 изменённых файлов: 18 добавлений и 18 удалений

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

@@ -379,7 +379,6 @@ 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)
@@ -419,21 +418,23 @@ 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
if isDownload { // attach extra headers to trigger a download on IE, Edge, and Safari
// attach extra headers to trigger a download on IE, Edge, and Safari ua := user_agent.New(r.UserAgent())
ua := user_agent.New(r.UserAgent()) bname, _ := ua.Browser()
bname, _ := ua.Browser()
parts := strings.Split(filename, "/") parts := strings.Split(filename, "/")
filePart := strings.Split(parts[len(parts)-1], "?")[0] filePart := strings.Split(parts[len(parts)-1], "?")[0]
w.Header().Set("Content-Disposition", "attachment;filename=\""+filePart+"\"") w.Header().Set("Content-Disposition", "attachment;filename=\""+filePart+"\"")
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
w.Header().Set("Content-Type", "application/octet-stream") w.Header().Set("Content-Type", "application/octet-stream")
}
} }
// prevent file links from being embedded in iframes
w.Header().Set("X-Frame-Options", "DENY")
w.Header().Set("Content-Security-Policy", "Frame-ancestors 'none'")
w.Write(f) w.Write(f)
} }

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

@@ -54,7 +54,7 @@
"FileSettings": { "FileSettings": {
"DriverName": "local", "DriverName": "local",
"Directory": "./data/", "Directory": "./data/",
"EnablePublicLink": true, "EnablePublicLink": false,
"PublicLinkSalt": "A705AklYF8MFDOfcwh3I488G8vtLlVip", "PublicLinkSalt": "A705AklYF8MFDOfcwh3I488G8vtLlVip",
"ThumbnailWidth": 120, "ThumbnailWidth": 120,
"ThumbnailHeight": 100, "ThumbnailHeight": 100,

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

@@ -130,7 +130,7 @@ class FileAttachment extends React.Component {
var filename = this.props.filename; var filename = this.props.filename;
var fileInfo = utils.splitFileLocation(filename); var fileInfo = utils.splitFileLocation(filename);
var fileUrl = utils.getFileUrl(filename, true); var fileUrl = utils.getFileUrl(filename);
var type = utils.getFileType(fileInfo.ext); var type = utils.getFileType(fileInfo.ext);
var thumbnail; var thumbnail;

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

@@ -228,7 +228,7 @@ 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, true); const fileUrl = Utils.getFileUrl(filename);
var content; var content;
if (this.state.loaded[this.state.imgId]) { if (this.state.loaded[this.state.imgId]) {

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

@@ -1110,9 +1110,8 @@ 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, isDownload) { export function getFileUrl(filename) {
const downloadParam = isDownload ? '?download=1' : ''; return getWindowLocationOrigin() + '/api/v1/files/get' + filename;
return getWindowLocationOrigin() + '/api/v1/files/get' + filename + 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.