support for video and improvements
This commit is contained in:
parent
3430488b58
commit
d96bbff550
|
@ -33,6 +33,10 @@ video {
|
||||||
display: inline-block
|
display: inline-block
|
||||||
}
|
}
|
||||||
|
|
||||||
|
video {
|
||||||
|
max-width: 100%;
|
||||||
|
}
|
||||||
|
|
||||||
audio:not([controls]) {
|
audio:not([controls]) {
|
||||||
display: none;
|
display: none;
|
||||||
height: 0
|
height: 0
|
||||||
|
|
|
@ -6,7 +6,11 @@
|
||||||
{{ else if eq .Type "audio" }}
|
{{ else if eq .Type "audio" }}
|
||||||
<audio src="{{ .URL }}?raw=true"></audio>
|
<audio src="{{ .URL }}?raw=true"></audio>
|
||||||
{{ else if eq .Type "video" }}
|
{{ else if eq .Type "video" }}
|
||||||
<!-- TODO: SHOW VIDEO ? -->
|
<video src="{{ .URL }}?raw=true" controls>
|
||||||
|
Sorry, your browser doesn't support embedded videos,
|
||||||
|
but don't worry, you can <a href="?download=true">download it</a>
|
||||||
|
and watch it with your favorite video player!
|
||||||
|
</video>
|
||||||
{{ else if eq .Type "blob" }}
|
{{ else if eq .Type "blob" }}
|
||||||
<a href="?download=true">Download</a>
|
<a href="?download=true">Download</a>
|
||||||
{{ else}}
|
{{ else}}
|
||||||
|
|
32
file/info.go
32
file/info.go
|
@ -16,7 +16,6 @@ import (
|
||||||
// Info contains the information about a particular file or directory
|
// Info contains the information about a particular file or directory
|
||||||
type Info struct {
|
type Info struct {
|
||||||
os.FileInfo
|
os.FileInfo
|
||||||
File *os.File
|
|
||||||
URL string
|
URL string
|
||||||
Path string // Relative path to Caddyfile
|
Path string // Relative path to Caddyfile
|
||||||
VirtualPath string // Relative path to u.FileSystem
|
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 = strings.Replace(i.Path, "\\", "/", -1)
|
||||||
i.Path = filepath.Clean(i.Path)
|
i.Path = filepath.Clean(i.Path)
|
||||||
|
|
||||||
i.File, err = os.Open(i.Path)
|
i.FileInfo, err = os.Stat(i.Path)
|
||||||
if err != nil {
|
|
||||||
return i, errors.ErrorToHTTPCode(err, false), err
|
|
||||||
}
|
|
||||||
|
|
||||||
i.FileInfo, err = i.File.Stat()
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return i, errors.ErrorToHTTPCode(err, true), err
|
return i, errors.ErrorToHTTPCode(err, true), err
|
||||||
}
|
}
|
||||||
|
|
||||||
p := make([]byte, 512)
|
return i, 0, nil
|
||||||
_, err = i.File.Read(p)
|
}
|
||||||
|
|
||||||
|
// 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 {
|
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.Mimetype = http.DetectContentType(p)
|
||||||
i.Type = simplifyMediaType(i.Mimetype)
|
i.Type = simplifyMediaType(i.Mimetype)
|
||||||
return i, 0, nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Reads the file.
|
||||||
func (i *Info) Read() error {
|
func (i *Info) Read() error {
|
||||||
var err error
|
var err error
|
||||||
i.Content, err = ioutil.ReadFile(i.Path)
|
i.Content, err = ioutil.ReadFile(i.Path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
i.Mimetype = http.DetectContentType(i.Content)
|
|
||||||
i.Type = simplifyMediaType(i.Mimetype)
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -12,11 +12,18 @@ import (
|
||||||
// ServeSingle serves a single file in an editor (if it is editable), shows the
|
// 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.
|
// 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) {
|
func ServeSingle(w http.ResponseWriter, r *http.Request, c *config.Config, u *config.User, i *file.Info) (int, error) {
|
||||||
err := i.Read()
|
var err error
|
||||||
if err != nil {
|
|
||||||
|
if err = i.RetrieveFileType(); err != nil {
|
||||||
return errors.ErrorToHTTPCode(err, true), err
|
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{
|
p := &page.Page{
|
||||||
Info: &page.Info{
|
Info: &page.Info{
|
||||||
Name: i.Name(),
|
Name: i.Name(),
|
||||||
|
|
Loading…
Reference in New Issue