This commit is contained in:
Henrique Dias 2016-06-11 10:37:36 +01:00
parent 4163aeae40
commit 36da3d0d7d
5 changed files with 88 additions and 85 deletions

View File

@ -17,13 +17,13 @@ h1 a:hover {
header,
#summary {
padding-left: 5%;
padding-right: 5%;
padding-left: 7%;
padding-right: 7%;
}
th:first-child,
td:first-child {
padding-left: 5%;
padding-left: 7%;
}
th:last-child,
@ -32,9 +32,9 @@ td:last-child {
}
header {
padding-top: 25px;
padding-bottom: 15px;
background-color: #f2f2f2;
background-color: #FFC107;
padding-top: 1.5em;
padding-bottom: 1.5em;
}
h1 {
@ -60,9 +60,11 @@ main {
.meta {
font-size: 12px;
font-family: Verdana, sans-serif;
border-bottom: 1px solid #9C9C9C;
/* border-bottom: 1px solid #9C9C9C; */
padding-top: 15px;
padding-bottom: 15px;
background-color: #FFA000;
color: #fff;
}
.meta-item {

View File

@ -3,6 +3,7 @@
<head>
<title>{{.Name}}</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="https://use.fontawesome.com/ede56571a0.js"></script>
<link rel="stylesheet" href="/_filemanagerinternal/css/styles.css">
<style>

View File

@ -58,9 +58,9 @@
<td>
<a href="{{.URL}}">
{{- if .IsDir}}
<svg width="1.5em" height="1em" version="1.1" viewBox="0 0 35.678803 28.527945"><use xlink:href="#folder"></use></svg>
<i class="fa fa-folder" aria-hidden="true"></i>
{{- else}}
<svg width="1.5em" height="1em" version="1.1" viewBox="0 0 26.604381 29.144726"><use xlink:href="#file"></use></svg>
<i class="fa fa-file-text-o" aria-hidden="true"></i>
{{- end}}
<span class="name">{{.Name}}</span>
</a>

View File

@ -1,8 +1,6 @@
package filemanager
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"net/url"
@ -67,8 +65,8 @@ func (f FileManager) loadDirectoryContents(requestedFilepath http.File, urlPath
}
// Assemble listing of directory contents
listing, hasIndex := directoryListing(files, canGoUp, urlPath)
return &listing, hasIndex, nil
listing, _ := directoryListing(files, canGoUp, urlPath)
return &listing, false, nil
}
// ServeListing returns a formatted view of 'requestedFilepath' contents'.
@ -110,41 +108,21 @@ func (f FileManager) ServeListing(w http.ResponseWriter, r *http.Request, reques
listing.ItemsLimitedTo = limit
}
var buf *bytes.Buffer
acceptHeader := strings.ToLower(strings.Join(r.Header["Accept"], ","))
page := &Page{
Info: &PageInfo{
Name: listing.Name,
Path: listing.Path,
Data: listing,
},
}
if strings.Contains(acceptHeader, "application/json") {
if buf, err = f.formatAsJSON(listing, bc); err != nil {
return http.StatusInternalServerError, err
}
w.Header().Set("Content-Type", "application/json; charset=utf-8")
} else {
page := &Page{
Name: listing.Name,
Path: listing.Path,
Config: bc,
Data: listing,
}
if buf, err = f.formatAsHTML(page, "listing"); err != nil {
return http.StatusInternalServerError, err
}
w.Header().Set("Content-Type", "text/html; charset=utf-8")
return page.PrintAsJSON(w)
}
buf.WriteTo(w)
return http.StatusOK, nil
}
func (f FileManager) 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
return page.PrintAsHTML(w, "listing")
}
func directoryListing(files []os.FileInfo, canGoUp bool, urlPath string) (Listing, bool) {

106
page.go
View File

@ -1,57 +1,23 @@
package filemanager
import (
"bytes"
"encoding/json"
"html/template"
"log"
"net/http"
"strings"
)
type Page struct {
Name string
Path string
Config *Config
Data interface{}
}
func (f FileManager) formatAsHTML(page *Page, templates ...string) (*bytes.Buffer, error) {
buf := new(bytes.Buffer)
templates = append(templates, "base")
var tpl *template.Template
// For each template, add it to the the tpl variable
for i, t := range templates {
// Get the template from the assets
page, err := Asset("templates/" + t + ".tmpl")
// Check if there is some error. If so, the template doesn't exist
if err != nil {
log.Print(err)
return new(bytes.Buffer), err
}
// If it's the first iteration, creates a new template and add the
// functions map
if i == 0 {
tpl, err = template.New(t).Parse(string(page))
} else {
tpl, err = tpl.Parse(string(page))
}
if err != nil {
log.Print(err)
return new(bytes.Buffer), err
}
}
err := tpl.Execute(buf, page)
return buf, err
// PageInfo contains the information of a page
type PageInfo struct {
Name string
Path string
Data interface{}
}
// BreadcrumbMap returns p.Path where every element is a map
// of URLs and path segment names.
func (p Page) BreadcrumbMap() map[string]string {
func (p PageInfo) BreadcrumbMap() map[string]string {
result := map[string]string{}
if len(p.Path) == 0 {
@ -76,3 +42,59 @@ func (p Page) BreadcrumbMap() map[string]string {
return result
}
// Page contains the informations and functions needed to show the page
type Page struct {
Info *PageInfo
}
// PrintAsHTML formats the page in HTML and executes the template
func (p Page) PrintAsHTML(w http.ResponseWriter, templates ...string) (int, error) {
templates = append(templates, "base")
var tpl *template.Template
// For each template, add it to the the tpl variable
for i, t := range templates {
// Get the template from the assets
page, err := Asset("templates/" + t + ".tmpl")
// Check if there is some error. If so, the template doesn't exist
if err != nil {
log.Print(err)
return http.StatusInternalServerError, err
}
// If it's the first iteration, creates a new template and add the
// functions map
if i == 0 {
tpl, err = template.New(t).Parse(string(page))
} else {
tpl, err = tpl.Parse(string(page))
}
if err != nil {
log.Print(err)
return http.StatusInternalServerError, err
}
}
w.Header().Set("Content-Type", "text/html; charset=utf-8")
err := tpl.Execute(w, p.Info)
if err != nil {
return http.StatusInternalServerError, err
}
return http.StatusOK, nil
}
// PrintAsJSON prints the current page infromation in JSON
func (p Page) PrintAsJSON(w http.ResponseWriter) (int, error) {
marsh, err := json.Marshal(p.Info.Data)
if err != nil {
return http.StatusInternalServerError, err
}
w.Header().Set("Content-Type", "application/json; charset=utf-8")
return w.Write(marsh)
}