Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
caddy
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
caddy
Commits
63b39c78
Commit
63b39c78
authored
Mar 24, 2015
by
Matthew Holt
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Better default template; other fixes
parent
13d9bcc0
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
161 additions
and
26 deletions
+161
-26
middleware/browse/browse.go
middleware/browse/browse.go
+18
-26
middleware/browse/template.go
middleware/browse/template.go
+143
-0
No files found.
middleware/browse/browse.go
View file @
63b39c78
...
...
@@ -13,6 +13,7 @@ import (
"strings"
"time"
"github.com/dustin/go-humanize"
"github.com/mholt/caddy/middleware"
)
...
...
@@ -55,6 +56,14 @@ type FileInfo struct {
Mode
os
.
FileMode
}
func
(
fi
FileInfo
)
HumanSize
()
string
{
return
humanize
.
Bytes
(
uint64
(
fi
.
Size
))
}
func
(
fi
FileInfo
)
HumanModTime
(
format
string
)
string
{
return
fi
.
ModTime
.
Format
(
format
)
}
var
IndexPages
=
[]
string
{
"index.html"
,
"index.htm"
,
...
...
@@ -205,30 +214,17 @@ func parse(c middleware.Controller) ([]BrowseConfig, error) {
for
c
.
Next
()
{
var
bc
BrowseConfig
if
!
c
.
NextArg
()
{
// First argument is directory to allow browsing; default is site root
if
c
.
NextArg
()
{
bc
.
PathScope
=
c
.
Val
()
}
else
{
bc
.
PathScope
=
"/"
err
:=
appendCfg
(
bc
)
if
err
!=
nil
{
return
configs
,
err
}
continue
}
bc
.
PathScope
=
c
.
Val
()
if
!
c
.
NextArg
()
{
err
:=
appendCfg
(
bc
)
if
err
!=
nil
{
return
configs
,
err
}
continue
}
tplFile
:=
c
.
Val
()
// Second argument would be the template file to use
var
tplText
string
if
tplFile
!=
""
{
tplBytes
,
err
:=
ioutil
.
ReadFile
(
tplFile
)
if
c
.
NextArg
()
{
tplBytes
,
err
:=
ioutil
.
ReadFile
(
c
.
Val
())
if
err
!=
nil
{
return
configs
,
err
}
...
...
@@ -237,12 +233,14 @@ func parse(c middleware.Controller) ([]BrowseConfig, error) {
tplText
=
defaultTemplate
}
// Build the template
tpl
,
err
:=
template
.
New
(
"listing"
)
.
Parse
(
tplText
)
if
err
!=
nil
{
return
configs
,
err
}
bc
.
Template
=
tpl
// Save configuration
err
=
appendCfg
(
bc
)
if
err
!=
nil
{
return
configs
,
err
...
...
@@ -251,9 +249,3 @@ func parse(c middleware.Controller) ([]BrowseConfig, error) {
return
configs
,
nil
}
const
defaultTemplate
=
`
{{range .}}
{{.Name}}<br>
{{end}}
`
middleware/browse/template.go
0 → 100644
View file @
63b39c78
package
browse
// The default template to use when serving up directory listings
const
defaultTemplate
=
`<!DOCTYPE html>
<html>
<head>
<title>{{.Name}}</title>
<meta charset="utf-8">
<style>
* { padding: 0; margin: 0; }
body {
padding: 1% 2%;
font: 16px sans-serif;
}
header {
font-size: 45px;
padding: 25px;
}
header a {
text-decoration: none;
color: inherit;
}
header .up {
display: inline-block;
height: 50px;
width: 50px;
text-align: center;
margin-right: 20px;
}
header a.up:hover {
background: #000;
color: #FFF;
}
h1 {
font-size: 30px;
display: inline;
}
table {
border: 0;
border-collapse: collapse;
max-width: 750px;
margin: 0 auto;
}
th,
td {
padding: 4px 20px;
vertical-align: middle;
line-height: 1.5em; /* emoji are kind of odd heights */
}
th {
text-align: left;
}
@media (max-width: 650px) {
.hideable {
display: none;
}
body {
padding: 0;
}
header,
header h1 {
font-size: 14px;
}
header {
position: fixed;
top: 0;
width: 100%;
background: #333;
color: #FFF;
padding: 10px;
text-align: center;
}
header .up {
height: auto;
width: auto;
display: none;
}
header a.up {
display: inline-block;
position: absolute;
left: 0;
top: 0;
width: 35px;
height: 28px;
font-size: 35px;
}
header h1 {
font-weight: normal;
}
main {
margin-top: 50px;
}
}
</style>
</head>
<body>
<header>
{{if .CanGoUp}}
<a href=".." class="up" title="Up one level">⬑</a>
{{else}}
<div class="up"> </div>
{{end}}
<h1>{{.Path}}</h1>
</header>
<main>
<table>
<tr>
<th>Name</th>
<th>Size</th>
<th class="hideable">Modified</th>
</tr>
{{range .Items}}
<tr>
<td>
{{if .IsDir}}📂{{else}}📄{{end}}
<a href="{{.URL}}">{{.Name}}</a>
</td>
<td>{{.HumanSize}}</td>
<td class="hideable">{{.HumanModTime "01/02/2006 3:04:05 PM -0700"}}</td>
</tr>
{{end}}
</table>
</main>
</body>
</html>`
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment