From 8c24409f3327d25bec2cbfc2e38f79a5aba48d1d Mon Sep 17 00:00:00 2001 From: Henrique Dias Date: Sat, 11 Jun 2016 22:34:00 +0100 Subject: [PATCH] update add single file view --- OLD/single.go | 67 ------------------------------------------- fileinfo.go | 77 ++++++++++++++++++++++++++++++++++++++++++++------ filemanager.go | 5 ++-- 3 files changed, 70 insertions(+), 79 deletions(-) delete mode 100644 OLD/single.go diff --git a/OLD/single.go b/OLD/single.go deleted file mode 100644 index c1e59676..00000000 --- a/OLD/single.go +++ /dev/null @@ -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" -} diff --git a/fileinfo.go b/fileinfo.go index a144c96c..c166a9e2 100644 --- a/fileinfo.go +++ b/fileinfo.go @@ -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" +} diff --git a/filemanager.go b/filemanager.go index dba56985..cb747ff7 100644 --- a/filemanager.go +++ b/filemanager.go @@ -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" {