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

Former-commit-id: 6f9843613e6abfe0b19e6e43f9299afb98645477 [formerly 805fa39b073401c946b559e0e967fdc553f16295] [formerly f9e2de337abc0cbeb5ccdb8b6962871b0bd3ee0e [formerly 1d26b8e95e73a94ba92673861c4e83dfded0d92e]] Former-commit-id: 259a1ba8cdc71e7d32add58f9487e5827aa6685e [formerly 161a1a49bc1158f11f07fc9ff638506dafb6c918] Former-commit-id: 67fe1fef4602baf4ddae40b28e65ac9a2a42ca9d
66 lines
1.5 KiB
Go
66 lines
1.5 KiB
Go
package filemanager
|
|
|
|
import (
|
|
"net/http"
|
|
"os"
|
|
"strings"
|
|
)
|
|
|
|
type requestContext struct {
|
|
User *User
|
|
FileManager *FileManager
|
|
Info *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
|
|
}
|
|
}
|