more updates and renaming

Former-commit-id: 8c194821906b2170df92672fe9dfb9d710640659
This commit is contained in:
Henrique Dias
2017-06-19 17:10:03 +01:00
parent ae61d5b6aa
commit 4829870890
25 changed files with 8783 additions and 546 deletions

View File

@@ -13,7 +13,6 @@ import (
"os"
fm "github.com/hacdias/filemanager"
"github.com/hacdias/filemanager/utils"
)
// checksum calculates the hash of a filemanager. Supports MD5, SHA1, SHA256 and SHA512.
@@ -22,7 +21,7 @@ func checksum(w http.ResponseWriter, r *http.Request, c *fm.Config, i *fm.FileIn
file, err := os.Open(i.Path)
if err != nil {
return utils.ErrorToHTTPCode(err, true), err
return errorToHTTPCode(err, true), err
}
defer file.Close()

View File

@@ -10,8 +10,6 @@ import (
fm "github.com/hacdias/filemanager"
"github.com/hacdias/filemanager/assets"
"github.com/hacdias/filemanager/page"
"github.com/hacdias/filemanager/utils"
"github.com/hacdias/filemanager/wrapper"
"github.com/mholt/caddy/caddyhttp/httpserver"
)
@@ -74,7 +72,7 @@ func ServeHTTP(w http.ResponseWriter, r *http.Request, c *fm.Config) (int, error
r.Method = "PROPFIND"
if r.Method == "HEAD" {
w = wrapper.NewResponseWriterNoBody(w)
w = NewResponseWriterNoBody(w)
}
}
case "PROPPATCH", "MOVE", "PATCH", "PUT", "DELETE":
@@ -133,7 +131,7 @@ func ServeHTTP(w http.ResponseWriter, r *http.Request, c *fm.Config) (int, error
if r.Method == http.MethodGet {
// Gets the information of the directory/file
fi, err = fm.GetInfo(r.URL, c, user)
code = utils.ErrorToHTTPCode(err, false)
code = errorToHTTPCode(err, false)
if err != nil {
if r.Method == http.MethodGet {
return page.PrintErrorHTML(w, code, err)
@@ -171,3 +169,21 @@ func ServeHTTP(w http.ResponseWriter, r *http.Request, c *fm.Config) (int, error
return http.StatusNotImplemented, nil
}
// errorToHTTPCode converts errors to HTTP Status Code.
func errorToHTTPCode(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
}
}

View File

@@ -8,7 +8,6 @@ import (
fm "github.com/hacdias/filemanager"
"github.com/hacdias/filemanager/page"
"github.com/hacdias/filemanager/utils"
"github.com/mholt/caddy/caddyhttp/httpserver"
)
@@ -19,7 +18,7 @@ func serveListing(w http.ResponseWriter, r *http.Request, c *fm.Config, u *fm.Us
// Loads the content of the directory
listing, err := fm.GetListing(u, i.VirtualPath, c.PrefixURL+r.URL.Path)
if err != nil {
return utils.ErrorToHTTPCode(err, true), err
return errorToHTTPCode(err, true), err
}
listing.Context = httpserver.Context{

29
http/response_writer.go Normal file
View File

@@ -0,0 +1,29 @@
package http
import "net/http"
// 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)
}

View File

@@ -6,7 +6,6 @@ import (
fm "github.com/hacdias/filemanager"
"github.com/hacdias/filemanager/page"
"github.com/hacdias/filemanager/utils"
)
// serveSingle serves a single file in an editor (if it is editable), shows the
@@ -15,7 +14,7 @@ func serveSingle(w http.ResponseWriter, r *http.Request, c *fm.Config, u *fm.Use
var err error
if err = i.RetrieveFileType(); err != nil {
return utils.ErrorToHTTPCode(err, true), err
return errorToHTTPCode(err, true), err
}
p := &page.Page{
@@ -36,7 +35,7 @@ func serveSingle(w http.ResponseWriter, r *http.Request, c *fm.Config, u *fm.Use
if i.Type == "text" {
if err = i.Read(); err != nil {
return utils.ErrorToHTTPCode(err, true), err
return errorToHTTPCode(err, true), err
}
}