mirror of
https://github.com/caddyserver/caddy.git
synced 2025-01-22 10:25:46 +03:00
Merge pull request #772 from mholt/fix-browse
Make Browse Great Again ★★★
This commit is contained in:
commit
c3417a0757
3 changed files with 214 additions and 140 deletions
|
@ -3,6 +3,7 @@ package setup
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
"net/http"
|
||||||
"text/template"
|
"text/template"
|
||||||
|
|
||||||
"github.com/mholt/caddy/middleware"
|
"github.com/mholt/caddy/middleware"
|
||||||
|
@ -17,7 +18,6 @@ func Browse(c *Controller) (middleware.Middleware, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
browse := browse.Browse{
|
browse := browse.Browse{
|
||||||
Root: c.Root,
|
|
||||||
Configs: configs,
|
Configs: configs,
|
||||||
IgnoreIndexes: false,
|
IgnoreIndexes: false,
|
||||||
}
|
}
|
||||||
|
@ -50,6 +50,16 @@ func browseParse(c *Controller) ([]browse.Config, error) {
|
||||||
} else {
|
} else {
|
||||||
bc.PathScope = "/"
|
bc.PathScope = "/"
|
||||||
}
|
}
|
||||||
|
bc.Root = http.Dir(c.Root)
|
||||||
|
theRoot, err := bc.Root.Open("/") // catch a missing path early
|
||||||
|
if err != nil {
|
||||||
|
return configs, err
|
||||||
|
}
|
||||||
|
defer theRoot.Close()
|
||||||
|
_, err = theRoot.Readdir(-1)
|
||||||
|
if err != nil {
|
||||||
|
return configs, err
|
||||||
|
}
|
||||||
|
|
||||||
// Second argument would be the template file to use
|
// Second argument would be the template file to use
|
||||||
var tplText string
|
var tplText string
|
||||||
|
@ -85,7 +95,6 @@ const defaultTemplate = `<!DOCTYPE html>
|
||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<title>{{.Name}}</title>
|
<title>{{.Name}}</title>
|
||||||
<meta charset="utf-8">
|
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
<style>
|
<style>
|
||||||
* { padding: 0; margin: 0; }
|
* { padding: 0; margin: 0; }
|
||||||
|
@ -106,7 +115,7 @@ h1 a:hover {
|
||||||
}
|
}
|
||||||
|
|
||||||
header,
|
header,
|
||||||
.content {
|
#summary {
|
||||||
padding-left: 5%;
|
padding-left: 5%;
|
||||||
padding-right: 5%;
|
padding-right: 5%;
|
||||||
}
|
}
|
||||||
|
@ -306,43 +315,49 @@ footer {
|
||||||
</header>
|
</header>
|
||||||
<main>
|
<main>
|
||||||
<div class="meta">
|
<div class="meta">
|
||||||
<div class="content">
|
<div id="summary">
|
||||||
<span class="meta-item"><b>{{.NumDirs}}</b> director{{if eq 1 .NumDirs}}y{{else}}ies{{end}}</span>
|
<span class="meta-item"><b>{{.NumDirs}}</b> director{{if eq 1 .NumDirs}}y{{else}}ies{{end}}</span>
|
||||||
<span class="meta-item"><b>{{.NumFiles}}</b> file{{if ne 1 .NumFiles}}s{{end}}</span>
|
<span class="meta-item"><b>{{.NumFiles}}</b> file{{if ne 1 .NumFiles}}s{{end}}</span>
|
||||||
|
{{- if ne 0 .ItemsLimitedTo}}
|
||||||
|
<span class="meta-item">(of which only <b>{{.ItemsLimitedTo}}</b> are displayed)</span>
|
||||||
|
{{- end}}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div class="listing">
|
<div class="listing">
|
||||||
<table>
|
<table aria-describedby="summary">
|
||||||
|
<thead>
|
||||||
<tr>
|
<tr>
|
||||||
<th>
|
<th>
|
||||||
{{if and (eq .Sort "name") (ne .Order "desc")}}
|
{{- if and (eq .Sort "name") (ne .Order "desc")}}
|
||||||
<a href="?sort=name&order=desc">Name <svg width="1em" height=".4em" version="1.1" viewBox="0 0 12.922194 6.0358899"><use xlink:href="#up-arrow"></use></svg></a>
|
<a href="?sort=name&order=desc{{if ne 0 .ItemsLimitedTo}}&limit={{.ItemsLimitedTo}}{{end}}">Name <svg width="1em" height=".4em" version="1.1" viewBox="0 0 12.922194 6.0358899"><use xlink:href="#up-arrow"></use></svg></a>
|
||||||
{{else if and (eq .Sort "name") (ne .Order "asc")}}
|
{{- else if and (eq .Sort "name") (ne .Order "asc")}}
|
||||||
<a href="?sort=name&order=asc">Name <svg width="1em" height=".4em" version="1.1" viewBox="0 0 12.922194 6.0358899"><use xlink:href="#down-arrow"></use></svg></a>
|
<a href="?sort=name&order=asc{{if ne 0 .ItemsLimitedTo}}&limit={{.ItemsLimitedTo}}{{end}}">Name <svg width="1em" height=".4em" version="1.1" viewBox="0 0 12.922194 6.0358899"><use xlink:href="#down-arrow"></use></svg></a>
|
||||||
{{else}}
|
{{- else}}
|
||||||
<a href="?sort=name&order=asc">Name</a>
|
<a href="?sort=name&order=asc{{if ne 0 .ItemsLimitedTo}}&limit={{.ItemsLimitedTo}}{{end}}">Name</a>
|
||||||
{{end}}
|
{{- end}}
|
||||||
</th>
|
</th>
|
||||||
<th>
|
<th>
|
||||||
{{if and (eq .Sort "size") (ne .Order "desc")}}
|
{{- if and (eq .Sort "size") (ne .Order "desc")}}
|
||||||
<a href="?sort=size&order=desc">Size <svg width="1em" height=".4em" version="1.1" viewBox="0 0 12.922194 6.0358899"><use xlink:href="#up-arrow"></use></svg></a></a>
|
<a href="?sort=size&order=desc{{if ne 0 .ItemsLimitedTo}}&limit={{.ItemsLimitedTo}}{{end}}">Size <svg width="1em" height=".4em" version="1.1" viewBox="0 0 12.922194 6.0358899"><use xlink:href="#up-arrow"></use></svg></a></a>
|
||||||
{{else if and (eq .Sort "size") (ne .Order "asc")}}
|
{{- else if and (eq .Sort "size") (ne .Order "asc")}}
|
||||||
<a href="?sort=size&order=asc">Size <svg width="1em" height=".4em" version="1.1" viewBox="0 0 12.922194 6.0358899"><use xlink:href="#down-arrow"></use></svg></a></a>
|
<a href="?sort=size&order=asc{{if ne 0 .ItemsLimitedTo}}&limit={{.ItemsLimitedTo}}{{end}}">Size <svg width="1em" height=".4em" version="1.1" viewBox="0 0 12.922194 6.0358899"><use xlink:href="#down-arrow"></use></svg></a></a>
|
||||||
{{else}}
|
{{- else}}
|
||||||
<a href="?sort=size&order=asc">Size</a>
|
<a href="?sort=size&order=asc{{if ne 0 .ItemsLimitedTo}}&limit={{.ItemsLimitedTo}}{{end}}">Size</a>
|
||||||
{{end}}
|
{{- end}}
|
||||||
</th>
|
</th>
|
||||||
<th class="hideable">
|
<th class="hideable">
|
||||||
{{if and (eq .Sort "time") (ne .Order "desc")}}
|
{{- if and (eq .Sort "time") (ne .Order "desc")}}
|
||||||
<a href="?sort=time&order=desc">Modified <svg width="1em" height=".4em" version="1.1" viewBox="0 0 12.922194 6.0358899"><use xlink:href="#up-arrow"></use></svg></a></a>
|
<a href="?sort=time&order=desc{{if ne 0 .ItemsLimitedTo}}&limit={{.ItemsLimitedTo}}{{end}}">Modified <svg width="1em" height=".4em" version="1.1" viewBox="0 0 12.922194 6.0358899"><use xlink:href="#up-arrow"></use></svg></a></a>
|
||||||
{{else if and (eq .Sort "time") (ne .Order "asc")}}
|
{{- else if and (eq .Sort "time") (ne .Order "asc")}}
|
||||||
<a href="?sort=time&order=asc">Modified <svg width="1em" height=".4em" version="1.1" viewBox="0 0 12.922194 6.0358899"><use xlink:href="#down-arrow"></use></svg></a></a>
|
<a href="?sort=time&order=asc{{if ne 0 .ItemsLimitedTo}}&limit={{.ItemsLimitedTo}}{{end}}">Modified <svg width="1em" height=".4em" version="1.1" viewBox="0 0 12.922194 6.0358899"><use xlink:href="#down-arrow"></use></svg></a></a>
|
||||||
{{else}}
|
{{- else}}
|
||||||
<a href="?sort=time&order=asc">Modified</a>
|
<a href="?sort=time&order=asc{{if ne 0 .ItemsLimitedTo}}&limit={{.ItemsLimitedTo}}{{end}}">Modified</a>
|
||||||
{{end}}
|
{{- end}}
|
||||||
</th>
|
</th>
|
||||||
</tr>
|
</tr>
|
||||||
{{if .CanGoUp}}
|
</thead>
|
||||||
|
<tbody>
|
||||||
|
{{- if .CanGoUp}}
|
||||||
<tr>
|
<tr>
|
||||||
<td>
|
<td>
|
||||||
<a href="..">
|
<a href="..">
|
||||||
|
@ -350,30 +365,46 @@ footer {
|
||||||
</a>
|
</a>
|
||||||
</td>
|
</td>
|
||||||
<td>—</td>
|
<td>—</td>
|
||||||
<td>—</td>
|
<td class="hideable">—</td>
|
||||||
</tr>
|
</tr>
|
||||||
{{end}}
|
{{- end}}
|
||||||
{{range .Items}}
|
{{- range .Items}}
|
||||||
<tr>
|
<tr>
|
||||||
<td>
|
<td>
|
||||||
<a href="{{.URL}}">
|
<a href="{{.URL}}">
|
||||||
{{if .IsDir}}
|
{{- if .IsDir}}
|
||||||
<svg width="1.5em" height="1em" version="1.1" viewBox="0 0 35.678803 28.527945"><use xlink:href="#folder"></use></svg>
|
<svg width="1.5em" height="1em" version="1.1" viewBox="0 0 35.678803 28.527945"><use xlink:href="#folder"></use></svg>
|
||||||
{{else}}
|
{{- else}}
|
||||||
<svg width="1.5em" height="1em" version="1.1" viewBox="0 0 26.604381 29.144726"><use xlink:href="#file"></use></svg>
|
<svg width="1.5em" height="1em" version="1.1" viewBox="0 0 26.604381 29.144726"><use xlink:href="#file"></use></svg>
|
||||||
{{end}}
|
{{- end}}
|
||||||
<span class="name">{{.Name}}</span>
|
<span class="name">{{.Name}}</span>
|
||||||
</a>
|
</a>
|
||||||
</td>
|
</td>
|
||||||
<td>{{.HumanSize}}</td>
|
{{- if .IsDir}}
|
||||||
<td class="hideable">{{.HumanModTime "01/02/2006 03:04:05 PM"}}</td>
|
<td data-order="-1">—</td>
|
||||||
|
{{- else}}
|
||||||
|
<td data-order="{{.Size}}">{{.HumanSize}}</td>
|
||||||
|
{{- end}}
|
||||||
|
<td class="hideable"><time datetime="{{.HumanModTime "2006-01-02 15:04:05-0700"}}">{{.HumanModTime "01/02/2006 03:04:05 PM"}}</time></td>
|
||||||
</tr>
|
</tr>
|
||||||
{{end}}
|
{{- end}}
|
||||||
|
</tbody>
|
||||||
</table>
|
</table>
|
||||||
</div>
|
</div>
|
||||||
</main>
|
</main>
|
||||||
<footer>
|
<footer>
|
||||||
Served with <a href="https://caddyserver.com">Caddy</a>
|
Served with <a href="https://caddyserver.com">Caddy</a>
|
||||||
</footer>
|
</footer>
|
||||||
|
<script type="text/javascript">
|
||||||
|
function localizeDatetime(e, index, ar) {
|
||||||
|
if (e.textContent === undefined) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
var d = new Date(e.getAttribute('datetime'));
|
||||||
|
e.textContent = d.toLocaleString();
|
||||||
|
}
|
||||||
|
var timeList = Array.prototype.slice.call(document.getElementsByTagName("time"));
|
||||||
|
timeList.forEach(localizeDatetime);
|
||||||
|
</script>
|
||||||
</body>
|
</body>
|
||||||
</html>`
|
</html>`
|
||||||
|
|
|
@ -5,12 +5,10 @@ package browse
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
"path/filepath"
|
|
||||||
"sort"
|
"sort"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
@ -25,7 +23,6 @@ import (
|
||||||
// directories in the given paths are specified.
|
// directories in the given paths are specified.
|
||||||
type Browse struct {
|
type Browse struct {
|
||||||
Next middleware.Handler
|
Next middleware.Handler
|
||||||
Root string
|
|
||||||
Configs []Config
|
Configs []Config
|
||||||
IgnoreIndexes bool
|
IgnoreIndexes bool
|
||||||
}
|
}
|
||||||
|
@ -33,6 +30,7 @@ type Browse struct {
|
||||||
// Config is a configuration for browsing in a particular path.
|
// Config is a configuration for browsing in a particular path.
|
||||||
type Config struct {
|
type Config struct {
|
||||||
PathScope string
|
PathScope string
|
||||||
|
Root http.FileSystem
|
||||||
Variables interface{}
|
Variables interface{}
|
||||||
Template *template.Template
|
Template *template.Template
|
||||||
}
|
}
|
||||||
|
@ -63,6 +61,9 @@ type Listing struct {
|
||||||
// And which order
|
// And which order
|
||||||
Order string
|
Order string
|
||||||
|
|
||||||
|
// If ≠0 then Items have been limited to that many elements
|
||||||
|
ItemsLimitedTo int
|
||||||
|
|
||||||
// Optional custom variables for use in browse templates
|
// Optional custom variables for use in browse templates
|
||||||
User interface{}
|
User interface{}
|
||||||
|
|
||||||
|
@ -135,7 +136,18 @@ func (l byName) Less(i, j int) bool {
|
||||||
// By Size
|
// By Size
|
||||||
func (l bySize) Len() int { return len(l.Items) }
|
func (l bySize) Len() int { return len(l.Items) }
|
||||||
func (l bySize) Swap(i, j int) { l.Items[i], l.Items[j] = l.Items[j], l.Items[i] }
|
func (l bySize) Swap(i, j int) { l.Items[i], l.Items[j] = l.Items[j], l.Items[i] }
|
||||||
func (l bySize) Less(i, j int) bool { return l.Items[i].Size < l.Items[j].Size }
|
|
||||||
|
const directoryOffset = -1 << 31 // = math.MinInt32
|
||||||
|
func (l bySize) Less(i, j int) bool {
|
||||||
|
iSize, jSize := l.Items[i].Size, l.Items[j].Size
|
||||||
|
if l.Items[i].IsDir {
|
||||||
|
iSize = directoryOffset + iSize
|
||||||
|
}
|
||||||
|
if l.Items[j].IsDir {
|
||||||
|
jSize = directoryOffset + jSize
|
||||||
|
}
|
||||||
|
return iSize < jSize
|
||||||
|
}
|
||||||
|
|
||||||
// By Time
|
// By Time
|
||||||
func (l byTime) Len() int { return len(l.Items) }
|
func (l byTime) Len() int { return len(l.Items) }
|
||||||
|
@ -173,22 +185,20 @@ func (l Listing) applySort() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func directoryListing(files []os.FileInfo, r *http.Request, canGoUp bool, root string, ignoreIndexes bool, vars interface{}) (Listing, error) {
|
func directoryListing(files []os.FileInfo, canGoUp bool, urlPath string) (Listing, bool) {
|
||||||
var (
|
var (
|
||||||
fileinfos []FileInfo
|
fileinfos []FileInfo
|
||||||
dirCount, fileCount int
|
dirCount, fileCount int
|
||||||
urlPath = r.URL.Path
|
hasIndexFile bool
|
||||||
)
|
)
|
||||||
|
|
||||||
for _, f := range files {
|
for _, f := range files {
|
||||||
name := f.Name()
|
name := f.Name()
|
||||||
|
|
||||||
// Directory is not browsable if it contains index file
|
|
||||||
if !ignoreIndexes {
|
|
||||||
for _, indexName := range middleware.IndexPages {
|
for _, indexName := range middleware.IndexPages {
|
||||||
if name == indexName {
|
if name == indexName {
|
||||||
return Listing{}, errors.New("Directory contains index file, not browsable!")
|
hasIndexFile = true
|
||||||
}
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -206,7 +216,7 @@ func directoryListing(files []os.FileInfo, r *http.Request, canGoUp bool, root s
|
||||||
Name: f.Name(),
|
Name: f.Name(),
|
||||||
Size: f.Size(),
|
Size: f.Size(),
|
||||||
URL: url.String(),
|
URL: url.String(),
|
||||||
ModTime: f.ModTime(),
|
ModTime: f.ModTime().UTC(),
|
||||||
Mode: f.Mode(),
|
Mode: f.Mode(),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -218,16 +228,11 @@ func directoryListing(files []os.FileInfo, r *http.Request, canGoUp bool, root s
|
||||||
Items: fileinfos,
|
Items: fileinfos,
|
||||||
NumDirs: dirCount,
|
NumDirs: dirCount,
|
||||||
NumFiles: fileCount,
|
NumFiles: fileCount,
|
||||||
Context: middleware.Context{
|
}, hasIndexFile
|
||||||
Root: http.Dir(root),
|
|
||||||
Req: r,
|
|
||||||
URL: r.URL,
|
|
||||||
},
|
|
||||||
User: vars,
|
|
||||||
}, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ServeHTTP implements the middleware.Handler interface.
|
// ServeHTTP determines if the request is for this plugin, and if all prerequisites are met.
|
||||||
|
// If so, control is handed over to ServeListing.
|
||||||
func (b Browse) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {
|
func (b Browse) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {
|
||||||
var bc *Config
|
var bc *Config
|
||||||
// See if there's a browse configuration to match the path
|
// See if there's a browse configuration to match the path
|
||||||
|
@ -241,8 +246,7 @@ func (b Browse) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {
|
||||||
inScope:
|
inScope:
|
||||||
|
|
||||||
// Browse works on existing directories; delegate everything else
|
// Browse works on existing directories; delegate everything else
|
||||||
requestedFilepath := filepath.Join(b.Root, r.URL.Path)
|
requestedFilepath, err := bc.Root.Open(r.URL.Path)
|
||||||
info, err := os.Stat(requestedFilepath)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
switch {
|
switch {
|
||||||
case os.IsPermission(err):
|
case os.IsPermission(err):
|
||||||
|
@ -253,6 +257,19 @@ inScope:
|
||||||
return b.Next.ServeHTTP(w, r)
|
return b.Next.ServeHTTP(w, r)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
defer requestedFilepath.Close()
|
||||||
|
|
||||||
|
info, err := requestedFilepath.Stat()
|
||||||
|
if err != nil {
|
||||||
|
switch {
|
||||||
|
case os.IsPermission(err):
|
||||||
|
return http.StatusForbidden, err
|
||||||
|
case os.IsExist(err):
|
||||||
|
return http.StatusGone, err
|
||||||
|
default:
|
||||||
|
return b.Next.ServeHTTP(w, r)
|
||||||
|
}
|
||||||
|
}
|
||||||
if !info.IsDir() {
|
if !info.IsDir() {
|
||||||
return b.Next.ServeHTTP(w, r)
|
return b.Next.ServeHTTP(w, r)
|
||||||
}
|
}
|
||||||
|
@ -274,108 +291,117 @@ inScope:
|
||||||
return 0, nil
|
return 0, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Load directory contents
|
return b.ServeListing(w, r, requestedFilepath, bc)
|
||||||
file, err := os.Open(requestedFilepath)
|
}
|
||||||
if err != nil {
|
|
||||||
switch {
|
|
||||||
case os.IsPermission(err):
|
|
||||||
return http.StatusForbidden, err
|
|
||||||
case os.IsExist(err):
|
|
||||||
return http.StatusGone, err
|
|
||||||
default:
|
|
||||||
return http.StatusInternalServerError, err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
defer file.Close()
|
|
||||||
|
|
||||||
files, err := file.Readdir(-1)
|
func (b Browse) loadDirectoryContents(requestedFilepath http.File, urlPath string) (*Listing, bool, error) {
|
||||||
|
files, err := requestedFilepath.Readdir(-1)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
switch {
|
return nil, false, err
|
||||||
case os.IsPermission(err):
|
|
||||||
return http.StatusForbidden, err
|
|
||||||
case os.IsExist(err):
|
|
||||||
return http.StatusGone, err
|
|
||||||
default:
|
|
||||||
return http.StatusInternalServerError, err
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Determine if user can browse up another folder
|
// Determine if user can browse up another folder
|
||||||
var canGoUp bool
|
var canGoUp bool
|
||||||
curPath := strings.TrimSuffix(r.URL.Path, "/")
|
curPathDir := path.Dir(strings.TrimSuffix(urlPath, "/"))
|
||||||
for _, other := range b.Configs {
|
for _, other := range b.Configs {
|
||||||
if strings.HasPrefix(path.Dir(curPath), other.PathScope) {
|
if strings.HasPrefix(curPathDir, other.PathScope) {
|
||||||
canGoUp = true
|
canGoUp = true
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Assemble listing of directory contents
|
|
||||||
listing, err := directoryListing(files, r, canGoUp, b.Root, b.IgnoreIndexes, bc.Variables)
|
|
||||||
if err != nil { // directory isn't browsable
|
|
||||||
return b.Next.ServeHTTP(w, r)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Copy the query values into the Listing struct
|
// Assemble listing of directory contents
|
||||||
listing.Sort, listing.Order = r.URL.Query().Get("sort"), r.URL.Query().Get("order")
|
listing, hasIndex := directoryListing(files, canGoUp, urlPath)
|
||||||
|
|
||||||
|
return &listing, hasIndex, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// handleSortOrder gets and stores for a Listing the 'sort' and 'order',
|
||||||
|
// and reads 'limit' if given. The latter is 0 if not given.
|
||||||
|
//
|
||||||
|
// This sets Cookies.
|
||||||
|
func (b Browse) handleSortOrder(w http.ResponseWriter, r *http.Request, scope string) (sort string, order string, limit int, err error) {
|
||||||
|
sort, order, limitQuery := r.URL.Query().Get("sort"), r.URL.Query().Get("order"), r.URL.Query().Get("limit")
|
||||||
|
|
||||||
// If the query 'sort' or 'order' is empty, use defaults or any values previously saved in Cookies
|
// If the query 'sort' or 'order' is empty, use defaults or any values previously saved in Cookies
|
||||||
if listing.Sort == "" {
|
switch sort {
|
||||||
listing.Sort = "name"
|
case "":
|
||||||
|
sort = "name"
|
||||||
if sortCookie, sortErr := r.Cookie("sort"); sortErr == nil {
|
if sortCookie, sortErr := r.Cookie("sort"); sortErr == nil {
|
||||||
listing.Sort = sortCookie.Value
|
sort = sortCookie.Value
|
||||||
}
|
}
|
||||||
} else { // Save the query value of 'sort' and 'order' as cookies.
|
case "name", "size", "type":
|
||||||
http.SetCookie(w, &http.Cookie{Name: "sort", Value: listing.Sort, Path: bc.PathScope, Secure: r.TLS != nil})
|
http.SetCookie(w, &http.Cookie{Name: "sort", Value: sort, Path: scope, Secure: r.TLS != nil})
|
||||||
http.SetCookie(w, &http.Cookie{Name: "order", Value: listing.Order, Path: bc.PathScope, Secure: r.TLS != nil})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if listing.Order == "" {
|
switch order {
|
||||||
listing.Order = "asc"
|
case "":
|
||||||
|
order = "asc"
|
||||||
if orderCookie, orderErr := r.Cookie("order"); orderErr == nil {
|
if orderCookie, orderErr := r.Cookie("order"); orderErr == nil {
|
||||||
listing.Order = orderCookie.Value
|
order = orderCookie.Value
|
||||||
}
|
}
|
||||||
} else {
|
case "asc", "desc":
|
||||||
http.SetCookie(w, &http.Cookie{Name: "order", Value: listing.Order, Path: bc.PathScope, Secure: r.TLS != nil})
|
http.SetCookie(w, &http.Cookie{Name: "order", Value: order, Path: scope, Secure: r.TLS != nil})
|
||||||
|
}
|
||||||
|
|
||||||
|
if limitQuery != "" {
|
||||||
|
limit, err = strconv.Atoi(limitQuery)
|
||||||
|
if err != nil { // if the 'limit' query can't be interpreted as a number, return err
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// ServeListing returns a formatted view of 'requestedFilepath' contents'.
|
||||||
|
func (b Browse) ServeListing(w http.ResponseWriter, r *http.Request, requestedFilepath http.File, bc *Config) (int, error) {
|
||||||
|
listing, containsIndex, err := b.loadDirectoryContents(requestedFilepath, r.URL.Path)
|
||||||
|
if err != nil {
|
||||||
|
switch {
|
||||||
|
case os.IsPermission(err):
|
||||||
|
return http.StatusForbidden, err
|
||||||
|
case os.IsExist(err):
|
||||||
|
return http.StatusGone, err
|
||||||
|
default:
|
||||||
|
return http.StatusInternalServerError, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if containsIndex && !b.IgnoreIndexes { // directory isn't browsable
|
||||||
|
return b.Next.ServeHTTP(w, r)
|
||||||
|
}
|
||||||
|
listing.Context = middleware.Context{
|
||||||
|
Root: bc.Root,
|
||||||
|
Req: r,
|
||||||
|
URL: r.URL,
|
||||||
|
}
|
||||||
|
listing.User = bc.Variables
|
||||||
|
|
||||||
|
// Copy the query values into the Listing struct
|
||||||
|
var limit int
|
||||||
|
listing.Sort, listing.Order, limit, err = b.handleSortOrder(w, r, bc.PathScope)
|
||||||
|
if err != nil {
|
||||||
|
return http.StatusBadRequest, err
|
||||||
}
|
}
|
||||||
|
|
||||||
listing.applySort()
|
listing.applySort()
|
||||||
|
|
||||||
var buf bytes.Buffer
|
if limit > 0 && limit <= len(listing.Items) {
|
||||||
// Check if we should provide json
|
listing.Items = listing.Items[:limit]
|
||||||
acceptHeader := strings.Join(r.Header["Accept"], ",")
|
listing.ItemsLimitedTo = limit
|
||||||
if strings.Contains(strings.ToLower(acceptHeader), "application/json") {
|
|
||||||
var marsh []byte
|
|
||||||
// Check if we are limited
|
|
||||||
if limitQuery := r.URL.Query().Get("limit"); limitQuery != "" {
|
|
||||||
limit, err := strconv.Atoi(limitQuery)
|
|
||||||
if err != nil { // if the 'limit' query can't be interpreted as a number, return err
|
|
||||||
return http.StatusBadRequest, err
|
|
||||||
}
|
|
||||||
// if `limit` is equal or less than len(listing.Items) and bigger than 0, list them
|
|
||||||
if limit <= len(listing.Items) && limit > 0 {
|
|
||||||
marsh, err = json.Marshal(listing.Items[:limit])
|
|
||||||
} else { // if the 'limit' query is empty, or has the wrong value, list everything
|
|
||||||
marsh, err = json.Marshal(listing.Items)
|
|
||||||
}
|
|
||||||
if err != nil {
|
|
||||||
return http.StatusInternalServerError, err
|
|
||||||
}
|
|
||||||
} else { // There's no 'limit' query; list them all
|
|
||||||
marsh, err = json.Marshal(listing.Items)
|
|
||||||
if err != nil {
|
|
||||||
return http.StatusInternalServerError, err
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Write the marshaled json to buf
|
var buf *bytes.Buffer
|
||||||
if _, err = buf.Write(marsh); err != nil {
|
acceptHeader := strings.ToLower(strings.Join(r.Header["Accept"], ","))
|
||||||
|
switch {
|
||||||
|
case strings.Contains(acceptHeader, "application/json"):
|
||||||
|
if buf, err = b.formatAsJSON(listing, bc); err != nil {
|
||||||
return http.StatusInternalServerError, err
|
return http.StatusInternalServerError, err
|
||||||
}
|
}
|
||||||
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||||||
|
|
||||||
} else { // There's no 'application/json' in the 'Accept' header; browse normally
|
default: // There's no 'application/json' in the 'Accept' header; browse normally
|
||||||
err = bc.Template.Execute(&buf, listing)
|
if buf, err = b.formatAsHTML(listing, bc); err != nil {
|
||||||
if err != nil {
|
|
||||||
return http.StatusInternalServerError, err
|
return http.StatusInternalServerError, err
|
||||||
}
|
}
|
||||||
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||||
|
@ -386,3 +412,20 @@ inScope:
|
||||||
|
|
||||||
return http.StatusOK, nil
|
return http.StatusOK, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (b Browse) formatAsJSON(listing *Listing, bc *Config) (*bytes.Buffer, error) {
|
||||||
|
marsh, err := json.Marshal(listing.Items)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
buf := new(bytes.Buffer)
|
||||||
|
_, err = buf.Write(marsh)
|
||||||
|
return buf, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b Browse) formatAsHTML(listing *Listing, bc *Config) (*bytes.Buffer, error) {
|
||||||
|
buf := new(bytes.Buffer)
|
||||||
|
err := bc.Template.Execute(buf, listing)
|
||||||
|
return buf, err
|
||||||
|
}
|
||||||
|
|
|
@ -114,10 +114,10 @@ func TestBrowseHTTPMethods(t *testing.T) {
|
||||||
Next: middleware.HandlerFunc(func(w http.ResponseWriter, r *http.Request) (int, error) {
|
Next: middleware.HandlerFunc(func(w http.ResponseWriter, r *http.Request) (int, error) {
|
||||||
return http.StatusTeapot, nil // not t.Fatalf, or we will not see what other methods yield
|
return http.StatusTeapot, nil // not t.Fatalf, or we will not see what other methods yield
|
||||||
}),
|
}),
|
||||||
Root: "./testdata",
|
|
||||||
Configs: []Config{
|
Configs: []Config{
|
||||||
{
|
{
|
||||||
PathScope: "/photos",
|
PathScope: "/photos",
|
||||||
|
Root: http.Dir("./testdata"),
|
||||||
Template: tmpl,
|
Template: tmpl,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
@ -153,10 +153,10 @@ func TestBrowseTemplate(t *testing.T) {
|
||||||
t.Fatalf("Next shouldn't be called")
|
t.Fatalf("Next shouldn't be called")
|
||||||
return 0, nil
|
return 0, nil
|
||||||
}),
|
}),
|
||||||
Root: "./testdata",
|
|
||||||
Configs: []Config{
|
Configs: []Config{
|
||||||
{
|
{
|
||||||
PathScope: "/photos",
|
PathScope: "/photos",
|
||||||
|
Root: http.Dir("./testdata"),
|
||||||
Template: tmpl,
|
Template: tmpl,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
@ -208,16 +208,16 @@ func TestBrowseJson(t *testing.T) {
|
||||||
t.Fatalf("Next shouldn't be called")
|
t.Fatalf("Next shouldn't be called")
|
||||||
return 0, nil
|
return 0, nil
|
||||||
}),
|
}),
|
||||||
Root: "./testdata",
|
|
||||||
Configs: []Config{
|
Configs: []Config{
|
||||||
{
|
{
|
||||||
PathScope: "/photos/",
|
PathScope: "/photos/",
|
||||||
|
Root: http.Dir("./testdata"),
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
//Getting the listing from the ./testdata/photos, the listing returned will be used to validate test results
|
//Getting the listing from the ./testdata/photos, the listing returned will be used to validate test results
|
||||||
testDataPath := b.Root + "/photos/"
|
testDataPath := filepath.Join("./testdata", "photos")
|
||||||
file, err := os.Open(testDataPath)
|
file, err := os.Open(testDataPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if os.IsPermission(err) {
|
if os.IsPermission(err) {
|
||||||
|
@ -238,7 +238,7 @@ func TestBrowseJson(t *testing.T) {
|
||||||
// Tests fail in CI environment because all file mod times are the same for
|
// Tests fail in CI environment because all file mod times are the same for
|
||||||
// some reason, making the sorting unpredictable. To hack around this,
|
// some reason, making the sorting unpredictable. To hack around this,
|
||||||
// we ensure here that each file has a different mod time.
|
// we ensure here that each file has a different mod time.
|
||||||
chTime := f.ModTime().Add(-(time.Duration(i) * time.Second))
|
chTime := f.ModTime().UTC().Add(-(time.Duration(i) * time.Second))
|
||||||
if err := os.Chtimes(filepath.Join(testDataPath, name), chTime, chTime); err != nil {
|
if err := os.Chtimes(filepath.Join(testDataPath, name), chTime, chTime); err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
@ -315,7 +315,7 @@ func TestBrowseJson(t *testing.T) {
|
||||||
code, err := b.ServeHTTP(rec, req)
|
code, err := b.ServeHTTP(rec, req)
|
||||||
|
|
||||||
if code != http.StatusOK {
|
if code != http.StatusOK {
|
||||||
t.Fatalf("Wrong status, expected %d, got %d", http.StatusOK, code)
|
t.Fatalf("In test %d: Wrong status, expected %d, got %d", i, http.StatusOK, code)
|
||||||
}
|
}
|
||||||
if rec.HeaderMap.Get("Content-Type") != "application/json; charset=utf-8" {
|
if rec.HeaderMap.Get("Content-Type") != "application/json; charset=utf-8" {
|
||||||
t.Fatalf("Expected Content type to be application/json; charset=utf-8, but got %s ", rec.HeaderMap.Get("Content-Type"))
|
t.Fatalf("Expected Content type to be application/json; charset=utf-8, but got %s ", rec.HeaderMap.Get("Content-Type"))
|
||||||
|
|
Loading…
Reference in a new issue