2016-10-22 10:47:49 +00:00
|
|
|
package handlers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/hacdias/caddy-filemanager/config"
|
|
|
|
"github.com/hacdias/caddy-filemanager/file"
|
|
|
|
"github.com/hacdias/caddy-filemanager/page"
|
2016-10-22 11:07:19 +00:00
|
|
|
"github.com/hacdias/caddy-filemanager/utils/errors"
|
2016-10-22 10:47:49 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// 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) {
|
2016-10-22 12:46:10 +00:00
|
|
|
var err error
|
|
|
|
|
|
|
|
if err = i.RetrieveFileType(); err != nil {
|
2016-10-22 11:07:19 +00:00
|
|
|
return errors.ErrorToHTTPCode(err, true), err
|
2016-10-22 10:47:49 +00:00
|
|
|
}
|
|
|
|
|
2016-10-22 12:46:10 +00:00
|
|
|
if i.Type == "text" {
|
|
|
|
if err = i.Read(); err != nil {
|
|
|
|
return errors.ErrorToHTTPCode(err, true), err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-22 10:47:49 +00:00
|
|
|
p := &page.Page{
|
|
|
|
Info: &page.Info{
|
|
|
|
Name: i.Name(),
|
|
|
|
Path: i.VirtualPath,
|
|
|
|
IsDir: false,
|
|
|
|
Data: i,
|
|
|
|
User: u,
|
|
|
|
Config: c,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
if i.CanBeEdited() && u.AllowEdit {
|
2016-10-22 11:07:19 +00:00
|
|
|
p.Data, err = GetEditor(i)
|
2016-10-22 10:47:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return http.StatusInternalServerError, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return p.PrintAsHTML(w, "frontmatter", "editor")
|
|
|
|
}
|
|
|
|
|
|
|
|
return p.PrintAsHTML(w, "single")
|
|
|
|
}
|