2017-06-24 11:12:15 +00:00
|
|
|
package filemanager
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
// 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.
|
2017-06-25 12:03:59 +00:00
|
|
|
func serveSingle(w http.ResponseWriter, r *http.Request, c *FileManager, u *user, i *fileInfo) (int, error) {
|
2017-06-24 11:12:15 +00:00
|
|
|
var err error
|
|
|
|
|
|
|
|
if err = i.RetrieveFileType(); err != nil {
|
|
|
|
return errorToHTTP(err, true), err
|
|
|
|
}
|
|
|
|
|
2017-06-25 12:00:33 +00:00
|
|
|
p := &page{
|
2017-06-25 13:24:26 +00:00
|
|
|
Name: i.Name,
|
|
|
|
Path: i.VirtualPath,
|
|
|
|
IsDir: false,
|
|
|
|
Data: i,
|
|
|
|
User: u,
|
|
|
|
PrefixURL: c.PrefixURL,
|
|
|
|
BaseURL: c.AbsoluteURL(),
|
|
|
|
WebDavURL: c.AbsoluteWebDavURL(),
|
2017-06-24 11:12:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// If the request accepts JSON, we send the file information.
|
|
|
|
if strings.Contains(r.Header.Get("Accept"), "application/json") {
|
|
|
|
return p.PrintAsJSON(w)
|
|
|
|
}
|
|
|
|
|
|
|
|
if i.Type == "text" {
|
|
|
|
if err = i.Read(); err != nil {
|
|
|
|
return errorToHTTP(err, true), err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if i.CanBeEdited() && u.AllowEdit {
|
2017-06-25 13:48:34 +00:00
|
|
|
p.Data, err = getEditor(r, i)
|
2017-06-24 11:12:15 +00:00
|
|
|
p.Editor = true
|
|
|
|
if err != nil {
|
|
|
|
return http.StatusInternalServerError, err
|
|
|
|
}
|
|
|
|
|
2017-06-25 13:24:26 +00:00
|
|
|
return p.PrintAsHTML(w, c.Assets.Templates, "frontmatter", "editor")
|
2017-06-24 11:12:15 +00:00
|
|
|
}
|
|
|
|
|
2017-06-25 13:24:26 +00:00
|
|
|
return p.PrintAsHTML(w, c.Assets.Templates, "single")
|
2017-06-24 11:12:15 +00:00
|
|
|
}
|