solve some back end bugs

Former-commit-id: de26609879e875b21de329588ecd1dcb44d152f3 [formerly 78b120bf0d33345808a422980db55d33c52304b7] [formerly d77c47bb41c1a7bf6ee2b2522bf7c86638d087bc [formerly 2819ab24b8]]
Former-commit-id: 3ddee564ddd5ed4fde01ed95f30386115a07df78 [formerly a3a1da0357874b7d99a88bd785c8f60f8170f663]
Former-commit-id: 0e15a59e28993f8178fa9409a466ce93a9936906
This commit is contained in:
Henrique Dias
2017-06-29 10:17:35 +01:00
parent a7b50c2de1
commit bff33c2c1e
10 changed files with 207 additions and 282 deletions

View File

@@ -2,18 +2,17 @@ package filemanager
import (
"net/http"
"strconv"
)
func serveDefault(c *requestContext, w http.ResponseWriter, r *http.Request) (int, error) {
var err error
// Starts building the page.
c.pg = &page{
Name: c.fi.Name,
Path: c.fi.VirtualPath,
User: c.us,
BaseURL: c.fm.RootURL(),
WebDavURL: c.fm.WebDavURL(),
Data: c.fi,
}
// If it is a dir, go and serve the listing.
@@ -26,27 +25,15 @@ func serveDefault(c *requestContext, w http.ResponseWriter, r *http.Request) (in
return errorToHTTP(err, true), err
}
// If it is a text file, reads its content.
if c.fi.Type == "text" {
if err = c.fi.Read(); 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.
if !c.fi.CanBeEdited() || !c.us.AllowEdit {
if c.fi.Type == "text" {
c.fi.Content = string(c.fi.content)
}
c.pg.Kind = "preview"
c.pg.Data = c.fi
} else {
// Otherwise, we just bring the editor in!
c.pg.Kind = "editor"
c.pg.Data, err = getEditor(r, c.fi)
err = c.fi.getEditor(r)
if err != nil {
return http.StatusInternalServerError, err
}
@@ -57,40 +44,31 @@ func serveDefault(c *requestContext, w http.ResponseWriter, r *http.Request) (in
// serveListing presents the user with a listage of a directory folder.
func serveListing(c *requestContext, w http.ResponseWriter, r *http.Request) (int, error) {
var (
err error
listing *listing
)
var err error
c.pg.Kind = "listing"
listing, err = getListing(c.us, c.fi.VirtualPath, c.fm.RootURL()+r.URL.Path, c.fi)
err = c.fi.getListing(c, r)
if err != nil {
return errorToHTTP(err, true), err
}
listing := c.fi.listing
cookieScope := c.fm.RootURL()
if cookieScope == "" {
cookieScope = "/"
}
// Copy the query values into the Listing struct
var limit int
listing.Sort, listing.Order, limit, err = handleSortOrder(w, r, cookieScope)
listing.Sort, listing.Order, err = handleSortOrder(w, r, cookieScope)
if err != nil {
return http.StatusBadRequest, err
}
listing.ApplySort()
if limit > 0 && limit <= len(listing.Items) {
listing.Items = listing.Items[:limit]
listing.ItemsLimitedTo = limit
}
listing.Display = displayMode(w, r, cookieScope)
c.pg.Data = listing
return c.pg.Render(c, w, r)
}
@@ -121,10 +99,9 @@ func displayMode(w http.ResponseWriter, r *http.Request, scope string) string {
// handleSortOrder gets and stores for a Listing the 'sort' and 'order',
// and reads 'limit' if given. The latter is 0 if not given. Sets cookies.
func handleSortOrder(w http.ResponseWriter, r *http.Request, scope string) (sort string, order string, limit int, err error) {
func handleSortOrder(w http.ResponseWriter, r *http.Request, scope string) (sort string, order string, err error) {
sort = r.URL.Query().Get("sort")
order = r.URL.Query().Get("order")
limitQuery := r.URL.Query().Get("limit")
// If the query 'sort' or 'order' is empty, use defaults or any values
// previously saved in Cookies.
@@ -158,13 +135,5 @@ func handleSortOrder(w http.ResponseWriter, r *http.Request, scope string) (sort
})
}
if limitQuery != "" {
limit, err = strconv.Atoi(limitQuery)
// If the 'limit' query can't be interpreted as a number, return err.
if err != nil {
return
}
}
return
}