From 4eb512f012f79bd4ecdd8cb6ce56bcf51940034f Mon Sep 17 00:00:00 2001 From: Henrique Dias Date: Wed, 26 Jul 2017 09:44:09 +0100 Subject: [PATCH] Small code improvements Former-commit-id: 9ee21a83fd926f2855d12eed60d9bf4f54191afe [formerly e142c5a46ee923cf76a04f9d75551fb291f663b0] [formerly 371878ef38e2dc56f5037ea5bb829bf746102f40 [formerly 1bcfa022cfccfca2cbabe9b31e42784b7790bc2e]] Former-commit-id: d980c4524e5bc3be339902dcdb4aef6e7c25f5a0 [formerly 36a7e2a81f1dba82e84961175533a2d1627b209d] Former-commit-id: 05b63304e221dba9beeed4c917722f3478c73e3c --- file.go | 6 +++--- resource.go | 51 ++++++++++++++++++++++++++++----------------------- 2 files changed, 31 insertions(+), 26 deletions(-) diff --git a/file.go b/file.go index 5cd799f0..4cf178cf 100644 --- a/file.go +++ b/file.go @@ -159,7 +159,7 @@ func (i *file) getListing(c *RequestContext, r *http.Request) error { Path: filepath.Join(i.Path, name), } - i.RetrieveFileType(false) + i.GetFileType(false) fileinfos = append(fileinfos, i) } @@ -200,9 +200,9 @@ func (i *file) getEditor() error { return nil } -// RetrieveFileType obtains the mimetype and converts it to a simple +// GetFileType obtains the mimetype and converts it to a simple // type nomenclature. -func (i *file) RetrieveFileType(checkContent bool) error { +func (i *file) GetFileType(checkContent bool) error { var content []byte var err error diff --git a/resource.go b/resource.go index 51b3954c..34a673ac 100644 --- a/resource.go +++ b/resource.go @@ -52,14 +52,14 @@ func resourceHandler(c *RequestContext, w http.ResponseWriter, r *http.Request) } func resourceGetHandler(c *RequestContext, w http.ResponseWriter, r *http.Request) (int, error) { - // Obtains the information of the directory/file. + // Gets the information of the directory/file. f, err := getInfo(r.URL, c.FM, c.User) if err != nil { return errorToHTTP(err, false), err } // If it's a dir and the path doesn't end with a trailing slash, - // redirect the user. + // add a trailing slash to the path. if f.IsDir && !strings.HasSuffix(r.URL.Path, "/") { r.URL.Path = r.URL.Path + "/" } @@ -71,22 +71,23 @@ func resourceGetHandler(c *RequestContext, w http.ResponseWriter, r *http.Reques } // Tries to get the file type. - if err = f.RetrieveFileType(true); err != nil { + if err = f.GetFileType(true); err != nil { return errorToHTTP(err, true), err } - // If it can't be edited or the user isn't allowed to, - // serve it as a listing, with a preview of the file. + // Serve a preview if the file can't be edited or the + // user has no permission to edit this file. Otherwise, + // just serve the editor. if !f.CanBeEdited() || !c.User.AllowEdit { f.Kind = "preview" - } else { - // Otherwise, we just bring the editor in! - f.Kind = "editor" + return renderJSON(w, f) + } - err = f.getEditor() - if err != nil { - return http.StatusInternalServerError, err - } + f.Kind = "editor" + + // Tries to get the editor data. + if err = f.getEditor(); err != nil { + return http.StatusInternalServerError, err } return renderJSON(w, f) @@ -96,21 +97,24 @@ func listingHandler(c *RequestContext, w http.ResponseWriter, r *http.Request) ( f := c.FI f.Kind = "listing" - err := f.getListing(c, r) - if err != nil { + // Tries to get the listing data. + if err := f.getListing(c, r); err != nil { return errorToHTTP(err, true), err } listing := f.listing + // Defines the cookie scope. cookieScope := c.FM.RootURL() if cookieScope == "" { cookieScope = "/" } // Copy the query values into the Listing struct - listing.Sort, listing.Order, err = handleSortOrder(w, r, cookieScope) - if err != nil { + if sort, order, err := handleSortOrder(w, r, cookieScope); err == nil { + listing.Sort = sort + listing.Order = order + } else { return http.StatusBadRequest, err } @@ -191,6 +195,7 @@ func resourcePostPutHandler(c *RequestContext, w http.ResponseWriter, r *http.Re return http.StatusOK, nil } +// resourcePatchHandler is the entry point for resource handler. func resourcePatchHandler(c *RequestContext, w http.ResponseWriter, r *http.Request) (int, error) { if !c.User.AllowEdit { return http.StatusForbidden, nil @@ -212,21 +217,21 @@ func resourcePatchHandler(c *RequestContext, w http.ResponseWriter, r *http.Requ return errorToHTTP(err, true), err } -// displayMode obtaisn the display mode from URL, or from the -// cookie. +// displayMode obtains the display mode from the Cookie. func displayMode(w http.ResponseWriter, r *http.Request, scope string) string { - displayMode := r.URL.Query().Get("display") + var displayMode string - if displayMode == "" { - if displayCookie, err := r.Cookie("display"); err == nil { - displayMode = displayCookie.Value - } + // Checks the cookie. + if displayCookie, err := r.Cookie("display"); err == nil { + displayMode = displayCookie.Value } + // If it's invalid, set it to mosaic, which is the default. if displayMode == "" || (displayMode != "mosaic" && displayMode != "list") { displayMode = "mosaic" } + // Set the cookie. http.SetCookie(w, &http.Cookie{ Name: "display", Value: displayMode,