Added the Context to the browse directive

Moved the Context type to middleware and exported it.
Users can use .Include and others in browse directive templating
Created test for the templates directive.
This commit is contained in:
Maxime
2015-07-17 20:07:24 +02:00
parent fcf2622c26
commit f536bc94b2
10 changed files with 165 additions and 32 deletions

View File

@ -5,13 +5,13 @@ package browse
import (
"bytes"
"errors"
"html/template"
"net/http"
"net/url"
"os"
"path"
"sort"
"strings"
"text/template"
"time"
"github.com/dustin/go-humanize"
@ -51,6 +51,8 @@ type Listing struct {
// And which order
Order string
middleware.Context
}
// FileInfo is the info about a particular file or directory
@ -135,8 +137,9 @@ var IndexPages = []string{
"default.htm",
}
func directoryListing(files []os.FileInfo, urlPath string, canGoUp bool) (Listing, error) {
func directoryListing(files []os.FileInfo, r *http.Request, canGoUp bool, root string) (Listing, error) {
var fileinfos []FileInfo
var urlPath = r.URL.Path
for _, f := range files {
name := f.Name()
@ -168,6 +171,11 @@ func directoryListing(files []os.FileInfo, urlPath string, canGoUp bool) (Listin
Path: urlPath,
CanGoUp: canGoUp,
Items: fileinfos,
Context: middleware.Context{
Root: http.Dir(root),
Req: r,
URL: r.URL,
},
}, nil
}
@ -222,7 +230,7 @@ func (b Browse) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {
}
}
// Assemble listing of directory contents
listing, err := directoryListing(files, r.URL.Path, canGoUp)
listing, err := directoryListing(files, r, canGoUp, b.Root)
if err != nil { // directory isn't browsable
continue
}