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

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