admin/api: bytes handlers now returned data as application/octet-stream

This commit is contained in:
nemunaire 2018-01-21 14:16:31 +01:00
parent 84774207c8
commit 69640506d0

View File

@ -24,8 +24,6 @@ func apiHandler(f DispatchFunction) func(http.ResponseWriter, *http.Request, htt
}
log.Printf("%s \"%s %s\" [%s]\n", r.RemoteAddr, r.Method, r.URL.Path, r.UserAgent())
w.Header().Set("Content-Type", "application/json")
var ret interface{}
var err error = nil
@ -64,14 +62,20 @@ func apiHandler(f DispatchFunction) func(http.ResponseWriter, *http.Request, htt
}
if str, found := ret.(string); found {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(resStatus)
io.WriteString(w, str)
} else if bts, found := ret.([]byte); found {
w.Header().Set("Content-Type", "application/octet-stream")
w.Header().Set("Content-Disposition", "attachment")
w.Header().Set("Content-Transfer-Encoding", "binary")
w.WriteHeader(resStatus)
w.Write(bts)
} else if j, err := json.Marshal(ret); err != nil {
w.Header().Set("Content-Type", "application/json")
http.Error(w, fmt.Sprintf("{\"errmsg\":%q}", err), http.StatusInternalServerError)
} else {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(resStatus)
w.Write(j)
}