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
import (
"io/ioutil"
"mime"
"net/http"
"net/url"
"os"
@ -13,13 +15,16 @@ import (
// FileInfo is the information about a particular file or directory
type FileInfo struct {
IsDir bool
Name string
Size int64
URL string
ModTime time.Time
Mode os.FileMode
Path string
IsDir bool
Name string
Size int64
URL string
ModTime time.Time
Mode os.FileMode
Path string
Mimetype string
Content string
Type string
}
// 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
func (fi FileInfo) GetExtendedFileInfo() error {
// TODO: do this!
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
}
}
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
}
@ -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)
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 {
//return f.ServeListing(w, r, file.File, c)
return http.StatusNotImplemented, nil
}
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.ServeSingleFile(w, r, file, c)
return http.StatusNotImplemented, nil
return fi.ServeAsHTML(w, r, c)
case http.MethodPost:
// Upload a new file
if r.Header.Get("Upload") == "true" {