Return the mime type with the file for easier panel integration

This commit is contained in:
Dane Everitt 2019-05-25 15:58:55 -07:00
parent 8d8ec70683
commit c943246e33
No known key found for this signature in database
GPG Key ID: EEA66103B3D71F53

13
http.go
View File

@ -219,16 +219,19 @@ func (rt *Router) routeServerFileRead(w http.ResponseWriter, r *http.Request, ps
return
}
st, err := os.Stat(cleaned)
st, err := s.Filesystem.Stat(cleaned)
if err != nil {
if !os.IsNotExist(err) {
zap.S().Errorw("failed to stat file for reading", zap.String("path", ps.ByName("path")), zap.String("server", s.Uuid), zap.Error(err))
}
http.Error(w, "failed to stat file", http.StatusInternalServerError)
return
}
http.NotFound(w, r)
return
}
f, err := os.OpenFile(cleaned, os.O_RDONLY, 0)
if err != nil {
if !os.IsNotExist(err) {
@ -240,12 +243,14 @@ func (rt *Router) routeServerFileRead(w http.ResponseWriter, r *http.Request, ps
}
defer f.Close()
w.Header().Set("X-Mime-Type", st.Mimetype)
w.Header().Set("Content-Length", strconv.Itoa(int(st.Info.Size())))
// If a download parameter is included in the URL go ahead and attach the necessary headers
// so that the file can be downloaded.
if r.URL.Query().Get("download") != "" {
w.Header().Set("Content-Disposition", "attachment; filename="+st.Name())
w.Header().Set("Content-Disposition", "attachment; filename="+st.Info.Name())
w.Header().Set("Content-Type", "application/octet-stream")
w.Header().Set("Content-Length", strconv.Itoa(int(st.Size())))
}
bufio.NewReader(f).WriteTo(w)