fix previous link; add more stuff

This commit is contained in:
Henrique Dias 2016-06-26 16:52:15 +01:00
parent ae6700709d
commit fff716b2b3
7 changed files with 150 additions and 139 deletions

View File

@ -328,7 +328,7 @@ header p i {
header #logout {
background-color: rgba(0,0,0,0.1);
border-radius: 0;
margin: -0.5em;
margin: -0.5em -0.5em -0.5em 0;
padding: .5em;
}
header p i {

View File

@ -18,59 +18,61 @@
{{ end }}
</head>
<body>
<header>
<div>
{{ $lnk := .PreviousLink }}
{{ if ne $lnk ""}}
<a href="{{ if eq $lnk " /" }}/{{else }}../../{{.PreviousLink}}{{ end }}">
<div class="action" id="prev">
<i class="material-icons">subdirectory_arrow_left</i>
</div>
</a>{{ else }}
<div class="action disabled" id="prev">
<i class="material-icons">subdirectory_arrow_left</i>
</div>
{{ end }}
<p>
<a href="{{ if eq .Config.BaseURL " " }}/{{ else }}{{ .Config.BaseURL }}{{ end }}">
{{ if .Config.HugoEnabled }}Hugo{{ else }}File Manager{{ end }}
</a>
{{ if ne .Name "/"}}
<i class="material-icons">chevron_right</i>
{{ .Name }}</p>
{{ end }}
</div>
<div>
<header>
<div>
{{ $lnk := .PreviousLink }}
{{ if ne $lnk ""}}
<a href="{{ $lnk }}">
<div class="action" id="prev">
<i class="material-icons">subdirectory_arrow_left</i>
</div>
</a>
{{ else }}
<div class="action disabled" id="prev">
<i class="material-icons">subdirectory_arrow_left</i>
</div>
{{ end }}
<p>
<a href="{{ if eq .Config.BaseURL " " }}/{{ else }}{{ .Config.BaseURL }}{{ end }}">
{{ if .Config.HugoEnabled }}Hugo{{ else }}File Manager{{ end }}
</a>
{{ if ne .Name "/"}}
<i class="material-icons">chevron_right</i>
{{ .Name }}</p>
{{ end }}
</div>
<div>
{{ if .IsDir}}
<!-- <form>
<i class="material-icons">search</i>
<input type="text" placeholder="Search">
</form> -->
<div class="action" id="view">
<i class="material-icons">view_headline</i>
</div>
<div class="action" id="upload">
<i class="material-icons">file_upload</i>
</div>
{{ else }}
{{ template "actions" . }}
<form>
<i class="material-icons">search</i>
<input type="text" placeholder="Search">
</form>
<div class="action" id="view">
<i class="material-icons">view_headline</i>
</div>
<div class="action" id="upload">
<i class="material-icons">file_upload</i>
</div>
{{ else }}
{{ template "actions" . }}
{{ end }}
{{ if .Config.HugoEnabled }}
<!-- Hugo plugin stuff -->
<a href="{{ .Config.BaseURL }}/settings">
<div class="action">
<i class="material-icons">settings</i>
</div>
</a>
<!-- Hugo plugin stuff -->
<a href="{{ .Config.BaseURL }}/settings">
<div class="action">
<i class="material-icons">settings</i>
</div>
</a>
{{ end }}
<div class="action" id="logout">
<i class="material-icons">exit_to_app</i>
<i class="material-icons">exit_to_app</i>
</div>
</div>
</header>
</div>
</header>
{{ if .IsDir }}
<div id="toolbar">
@ -89,6 +91,7 @@
{{ end }}
<main>
{{ .Path }}
{{ template "content" .Data }}
</main>
<footer>

View File

@ -8,12 +8,7 @@
package filemanager
import (
"io"
"log"
"mime/multipart"
"net/http"
"os"
"path/filepath"
"strings"
a "github.com/hacdias/caddy-filemanager/internal/assets"
@ -80,9 +75,15 @@ func (f FileManager) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, err
case http.MethodPost:
// Upload a new file
if r.Header.Get("Upload") == "true" {
return Upload(w, r, c)
return file.Upload(w, r, c)
}
return NewFolder(w, r, c)
// Search and git commands
if r.Header.Get("Search") == "true" {
// TODO: search and git commands
}
// Creates a new folder
// TODO: not implemented on frontend
return file.NewDir(w, r, c)
case http.MethodDelete:
// Delete a file or a directory
return fi.Delete()
@ -97,79 +98,3 @@ func (f FileManager) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, err
return f.Next.ServeHTTP(w, r)
}
// ErrorToHTTPCode gets the respective HTTP code for an error
func ErrorToHTTPCode(err error) int {
switch {
case os.IsPermission(err):
return http.StatusForbidden
case os.IsNotExist(err):
return http.StatusNotFound
case os.IsExist(err):
return http.StatusGone
default:
return http.StatusInternalServerError
}
}
// Upload is used to handle the upload requests to the server
func Upload(w http.ResponseWriter, r *http.Request, c *config.Config) (int, error) {
// Parse the multipart form in the request
err := r.ParseMultipartForm(100000)
if err != nil {
log.Println(err)
return http.StatusInternalServerError, err
}
// For each file header in the multipart form
for _, headers := range r.MultipartForm.File {
// Handle each file
for _, header := range headers {
// Open the first file
var src multipart.File
if src, err = header.Open(); nil != err {
return http.StatusInternalServerError, err
}
filename := strings.Replace(r.URL.Path, c.BaseURL, c.PathScope, 1)
filename = filename + header.Filename
filename = filepath.Clean(filename)
// Create the file
var dst *os.File
if dst, err = os.Create(filename); nil != err {
if os.IsExist(err) {
return http.StatusConflict, err
}
return http.StatusInternalServerError, err
}
// Copy the file content
if _, err = io.Copy(dst, src); nil != err {
return http.StatusInternalServerError, err
}
defer dst.Close()
}
}
return http.StatusOK, nil
}
// NewFolder makes a new directory
func NewFolder(w http.ResponseWriter, r *http.Request, c *config.Config) (int, error) {
path := strings.Replace(r.URL.Path, c.BaseURL, c.PathScope, 1)
path = filepath.Clean(path)
err := os.MkdirAll(path, 0755)
if err != nil {
switch {
case os.IsPermission(err):
return http.StatusForbidden, err
case os.IsExist(err):
return http.StatusConflict, err
default:
return http.StatusInternalServerError, err
}
}
return http.StatusCreated, nil
}

28
internal/file/dir.go Normal file
View File

@ -0,0 +1,28 @@
package file
import (
"net/http"
"os"
"path/filepath"
"strings"
"github.com/hacdias/caddy-filemanager/internal/config"
)
// NewDir makes a new directory
func NewDir(w http.ResponseWriter, r *http.Request, c *config.Config) (int, error) {
path := strings.Replace(r.URL.Path, c.BaseURL, c.PathScope, 1)
path = filepath.Clean(path)
err := os.MkdirAll(path, 0755)
if err != nil {
switch {
case os.IsPermission(err):
return http.StatusForbidden, err
case os.IsExist(err):
return http.StatusConflict, err
default:
return http.StatusInternalServerError, err
}
}
return http.StatusCreated, nil
}

57
internal/file/upload.go Normal file
View File

@ -0,0 +1,57 @@
package file
import (
"io"
"log"
"mime/multipart"
"net/http"
"os"
"path/filepath"
"strings"
"github.com/hacdias/caddy-filemanager/internal/config"
)
// Upload is used to handle the upload requests to the server
func Upload(w http.ResponseWriter, r *http.Request, c *config.Config) (int, error) {
// Parse the multipart form in the request
err := r.ParseMultipartForm(100000)
if err != nil {
log.Println(err)
return http.StatusInternalServerError, err
}
// For each file header in the multipart form
for _, headers := range r.MultipartForm.File {
// Handle each file
for _, header := range headers {
// Open the first file
var src multipart.File
if src, err = header.Open(); nil != err {
return http.StatusInternalServerError, err
}
filename := strings.Replace(r.URL.Path, c.BaseURL, c.PathScope, 1)
filename = filename + header.Filename
filename = filepath.Clean(filename)
// Create the file
var dst *os.File
if dst, err = os.Create(filename); nil != err {
if os.IsExist(err) {
return http.StatusConflict, err
}
return http.StatusInternalServerError, err
}
// Copy the file content
if _, err = io.Copy(dst, src); nil != err {
return http.StatusInternalServerError, err
}
defer dst.Close()
}
}
return http.StatusOK, nil
}

View File

@ -57,19 +57,16 @@ func (i Info) BreadcrumbMap() map[string]string {
// PreviousLink returns the path of the previous folder
func (i Info) PreviousLink() string {
parts := strings.Split(strings.TrimSuffix(i.Path, "/"), "/")
if len(parts) <= 1 {
path := strings.TrimSuffix(i.Path, "/")
path = strings.TrimPrefix(path, "/")
path = i.Config.BaseURL + "/" + path
path = path[0 : len(path)-len(i.Name)]
if len(path) < len(i.Config.BaseURL+"/") {
return ""
}
if parts[len(parts)-2] == "" {
if i.Config.BaseURL == "" {
return "/"
}
return i.Config.BaseURL
}
return parts[len(parts)-2]
return path
}
// PrintAsHTML formats the page in HTML and executes the template

View File

@ -0,0 +1 @@
package search