mirror of
https://github.com/filebrowser/filebrowser.git
synced 2025-05-08 19:22:57 +00:00

Former-commit-id: 8220f5660a75af69ee6358af3782416dae9aa185 [formerly 098749492c954879b5a01324076804e74947c780] [formerly 34f6dda3fafc72730b4e44d0c23ade6940bb90d4 [formerly 4b3ebca48c26220825e07a5f1f9c1718911532a4]] Former-commit-id: 722e71cd19b9ecceb627cc1e65572839bd2a55c8 [formerly 23d5ac81b9ff1f39b51cc042eb2ac12d2e5da6fc] Former-commit-id: c4e5ea0d6db7edcfdc3b551e0c43d1c77ef587c3
67 lines
1.5 KiB
Go
67 lines
1.5 KiB
Go
package filemanager
|
|
|
|
import (
|
|
"net/http"
|
|
"os"
|
|
"strings"
|
|
)
|
|
|
|
// requestContext contains the needed information to make handlers work.
|
|
type requestContext struct {
|
|
us *User
|
|
fm *FileManager
|
|
fi *fileInfo
|
|
}
|
|
|
|
// responseWriterNoBody is a wrapper used to suprress the body of the response
|
|
// to a request. Mainly used for HEAD requests.
|
|
type responseWriterNoBody struct {
|
|
http.ResponseWriter
|
|
}
|
|
|
|
// newResponseWriterNoBody creates a new responseWriterNoBody.
|
|
func newResponseWriterNoBody(w http.ResponseWriter) *responseWriterNoBody {
|
|
return &responseWriterNoBody{w}
|
|
}
|
|
|
|
// Header executes the Header method from the http.ResponseWriter.
|
|
func (w responseWriterNoBody) Header() http.Header {
|
|
return w.ResponseWriter.Header()
|
|
}
|
|
|
|
// Write suprresses the body.
|
|
func (w responseWriterNoBody) Write(data []byte) (int, error) {
|
|
return 0, nil
|
|
}
|
|
|
|
// WriteHeader writes the header to the http.ResponseWriter.
|
|
func (w responseWriterNoBody) WriteHeader(statusCode int) {
|
|
w.ResponseWriter.WriteHeader(statusCode)
|
|
}
|
|
|
|
// matchURL checks if the first URL matches the second.
|
|
func matchURL(first, second string) bool {
|
|
first = strings.ToLower(first)
|
|
second = strings.ToLower(second)
|
|
|
|
return strings.HasPrefix(first, second)
|
|
}
|
|
|
|
// errorToHTTP converts errors to HTTP Status Code.
|
|
func errorToHTTP(err error, gone bool) int {
|
|
switch {
|
|
case os.IsPermission(err):
|
|
return http.StatusForbidden
|
|
case os.IsNotExist(err):
|
|
if !gone {
|
|
return http.StatusNotFound
|
|
}
|
|
|
|
return http.StatusGone
|
|
case os.IsExist(err):
|
|
return http.StatusGone
|
|
default:
|
|
return http.StatusInternalServerError
|
|
}
|
|
}
|