2017-06-27 18:00:58 +00:00
|
|
|
package filemanager
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
)
|
|
|
|
|
|
|
|
func serveDefault(c *requestContext, w http.ResponseWriter, r *http.Request) (int, error) {
|
|
|
|
var err error
|
|
|
|
|
2017-06-29 09:17:35 +00:00
|
|
|
// Starts building the page.
|
2017-06-27 18:00:58 +00:00
|
|
|
c.pg = &page{
|
|
|
|
User: c.us,
|
|
|
|
BaseURL: c.fm.RootURL(),
|
|
|
|
WebDavURL: c.fm.WebDavURL(),
|
2017-06-29 09:17:35 +00:00
|
|
|
Data: c.fi,
|
2017-06-27 18:00:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// If it is a dir, go and serve the listing.
|
|
|
|
if c.fi.IsDir {
|
|
|
|
return serveListing(c, w, r)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tries to get the file type.
|
|
|
|
if err = c.fi.RetrieveFileType(); 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 {
|
2017-06-28 10:45:41 +00:00
|
|
|
c.pg.Kind = "preview"
|
|
|
|
} else {
|
|
|
|
// Otherwise, we just bring the editor in!
|
|
|
|
c.pg.Kind = "editor"
|
|
|
|
|
2017-06-29 09:17:35 +00:00
|
|
|
err = c.fi.getEditor(r)
|
2017-06-28 10:45:41 +00:00
|
|
|
if err != nil {
|
|
|
|
return http.StatusInternalServerError, err
|
|
|
|
}
|
2017-06-27 18:00:58 +00:00
|
|
|
}
|
|
|
|
|
2017-06-28 18:06:08 +00:00
|
|
|
return c.pg.Render(c, w, r)
|
2017-06-27 18:00:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// serveListing presents the user with a listage of a directory folder.
|
|
|
|
func serveListing(c *requestContext, w http.ResponseWriter, r *http.Request) (int, error) {
|
2017-06-29 09:17:35 +00:00
|
|
|
var err error
|
2017-06-27 18:00:58 +00:00
|
|
|
|
2017-06-28 10:45:41 +00:00
|
|
|
c.pg.Kind = "listing"
|
2017-06-27 18:00:58 +00:00
|
|
|
|
2017-06-29 09:17:35 +00:00
|
|
|
err = c.fi.getListing(c, r)
|
2017-06-27 18:00:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return errorToHTTP(err, true), err
|
|
|
|
}
|
|
|
|
|
2017-06-29 09:17:35 +00:00
|
|
|
listing := c.fi.listing
|
|
|
|
|
2017-06-27 18:00:58 +00:00
|
|
|
cookieScope := c.fm.RootURL()
|
|
|
|
if cookieScope == "" {
|
|
|
|
cookieScope = "/"
|
|
|
|
}
|
|
|
|
|
|
|
|
// Copy the query values into the Listing struct
|
2017-06-29 09:17:35 +00:00
|
|
|
listing.Sort, listing.Order, err = handleSortOrder(w, r, cookieScope)
|
2017-06-27 18:00:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return http.StatusBadRequest, err
|
|
|
|
}
|
|
|
|
|
|
|
|
listing.ApplySort()
|
|
|
|
|
2017-06-28 15:05:30 +00:00
|
|
|
listing.Display = displayMode(w, r, cookieScope)
|
2017-06-28 18:06:08 +00:00
|
|
|
return c.pg.Render(c, w, r)
|
2017-06-27 18:00:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// displayMode obtaisn the display mode from URL, or from the
|
|
|
|
// cookie.
|
|
|
|
func displayMode(w http.ResponseWriter, r *http.Request, scope string) string {
|
|
|
|
displayMode := r.URL.Query().Get("display")
|
|
|
|
|
|
|
|
if displayMode == "" {
|
|
|
|
if displayCookie, err := r.Cookie("display"); err == nil {
|
|
|
|
displayMode = displayCookie.Value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if displayMode == "" || (displayMode != "mosaic" && displayMode != "list") {
|
|
|
|
displayMode = "mosaic"
|
|
|
|
}
|
|
|
|
|
|
|
|
http.SetCookie(w, &http.Cookie{
|
|
|
|
Name: "display",
|
|
|
|
Value: displayMode,
|
2017-06-30 16:49:05 +00:00
|
|
|
MaxAge: 31536000,
|
2017-06-27 18:00:58 +00:00
|
|
|
Path: scope,
|
|
|
|
Secure: r.TLS != nil,
|
|
|
|
})
|
|
|
|
|
|
|
|
return displayMode
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
2017-06-29 09:17:35 +00:00
|
|
|
func handleSortOrder(w http.ResponseWriter, r *http.Request, scope string) (sort string, order string, err error) {
|
2017-06-27 18:00:58 +00:00
|
|
|
sort = r.URL.Query().Get("sort")
|
|
|
|
order = r.URL.Query().Get("order")
|
|
|
|
|
|
|
|
// If the query 'sort' or 'order' is empty, use defaults or any values
|
|
|
|
// previously saved in Cookies.
|
|
|
|
switch sort {
|
|
|
|
case "":
|
|
|
|
sort = "name"
|
|
|
|
if sortCookie, sortErr := r.Cookie("sort"); sortErr == nil {
|
|
|
|
sort = sortCookie.Value
|
|
|
|
}
|
|
|
|
case "name", "size", "type":
|
|
|
|
http.SetCookie(w, &http.Cookie{
|
|
|
|
Name: "sort",
|
|
|
|
Value: sort,
|
2017-06-30 16:49:05 +00:00
|
|
|
MaxAge: 31536000,
|
2017-06-27 18:00:58 +00:00
|
|
|
Path: scope,
|
|
|
|
Secure: r.TLS != nil,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
switch order {
|
|
|
|
case "":
|
|
|
|
order = "asc"
|
|
|
|
if orderCookie, orderErr := r.Cookie("order"); orderErr == nil {
|
|
|
|
order = orderCookie.Value
|
|
|
|
}
|
|
|
|
case "asc", "desc":
|
|
|
|
http.SetCookie(w, &http.Cookie{
|
|
|
|
Name: "order",
|
|
|
|
Value: order,
|
2017-06-30 16:49:05 +00:00
|
|
|
MaxAge: 31536000,
|
2017-06-27 18:00:58 +00:00
|
|
|
Path: scope,
|
|
|
|
Secure: r.TLS != nil,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|