restructure page
parent
07b6459e54
commit
8b7c83539a
|
@ -349,6 +349,18 @@ var localizeDatetime = function (e, index, ar) {
|
|||
}
|
||||
|
||||
document.addEventListener("DOMContentLoaded", function (event) {
|
||||
|
||||
document.getElementById("logout").addEventListener("click", event => {
|
||||
let request = new XMLHttpRequest();
|
||||
request.open('GET', window.location.pathname, true, "username", "password");
|
||||
request.send();
|
||||
request.onreadystatechange = function () {
|
||||
if (request.readyState == 4) {
|
||||
window.location = "/";
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
var timeList = Array.prototype.slice.call(document.getElementsByTagName("time"));
|
||||
timeList.forEach(localizeDatetime);
|
||||
|
||||
|
@ -375,7 +387,7 @@ document.addEventListener("DOMContentLoaded", function (event) {
|
|||
|
||||
let rename = document.getElementById("rename");
|
||||
if (rename) {
|
||||
rename.addEventListener("click", renameEvent);
|
||||
rename.addEventListener("click", renameEvent);
|
||||
}
|
||||
|
||||
if (document.getElementById("listing")) {
|
||||
|
|
|
@ -5,19 +5,17 @@
|
|||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
|
||||
<link href='https://fonts.googleapis.com/css?family=Roboto:400,500' rel='stylesheet' type='text/css'>
|
||||
|
||||
<link rel="stylesheet" href="{{ .Config.BaseURL }}/_filemanagerinternal/css/styles.css">
|
||||
|
||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.2.3/ace.js"></script>
|
||||
<script src="{{ .Config.BaseURL }}/_filemanagerinternal/js/application.js"></script>
|
||||
|
||||
{{ if ne .Config.StyleSheet "" }}
|
||||
<style>
|
||||
{{.Config.StyleSheet}}
|
||||
</style>
|
||||
{{ end }}
|
||||
{{ if .Config.HugoEnabled }}
|
||||
<!-- Hugo plugin stuff -->
|
||||
<link rel="stylesheet" href="{{ .Config.BaseURL }}/_hugointernal/css/styles.css">
|
||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.2.3/ace.js"></script>
|
||||
<script src="{{ .Config.BaseURL }}/_hugointernal/js/application.js"></script>
|
||||
{{ end }}
|
||||
</head>
|
||||
<body>
|
||||
<header>
|
||||
|
@ -65,10 +63,11 @@
|
|||
<i class="material-icons">settings</i>
|
||||
</div>
|
||||
</a>
|
||||
<div class="action" id="logout">
|
||||
<i class="material-icons">exit_to_app</i>
|
||||
</div>
|
||||
|
||||
{{ end }}
|
||||
<div class="action" id="logout">
|
||||
<i class="material-icons">exit_to_app</i>
|
||||
</div>
|
||||
</div>
|
||||
</header>
|
||||
|
||||
|
@ -87,6 +86,7 @@
|
|||
</div>
|
||||
</div>
|
||||
{{ end }}
|
||||
|
||||
<main>
|
||||
{{ template "content" .Data }}
|
||||
</main>
|
||||
|
|
59
fileinfo.go
59
fileinfo.go
|
@ -3,7 +3,6 @@ package filemanager
|
|||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"mime"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
|
@ -69,16 +68,12 @@ func GetFileInfo(url *url.URL, c *Config) (*FileInfo, int, error) {
|
|||
|
||||
// GetExtendedFileInfo is used to get extra parameters for FileInfo struct
|
||||
func (fi *FileInfo) GetExtendedFileInfo() error {
|
||||
fi.Mimetype = mime.TypeByExtension(filepath.Ext(fi.Path))
|
||||
fi.Type = SimplifyMimeType(fi.Mimetype)
|
||||
|
||||
if fi.Type == "text" {
|
||||
err := fi.Read()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err := fi.Read()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
fi.Type = SimplifyMimeType(fi.Mimetype)
|
||||
return nil
|
||||
}
|
||||
|
||||
|
@ -88,6 +83,7 @@ func (fi *FileInfo) Read() error {
|
|||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fi.Mimetype = http.DetectContentType(raw)
|
||||
fi.Content = string(raw)
|
||||
return nil
|
||||
}
|
||||
|
@ -104,7 +100,7 @@ func (fi FileInfo) HumanModTime(format string) string {
|
|||
}
|
||||
|
||||
// Delete handles the delete requests
|
||||
func (fi FileInfo) Delete() (int, error) {
|
||||
func (fi *FileInfo) Delete() (int, error) {
|
||||
var err error
|
||||
|
||||
// If it's a directory remove all the contents inside
|
||||
|
@ -122,7 +118,7 @@ func (fi FileInfo) Delete() (int, error) {
|
|||
}
|
||||
|
||||
// Rename function is used tor rename a file or a directory
|
||||
func (fi FileInfo) Rename(w http.ResponseWriter, r *http.Request) (int, error) {
|
||||
func (fi *FileInfo) Rename(w http.ResponseWriter, r *http.Request) (int, error) {
|
||||
newname := r.Header.Get("Rename-To")
|
||||
if newname == "" {
|
||||
return http.StatusBadRequest, nil
|
||||
|
@ -135,11 +131,12 @@ func (fi FileInfo) Rename(w http.ResponseWriter, r *http.Request) (int, error) {
|
|||
return ErrorToHTTPCode(err), err
|
||||
}
|
||||
|
||||
fi.Path = newpath
|
||||
return http.StatusOK, nil
|
||||
}
|
||||
|
||||
// ServeAsHTML is used to serve single file pages
|
||||
func (fi FileInfo) ServeAsHTML(w http.ResponseWriter, r *http.Request, c *Config) (int, error) {
|
||||
func (fi *FileInfo) ServeAsHTML(w http.ResponseWriter, r *http.Request, c *Config) (int, error) {
|
||||
if fi.IsDir {
|
||||
return fi.serveListing(w, r, c)
|
||||
}
|
||||
|
@ -147,12 +144,16 @@ func (fi FileInfo) ServeAsHTML(w http.ResponseWriter, r *http.Request, c *Config
|
|||
return fi.serveSingleFile(w, r, c)
|
||||
}
|
||||
|
||||
func (fi FileInfo) serveSingleFile(w http.ResponseWriter, r *http.Request, c *Config) (int, error) {
|
||||
func (fi *FileInfo) serveSingleFile(w http.ResponseWriter, r *http.Request, c *Config) (int, error) {
|
||||
err := fi.GetExtendedFileInfo()
|
||||
if err != nil {
|
||||
return ErrorToHTTPCode(err), err
|
||||
}
|
||||
|
||||
if fi.Type == "blob" {
|
||||
return fi.ServeRawFile(w, r, c)
|
||||
}
|
||||
|
||||
page := &Page{
|
||||
Info: &PageInfo{
|
||||
Name: fi.Name,
|
||||
|
@ -163,18 +164,10 @@ func (fi FileInfo) serveSingleFile(w http.ResponseWriter, r *http.Request, c *Co
|
|||
},
|
||||
}
|
||||
|
||||
templates := []string{"single", "actions", "base"}
|
||||
for _, t := range templates {
|
||||
code, err := page.AddTemplate(t, Asset, nil)
|
||||
if err != nil {
|
||||
return code, err
|
||||
}
|
||||
}
|
||||
|
||||
return page.PrintAsHTML(w)
|
||||
return page.PrintAsHTML(w, "single")
|
||||
}
|
||||
|
||||
func (fi FileInfo) serveListing(w http.ResponseWriter, r *http.Request, c *Config) (int, error) {
|
||||
func (fi *FileInfo) serveListing(w http.ResponseWriter, r *http.Request, c *Config) (int, error) {
|
||||
var err error
|
||||
|
||||
file, err := c.Root.Open(fi.RootPath)
|
||||
|
@ -226,15 +219,7 @@ func (fi FileInfo) serveListing(w http.ResponseWriter, r *http.Request, c *Confi
|
|||
},
|
||||
}
|
||||
|
||||
templates := []string{"listing", "actions", "base"}
|
||||
for _, t := range templates {
|
||||
code, err := page.AddTemplate(t, Asset, nil)
|
||||
if err != nil {
|
||||
return code, err
|
||||
}
|
||||
}
|
||||
|
||||
return page.PrintAsHTML(w)
|
||||
return page.PrintAsHTML(w, "listing")
|
||||
}
|
||||
|
||||
func (fi FileInfo) loadDirectoryContents(file http.File, c *Config) (*Listing, error) {
|
||||
|
@ -314,5 +299,13 @@ func SimplifyMimeType(name string) string {
|
|||
return "image"
|
||||
}
|
||||
|
||||
return "text"
|
||||
if strings.HasPrefix(name, "text") {
|
||||
return "text"
|
||||
}
|
||||
|
||||
if strings.HasPrefix(name, "application/javascript") {
|
||||
return "text"
|
||||
}
|
||||
|
||||
return "blob"
|
||||
}
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
//go:generate go get github.com/jteeuwen/go-bindata
|
||||
//go:generate go install github.com/jteeuwen/go-bindata/go-bindata
|
||||
//go:generate go-bindata -pkg filemanager -prefix "assets" -o binary.go assets/...
|
||||
//go:generate go-bindata -debug -pkg filemanager -prefix "assets" -o binary.go assets/...
|
||||
|
||||
// Package filemanager provides middleware for managing files in a directory
|
||||
// when directory path is requested instead of a specific file. Based on browse
|
||||
|
|
60
page.go
60
page.go
|
@ -12,7 +12,6 @@ import (
|
|||
// Page contains the informations and functions needed to show the page
|
||||
type Page struct {
|
||||
Info *PageInfo
|
||||
Tpl *template.Template
|
||||
}
|
||||
|
||||
// AssetFunc is an Assets function
|
||||
|
@ -72,37 +71,38 @@ func (p PageInfo) PreviousLink() string {
|
|||
return parts[len(parts)-2]
|
||||
}
|
||||
|
||||
// AddTemplate adds a template file to the page template
|
||||
func (p *Page) AddTemplate(name string, assets AssetFunc, functions template.FuncMap) (int, error) {
|
||||
|
||||
// Get the template from the assets
|
||||
page, err := assets("templates/" + name + ".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 p.Tpl == nil {
|
||||
p.Tpl, err = template.New(name).Funcs(functions).Parse(string(page))
|
||||
} else {
|
||||
p.Tpl, err = p.Tpl.Parse(string(page))
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
log.Print(err)
|
||||
return http.StatusInternalServerError, err
|
||||
}
|
||||
return 0, nil
|
||||
}
|
||||
|
||||
// PrintAsHTML formats the page in HTML and executes the template
|
||||
func (p Page) PrintAsHTML(w http.ResponseWriter) (int, error) {
|
||||
func (p Page) PrintAsHTML(w http.ResponseWriter, templates ...string) (int, error) {
|
||||
templates = append(templates, "actions", "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
|
||||
}
|
||||
}
|
||||
|
||||
buf := &bytes.Buffer{}
|
||||
err := p.Tpl.Execute(buf, p.Info)
|
||||
err := tpl.Execute(buf, p.Info)
|
||||
|
||||
if err != nil {
|
||||
return http.StatusInternalServerError, err
|
||||
|
|
Loading…
Reference in New Issue