filebrowser/OLD/single.go

68 lines
1.3 KiB
Go
Raw Normal View History

2016-06-11 09:08:33 +00:00
package filemanager
2016-06-11 13:32:50 +00:00
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
}
2016-06-11 09:08:33 +00:00
// ServeSingleFile redirects the request for the respective method
2016-06-11 13:32:50 +00:00
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"
2016-06-11 09:08:33 +00:00
}