Read https://github.com/filebrowser/filebrowser/pull/575.
This commit is contained in:
Henrique Dias
2019-01-05 22:44:33 +00:00
committed by GitHub
parent 013ddf45c7
commit d309066def
121 changed files with 5410 additions and 4697 deletions

39
http/utils.go Normal file
View File

@@ -0,0 +1,39 @@
package http
import (
"encoding/json"
"net/http"
"os"
"github.com/filebrowser/filebrowser/v2/errors"
)
func renderJSON(w http.ResponseWriter, r *http.Request, data interface{}) (int, error) {
marsh, err := json.Marshal(data)
if err != nil {
return http.StatusInternalServerError, err
}
w.Header().Set("Content-Type", "application/json; charset=utf-8")
if _, err := w.Write(marsh); err != nil {
return http.StatusInternalServerError, err
}
return 0, nil
}
func errToStatus(err error) int {
switch {
case err == nil:
return http.StatusOK
case os.IsPermission(err):
return http.StatusForbidden
case os.IsNotExist(err), err == errors.ErrNotExist:
return http.StatusNotFound
case os.IsExist(err), err == errors.ErrExist:
return http.StatusConflict
default:
return http.StatusInternalServerError
}
}