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

View File

@ -3,6 +3,7 @@
<head> <head>
<title>{{.Name}}</title> <title>{{.Name}}</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0"> <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"> <link rel="stylesheet" href="/_filemanagerinternal/css/styles.css">
<style> <style>

View File

@ -58,9 +58,9 @@
<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> <i class="fa fa-folder" aria-hidden="true"></i>
{{- else}} {{- 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}} {{- end}}
<span class="name">{{.Name}}</span> <span class="name">{{.Name}}</span>
</a> </a>

View File

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

100
page.go
View File

@ -1,57 +1,23 @@
package filemanager package filemanager
import ( import (
"bytes" "encoding/json"
"html/template" "html/template"
"log" "log"
"net/http"
"strings" "strings"
) )
type Page struct { // PageInfo contains the information of a page
type PageInfo struct {
Name string Name string
Path string Path string
Config *Config
Data interface{} 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
}
// BreadcrumbMap returns p.Path where every element is a map // BreadcrumbMap returns p.Path where every element is a map
// of URLs and path segment names. // of URLs and path segment names.
func (p Page) BreadcrumbMap() map[string]string { func (p PageInfo) BreadcrumbMap() map[string]string {
result := map[string]string{} result := map[string]string{}
if len(p.Path) == 0 { if len(p.Path) == 0 {
@ -76,3 +42,59 @@ func (p Page) BreadcrumbMap() map[string]string {
return result 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)
}