filebrowser/file/listing.go

146 lines
3.2 KiB
Go
Raw Normal View History

2016-10-18 20:49:46 +00:00
package file
2016-06-11 22:01:24 +00:00
import (
2016-10-18 20:06:31 +00:00
"encoding/json"
2016-06-11 22:01:24 +00:00
"net/http"
2016-10-18 20:06:31 +00:00
"net/url"
"os"
"path"
2016-06-11 22:01:24 +00:00
"strings"
2016-10-18 20:06:31 +00:00
"github.com/hacdias/caddy-filemanager/config"
2016-10-18 20:30:10 +00:00
"github.com/hacdias/caddy-filemanager/page"
"github.com/hacdias/caddy-filemanager/utils"
2016-06-11 22:01:24 +00:00
"github.com/mholt/caddy/caddyhttp/httpserver"
)
// A Listing is the context used to fill out a template.
type Listing struct {
// The name of the directory (the last element of the path)
Name string
// The full path of the request
Path string
// The items (files and folders) in the path
2016-10-18 20:49:46 +00:00
Items []Info
2016-06-11 22:01:24 +00:00
// The number of directories in the listing
NumDirs int
// The number of files (items that aren't directories) in the listing
NumFiles int
// Which sorting order is used
Sort string
// And which order
Order string
// If ≠0 then Items have been limited to that many elements
2016-08-11 21:19:21 +00:00
ItemsLimitedTo int
httpserver.Context `json:"-"`
2016-06-11 22:01:24 +00:00
}
2016-10-18 20:49:46 +00:00
func (i *Info) serveListing(w http.ResponseWriter, r *http.Request, c *config.Config, u *config.User) (int, error) {
2016-10-18 20:06:31 +00:00
var err error
2016-10-19 19:58:08 +00:00
// Gets the directory information using the Virtual File System of
// the user configuration
2016-10-18 20:06:31 +00:00
file, err := u.FileSystem.OpenFile(i.VirtualPath, os.O_RDONLY, 0)
if err != nil {
2016-10-18 20:30:10 +00:00
return utils.ErrorToHTTPCode(err, true), err
2016-10-18 20:06:31 +00:00
}
defer file.Close()
2016-10-19 19:58:08 +00:00
// Loads the content of the directory
2016-10-18 20:06:31 +00:00
listing, err := i.loadDirectoryContents(file, r.URL.Path, u)
if err != nil {
2016-10-19 19:58:08 +00:00
return utils.ErrorToHTTPCode(err, true), err
2016-10-18 20:06:31 +00:00
}
listing.Context = httpserver.Context{
Root: http.Dir(u.Scope),
Req: r,
URL: r.URL,
}
// Copy the query values into the Listing struct
var limit int
listing.Sort, listing.Order, limit, err = handleSortOrder(w, r, c.Scope)
if err != nil {
return http.StatusBadRequest, err
}
listing.applySort()
if limit > 0 && limit <= len(listing.Items) {
listing.Items = listing.Items[:limit]
listing.ItemsLimitedTo = limit
}
if strings.Contains(r.Header.Get("Accept"), "application/json") {
marsh, err := json.Marshal(listing.Items)
if err != nil {
return http.StatusInternalServerError, err
}
w.Header().Set("Content-Type", "application/json; charset=utf-8")
if _, err := w.Write(marsh); err != nil {
return http.StatusInternalServerError, err
}
return http.StatusOK, nil
}
2016-10-18 20:30:10 +00:00
page := &page.Page{
Info: &page.Info{
2016-10-18 20:06:31 +00:00
Name: listing.Name,
Path: i.VirtualPath,
IsDir: true,
User: u,
Config: c,
Data: listing,
},
}
if r.Header.Get("Minimal") == "true" {
page.Minimal = true
}
return page.PrintAsHTML(w, "listing")
}
2016-10-19 19:58:08 +00:00
func (i Info) loadDirectoryContents(file http.File, basePath string, u *config.User) (*Listing, error) {
2016-10-18 20:06:31 +00:00
files, err := file.Readdir(-1)
if err != nil {
return nil, err
}
var (
2016-10-18 20:49:46 +00:00
fileinfos []Info
2016-10-18 20:06:31 +00:00
dirCount, fileCount int
)
for _, f := range files {
name := f.Name()
if f.IsDir() {
name += "/"
dirCount++
} else {
fileCount++
}
// Absolute URL
url := url.URL{Path: basePath + name}
2016-10-18 20:49:46 +00:00
fileinfos = append(fileinfos, Info{
2016-10-18 20:06:31 +00:00
FileInfo: f,
URL: url.String(),
UserAllowed: u.Allowed(url.String()),
})
}
2016-10-19 19:58:08 +00:00
return &Listing{
Name: path.Base(i.VirtualPath),
Path: i.VirtualPath,
2016-10-18 20:06:31 +00:00
Items: fileinfos,
NumDirs: dirCount,
NumFiles: fileCount,
2016-10-19 19:58:08 +00:00
}, nil
2016-10-18 20:06:31 +00:00
}