update add single file view

pull/144/head
Henrique Dias 2016-06-11 22:34:00 +01:00
parent da02deb3a4
commit 8c24409f33
3 changed files with 70 additions and 79 deletions

View File

@ -1,67 +0,0 @@
package filemanager
import (
"encoding/base64"
"html"
"io/ioutil"
"mime"
"net/http"
"path/filepath"
"regexp"
)
var (
videoRegex = regexp.MustCompile("video[/]")
audioRegex = regexp.MustCompile("audio[/]")
imageRegex = regexp.MustCompile("image[/]")
)
type File struct {
*FileInfo
Content string
}
// ServeSingleFile redirects the request for the respective method
func (f FileManager) ServeSingleFile(w http.ResponseWriter, r *http.Request, file *InfoRequest, c *Config) (int, error) {
fullpath := c.PathScope + file.Path
fullpath = filepath.Clean(fullpath)
raw, err := ioutil.ReadFile(fullpath)
if err != nil {
return http.StatusInternalServerError, err
}
base := base64.StdEncoding.EncodeToString(raw)
mimetype := mime.TypeByExtension(filepath.Ext(file.Path))
data := "data:" + mimetype + ";base64," + base
page := &Page{
Info: &PageInfo{
Name: file.Path,
Path: file.Path,
Data: map[string]string{
"Type": RetrieveContentType(mimetype),
"Base64": data,
"Content": html.EscapeString(string(raw)),
},
},
}
return page.PrintAsHTML(w, "single")
}
func RetrieveContentType(name string) string {
if videoRegex.FindString(name) != "" {
return "video"
}
if audioRegex.FindString(name) != "" {
return "audio"
}
if imageRegex.FindString(name) != "" {
return "image"
}
return "text"
}

View File

@ -1,6 +1,8 @@
package filemanager package filemanager
import ( import (
"io/ioutil"
"mime"
"net/http" "net/http"
"net/url" "net/url"
"os" "os"
@ -20,6 +22,9 @@ type FileInfo struct {
ModTime time.Time ModTime time.Time
Mode os.FileMode Mode os.FileMode
Path string Path string
Mimetype string
Content string
Type string
} }
// GetFileInfo gets the file information and, in case of error, returns the // GetFileInfo gets the file information and, in case of error, returns the
@ -53,8 +58,27 @@ func GetFileInfo(url *url.URL, c *Config) (*FileInfo, int, error) {
} }
// GetExtendedFileInfo is used to get extra parameters for FileInfo struct // GetExtendedFileInfo is used to get extra parameters for FileInfo struct
func (fi FileInfo) GetExtendedFileInfo() error { func (fi *FileInfo) GetExtendedFileInfo() error {
// TODO: do this! 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
}
}
return nil
}
// Read is used to read a file and store its content
func (fi *FileInfo) Read() error {
raw, err := ioutil.ReadFile(fi.Path)
if err != nil {
return err
}
fi.Content = string(raw)
return nil return nil
} }
@ -104,3 +128,38 @@ func (fi FileInfo) Rename(w http.ResponseWriter, r *http.Request) (int, error) {
http.Redirect(w, r, strings.Replace(fi.URL, fi.Name, newname, 1), http.StatusTemporaryRedirect) http.Redirect(w, r, strings.Replace(fi.URL, fi.Name, newname, 1), http.StatusTemporaryRedirect)
return 0, nil return 0, nil
} }
// ServeAsHTML is used to serve single file pages
func (fi FileInfo) ServeAsHTML(w http.ResponseWriter, r *http.Request, c *Config) (int, error) {
err := fi.GetExtendedFileInfo()
if err != nil {
return ErrorToHTTPCode(err), err
}
page := &Page{
Info: &PageInfo{
Name: fi.Path,
Path: fi.Path,
Data: fi,
},
}
return page.PrintAsHTML(w, "single")
}
// SimplifyMimeType returns the base type of a file
func SimplifyMimeType(name string) string {
if strings.HasPrefix(name, "video") {
return "video"
}
if strings.HasPrefix(name, "audio") {
return "audio"
}
if strings.HasPrefix(name, "image") {
return "image"
}
return "text"
}

View File

@ -66,6 +66,7 @@ func (f FileManager) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, err
if fi.IsDir { if fi.IsDir {
//return f.ServeListing(w, r, file.File, c) //return f.ServeListing(w, r, file.File, c)
return http.StatusNotImplemented, nil
} }
query := r.URL.Query() query := r.URL.Query()
@ -78,9 +79,7 @@ func (f FileManager) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, err
return f.Next.ServeHTTP(w, r) return f.Next.ServeHTTP(w, r)
} }
// return f.ServeSingleFile(w, r, file, c) return fi.ServeAsHTML(w, r, c)
return http.StatusNotImplemented, nil
case http.MethodPost: case http.MethodPost:
// Upload a new file // Upload a new file
if r.Header.Get("Upload") == "true" { if r.Header.Get("Upload") == "true" {