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
|
||
|
}
|
||
|
}
|