diff --git a/_embed/public/css/styles.css b/_embed/public/css/styles.css
index 644d9cec..0db84589 100644
--- a/_embed/public/css/styles.css
+++ b/_embed/public/css/styles.css
@@ -33,6 +33,10 @@ video {
display: inline-block
}
+video {
+ max-width: 100%;
+}
+
audio:not([controls]) {
display: none;
height: 0
diff --git a/_embed/templates/single.tmpl b/_embed/templates/single.tmpl
index f360663e..e070dcd8 100644
--- a/_embed/templates/single.tmpl
+++ b/_embed/templates/single.tmpl
@@ -6,7 +6,11 @@
{{ else if eq .Type "audio" }}
{{ else if eq .Type "video" }}
-
+
{{ else if eq .Type "blob" }}
Download
{{ else}}
diff --git a/file/info.go b/file/info.go
index 7fc717c3..df0b40ea 100644
--- a/file/info.go
+++ b/file/info.go
@@ -16,7 +16,6 @@ import (
// Info contains the information about a particular file or directory
type Info struct {
os.FileInfo
- File *os.File
URL string
Path string // Relative path to Caddyfile
VirtualPath string // Relative path to u.FileSystem
@@ -40,36 +39,41 @@ func GetInfo(url *url.URL, c *config.Config, u *config.User) (*Info, int, error)
i.Path = strings.Replace(i.Path, "\\", "/", -1)
i.Path = filepath.Clean(i.Path)
- i.File, err = os.Open(i.Path)
- if err != nil {
- return i, errors.ErrorToHTTPCode(err, false), err
- }
-
- i.FileInfo, err = i.File.Stat()
+ i.FileInfo, err = os.Stat(i.Path)
if err != nil {
return i, errors.ErrorToHTTPCode(err, true), err
}
- p := make([]byte, 512)
- _, err = i.File.Read(p)
+ return i, 0, nil
+}
+
+// RetrieveFileType obtains the mimetype and a simplified internal Type
+// using the first 512 bytes from the file.
+func (i *Info) RetrieveFileType() error {
+ file, err := os.Open(i.Path)
if err != nil {
- return i, errors.ErrorToHTTPCode(err, false), err
+ return err
+ }
+ defer file.Close()
+
+ p := make([]byte, 512)
+ _, err = file.Read(p)
+ if err != nil {
+ return err
}
i.Mimetype = http.DetectContentType(p)
i.Type = simplifyMediaType(i.Mimetype)
- return i, 0, nil
+ return nil
}
+// Reads the file.
func (i *Info) Read() error {
var err error
i.Content, err = ioutil.ReadFile(i.Path)
if err != nil {
return err
}
-
- i.Mimetype = http.DetectContentType(i.Content)
- i.Type = simplifyMediaType(i.Mimetype)
return nil
}
diff --git a/handlers/single.go b/handlers/single.go
index 2c22f885..85d0ff5a 100644
--- a/handlers/single.go
+++ b/handlers/single.go
@@ -12,11 +12,18 @@ import (
// ServeSingle serves a single file in an editor (if it is editable), shows the
// plain file, or downloads it if it can't be shown.
func ServeSingle(w http.ResponseWriter, r *http.Request, c *config.Config, u *config.User, i *file.Info) (int, error) {
- err := i.Read()
- if err != nil {
+ var err error
+
+ if err = i.RetrieveFileType(); err != nil {
return errors.ErrorToHTTPCode(err, true), err
}
+ if i.Type == "text" {
+ if err = i.Read(); err != nil {
+ return errors.ErrorToHTTPCode(err, true), err
+ }
+ }
+
p := &page.Page{
Info: &page.Info{
Name: i.Name(),