Small code improvements
parent
fc02e7ef57
commit
1bcfa022cf
6
file.go
6
file.go
|
@ -159,7 +159,7 @@ func (i *file) getListing(c *RequestContext, r *http.Request) error {
|
||||||
Path: filepath.Join(i.Path, name),
|
Path: filepath.Join(i.Path, name),
|
||||||
}
|
}
|
||||||
|
|
||||||
i.RetrieveFileType(false)
|
i.GetFileType(false)
|
||||||
fileinfos = append(fileinfos, i)
|
fileinfos = append(fileinfos, i)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -200,9 +200,9 @@ func (i *file) getEditor() error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// RetrieveFileType obtains the mimetype and converts it to a simple
|
// GetFileType obtains the mimetype and converts it to a simple
|
||||||
// type nomenclature.
|
// type nomenclature.
|
||||||
func (i *file) RetrieveFileType(checkContent bool) error {
|
func (i *file) GetFileType(checkContent bool) error {
|
||||||
var content []byte
|
var content []byte
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
|
|
51
resource.go
51
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) {
|
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)
|
f, err := getInfo(r.URL, c.FM, c.User)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errorToHTTP(err, false), err
|
return errorToHTTP(err, false), err
|
||||||
}
|
}
|
||||||
|
|
||||||
// If it's a dir and the path doesn't end with a trailing slash,
|
// 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, "/") {
|
if f.IsDir && !strings.HasSuffix(r.URL.Path, "/") {
|
||||||
r.URL.Path = 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.
|
// 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
|
return errorToHTTP(err, true), err
|
||||||
}
|
}
|
||||||
|
|
||||||
// If it can't be edited or the user isn't allowed to,
|
// Serve a preview if the file can't be edited or the
|
||||||
// serve it as a listing, with a preview of the file.
|
// user has no permission to edit this file. Otherwise,
|
||||||
|
// just serve the editor.
|
||||||
if !f.CanBeEdited() || !c.User.AllowEdit {
|
if !f.CanBeEdited() || !c.User.AllowEdit {
|
||||||
f.Kind = "preview"
|
f.Kind = "preview"
|
||||||
} else {
|
return renderJSON(w, f)
|
||||||
// Otherwise, we just bring the editor in!
|
}
|
||||||
f.Kind = "editor"
|
|
||||||
|
|
||||||
err = f.getEditor()
|
f.Kind = "editor"
|
||||||
if err != nil {
|
|
||||||
return http.StatusInternalServerError, err
|
// Tries to get the editor data.
|
||||||
}
|
if err = f.getEditor(); err != nil {
|
||||||
|
return http.StatusInternalServerError, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return renderJSON(w, f)
|
return renderJSON(w, f)
|
||||||
|
@ -96,21 +97,24 @@ func listingHandler(c *RequestContext, w http.ResponseWriter, r *http.Request) (
|
||||||
f := c.FI
|
f := c.FI
|
||||||
f.Kind = "listing"
|
f.Kind = "listing"
|
||||||
|
|
||||||
err := f.getListing(c, r)
|
// Tries to get the listing data.
|
||||||
if err != nil {
|
if err := f.getListing(c, r); err != nil {
|
||||||
return errorToHTTP(err, true), err
|
return errorToHTTP(err, true), err
|
||||||
}
|
}
|
||||||
|
|
||||||
listing := f.listing
|
listing := f.listing
|
||||||
|
|
||||||
|
// Defines the cookie scope.
|
||||||
cookieScope := c.FM.RootURL()
|
cookieScope := c.FM.RootURL()
|
||||||
if cookieScope == "" {
|
if cookieScope == "" {
|
||||||
cookieScope = "/"
|
cookieScope = "/"
|
||||||
}
|
}
|
||||||
|
|
||||||
// Copy the query values into the Listing struct
|
// Copy the query values into the Listing struct
|
||||||
listing.Sort, listing.Order, err = handleSortOrder(w, r, cookieScope)
|
if sort, order, err := handleSortOrder(w, r, cookieScope); err == nil {
|
||||||
if err != nil {
|
listing.Sort = sort
|
||||||
|
listing.Order = order
|
||||||
|
} else {
|
||||||
return http.StatusBadRequest, err
|
return http.StatusBadRequest, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -191,6 +195,7 @@ func resourcePostPutHandler(c *RequestContext, w http.ResponseWriter, r *http.Re
|
||||||
return http.StatusOK, nil
|
return http.StatusOK, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// resourcePatchHandler is the entry point for resource handler.
|
||||||
func resourcePatchHandler(c *RequestContext, w http.ResponseWriter, r *http.Request) (int, error) {
|
func resourcePatchHandler(c *RequestContext, w http.ResponseWriter, r *http.Request) (int, error) {
|
||||||
if !c.User.AllowEdit {
|
if !c.User.AllowEdit {
|
||||||
return http.StatusForbidden, nil
|
return http.StatusForbidden, nil
|
||||||
|
@ -212,21 +217,21 @@ func resourcePatchHandler(c *RequestContext, w http.ResponseWriter, r *http.Requ
|
||||||
return errorToHTTP(err, true), err
|
return errorToHTTP(err, true), err
|
||||||
}
|
}
|
||||||
|
|
||||||
// displayMode obtaisn the display mode from URL, or from the
|
// displayMode obtains the display mode from the Cookie.
|
||||||
// cookie.
|
|
||||||
func displayMode(w http.ResponseWriter, r *http.Request, scope string) string {
|
func displayMode(w http.ResponseWriter, r *http.Request, scope string) string {
|
||||||
displayMode := r.URL.Query().Get("display")
|
var displayMode string
|
||||||
|
|
||||||
if displayMode == "" {
|
// Checks the cookie.
|
||||||
if displayCookie, err := r.Cookie("display"); err == nil {
|
if displayCookie, err := r.Cookie("display"); err == nil {
|
||||||
displayMode = displayCookie.Value
|
displayMode = displayCookie.Value
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// If it's invalid, set it to mosaic, which is the default.
|
||||||
if displayMode == "" || (displayMode != "mosaic" && displayMode != "list") {
|
if displayMode == "" || (displayMode != "mosaic" && displayMode != "list") {
|
||||||
displayMode = "mosaic"
|
displayMode = "mosaic"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Set the cookie.
|
||||||
http.SetCookie(w, &http.Cookie{
|
http.SetCookie(w, &http.Cookie{
|
||||||
Name: "display",
|
Name: "display",
|
||||||
Value: displayMode,
|
Value: displayMode,
|
||||||
|
|
Loading…
Reference in New Issue