2017-08-18 08:00:32 +00:00
|
|
|
package http
|
2017-07-08 16:51:47 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2017-08-07 13:44:32 +00:00
|
|
|
"io/ioutil"
|
2017-08-10 14:04:07 +00:00
|
|
|
"log"
|
2017-07-08 16:51:47 +00:00
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
2017-08-10 14:04:07 +00:00
|
|
|
"time"
|
2017-07-08 16:51:47 +00:00
|
|
|
|
2017-08-18 08:00:32 +00:00
|
|
|
fm "github.com/hacdias/filemanager"
|
2017-07-27 20:39:23 +00:00
|
|
|
"github.com/hacdias/fileutils"
|
2017-07-26 10:14:05 +00:00
|
|
|
)
|
2017-07-26 08:22:15 +00:00
|
|
|
|
2017-07-26 14:55:39 +00:00
|
|
|
// sanitizeURL sanitizes the URL to prevent path transversal
|
2017-07-27 20:39:23 +00:00
|
|
|
// using fileutils.SlashClean and adds the trailing slash bar.
|
2017-07-26 14:55:39 +00:00
|
|
|
func sanitizeURL(url string) string {
|
2017-07-27 20:39:23 +00:00
|
|
|
path := fileutils.SlashClean(url)
|
2017-07-26 14:55:39 +00:00
|
|
|
if strings.HasSuffix(url, "/") && path != "/" {
|
|
|
|
return path + "/"
|
2017-07-26 10:14:05 +00:00
|
|
|
}
|
2017-07-26 14:55:39 +00:00
|
|
|
return path
|
|
|
|
}
|
|
|
|
|
2017-08-18 08:00:32 +00:00
|
|
|
func resourceHandler(c *fm.Context, w http.ResponseWriter, r *http.Request) (int, error) {
|
2017-07-26 14:55:39 +00:00
|
|
|
r.URL.Path = sanitizeURL(r.URL.Path)
|
2017-07-26 10:14:05 +00:00
|
|
|
|
2017-07-08 16:51:47 +00:00
|
|
|
switch r.Method {
|
|
|
|
case http.MethodGet:
|
|
|
|
return resourceGetHandler(c, w, r)
|
|
|
|
case http.MethodDelete:
|
|
|
|
return resourceDeleteHandler(c, w, r)
|
|
|
|
case http.MethodPut:
|
|
|
|
// Before save command handler.
|
2017-08-20 08:21:36 +00:00
|
|
|
path := filepath.Join(c.User.Scope, r.URL.Path)
|
2017-09-07 15:38:32 +00:00
|
|
|
if err := c.Runner("before_save", path, "", c.User); err != nil {
|
2017-07-08 16:51:47 +00:00
|
|
|
return http.StatusInternalServerError, err
|
|
|
|
}
|
|
|
|
|
|
|
|
code, err := resourcePostPutHandler(c, w, r)
|
|
|
|
if code != http.StatusOK {
|
|
|
|
return code, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// After save command handler.
|
2017-09-07 15:38:32 +00:00
|
|
|
if err := c.Runner("after_save", path, "", c.User); err != nil {
|
2017-07-08 16:51:47 +00:00
|
|
|
return http.StatusInternalServerError, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return code, err
|
|
|
|
case http.MethodPatch:
|
|
|
|
return resourcePatchHandler(c, w, r)
|
|
|
|
case http.MethodPost:
|
|
|
|
return resourcePostPutHandler(c, w, r)
|
|
|
|
}
|
|
|
|
|
|
|
|
return http.StatusNotImplemented, nil
|
|
|
|
}
|
|
|
|
|
2017-08-18 08:00:32 +00:00
|
|
|
func resourceGetHandler(c *fm.Context, w http.ResponseWriter, r *http.Request) (int, error) {
|
2017-07-26 08:44:09 +00:00
|
|
|
// Gets the information of the directory/file.
|
2017-08-19 11:35:44 +00:00
|
|
|
f, err := fm.GetInfo(r.URL, c.FileManager, c.User)
|
2017-07-08 16:51:47 +00:00
|
|
|
if err != nil {
|
2017-08-19 11:35:44 +00:00
|
|
|
return ErrorToHTTP(err, false), err
|
2017-07-08 16:51:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// If it's a dir and the path doesn't end with a trailing slash,
|
2017-07-26 08:44:09 +00:00
|
|
|
// add a trailing slash to the path.
|
2017-07-08 16:51:47 +00:00
|
|
|
if f.IsDir && !strings.HasSuffix(r.URL.Path, "/") {
|
|
|
|
r.URL.Path = r.URL.Path + "/"
|
|
|
|
}
|
|
|
|
|
|
|
|
// If it is a dir, go and serve the listing.
|
|
|
|
if f.IsDir {
|
2017-08-01 19:49:56 +00:00
|
|
|
c.File = f
|
2017-07-08 16:51:47 +00:00
|
|
|
return listingHandler(c, w, r)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tries to get the file type.
|
2017-07-26 08:44:09 +00:00
|
|
|
if err = f.GetFileType(true); err != nil {
|
2017-08-19 11:35:44 +00:00
|
|
|
return ErrorToHTTP(err, true), err
|
2017-07-08 16:51:47 +00:00
|
|
|
}
|
|
|
|
|
2017-07-26 08:44:09 +00:00
|
|
|
// Serve a preview if the file can't be edited or the
|
|
|
|
// user has no permission to edit this file. Otherwise,
|
|
|
|
// just serve the editor.
|
2017-07-11 15:58:18 +00:00
|
|
|
if !f.CanBeEdited() || !c.User.AllowEdit {
|
2017-07-08 16:51:47 +00:00
|
|
|
f.Kind = "preview"
|
2017-07-26 08:44:09 +00:00
|
|
|
return renderJSON(w, f)
|
|
|
|
}
|
2017-07-08 16:51:47 +00:00
|
|
|
|
2017-07-26 08:44:09 +00:00
|
|
|
f.Kind = "editor"
|
|
|
|
|
|
|
|
// Tries to get the editor data.
|
2017-08-19 11:35:44 +00:00
|
|
|
if err = f.GetEditor(); err != nil {
|
2017-07-26 08:44:09 +00:00
|
|
|
return http.StatusInternalServerError, err
|
2017-07-08 16:51:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return renderJSON(w, f)
|
|
|
|
}
|
|
|
|
|
2017-08-18 08:00:32 +00:00
|
|
|
func listingHandler(c *fm.Context, w http.ResponseWriter, r *http.Request) (int, error) {
|
2017-08-01 19:49:56 +00:00
|
|
|
f := c.File
|
2017-07-08 16:51:47 +00:00
|
|
|
f.Kind = "listing"
|
|
|
|
|
2017-07-26 08:44:09 +00:00
|
|
|
// Tries to get the listing data.
|
2017-08-19 11:35:44 +00:00
|
|
|
if err := f.GetListing(c.User, r); err != nil {
|
|
|
|
return ErrorToHTTP(err, true), err
|
2017-07-08 16:51:47 +00:00
|
|
|
}
|
|
|
|
|
2017-08-19 11:35:44 +00:00
|
|
|
listing := f.Listing
|
2017-07-08 16:51:47 +00:00
|
|
|
|
2017-07-26 08:44:09 +00:00
|
|
|
// Defines the cookie scope.
|
2017-08-01 19:49:56 +00:00
|
|
|
cookieScope := c.RootURL()
|
2017-07-08 16:51:47 +00:00
|
|
|
if cookieScope == "" {
|
|
|
|
cookieScope = "/"
|
|
|
|
}
|
|
|
|
|
|
|
|
// Copy the query values into the Listing struct
|
2017-07-26 08:44:09 +00:00
|
|
|
if sort, order, err := handleSortOrder(w, r, cookieScope); err == nil {
|
|
|
|
listing.Sort = sort
|
|
|
|
listing.Order = order
|
|
|
|
} else {
|
2017-07-08 16:51:47 +00:00
|
|
|
return http.StatusBadRequest, err
|
|
|
|
}
|
|
|
|
|
|
|
|
listing.ApplySort()
|
|
|
|
return renderJSON(w, f)
|
|
|
|
}
|
|
|
|
|
2017-08-18 08:00:32 +00:00
|
|
|
func resourceDeleteHandler(c *fm.Context, w http.ResponseWriter, r *http.Request) (int, error) {
|
2017-07-08 16:51:47 +00:00
|
|
|
// Prevent the removal of the root directory.
|
2017-07-14 06:51:32 +00:00
|
|
|
if r.URL.Path == "/" || !c.User.AllowEdit {
|
2017-07-08 16:51:47 +00:00
|
|
|
return http.StatusForbidden, nil
|
|
|
|
}
|
|
|
|
|
2017-09-07 15:38:32 +00:00
|
|
|
// Fire the before trigger.
|
|
|
|
if err := c.Runner("before_delete", r.URL.Path, "", c.User); err != nil {
|
|
|
|
return http.StatusInternalServerError, err
|
|
|
|
}
|
|
|
|
|
2017-07-08 16:51:47 +00:00
|
|
|
// Remove the file or folder.
|
2017-07-26 10:14:05 +00:00
|
|
|
err := c.User.FileSystem.RemoveAll(r.URL.Path)
|
2017-07-08 16:51:47 +00:00
|
|
|
if err != nil {
|
2017-08-19 11:35:44 +00:00
|
|
|
return ErrorToHTTP(err, true), err
|
2017-07-08 16:51:47 +00:00
|
|
|
}
|
|
|
|
|
2017-09-07 15:38:32 +00:00
|
|
|
// Fire the after trigger.
|
|
|
|
if err := c.Runner("after_delete", r.URL.Path, "", c.User); err != nil {
|
|
|
|
return http.StatusInternalServerError, err
|
|
|
|
}
|
|
|
|
|
2017-07-08 16:51:47 +00:00
|
|
|
return http.StatusOK, nil
|
|
|
|
}
|
|
|
|
|
2017-08-18 08:00:32 +00:00
|
|
|
func resourcePostPutHandler(c *fm.Context, w http.ResponseWriter, r *http.Request) (int, error) {
|
2017-07-14 06:51:32 +00:00
|
|
|
if !c.User.AllowNew && r.Method == http.MethodPost {
|
|
|
|
return http.StatusForbidden, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if !c.User.AllowEdit && r.Method == http.MethodPut {
|
|
|
|
return http.StatusForbidden, nil
|
|
|
|
}
|
|
|
|
|
2017-08-07 13:44:32 +00:00
|
|
|
// Discard any invalid upload before returning to avoid connection
|
2017-08-24 11:33:54 +00:00
|
|
|
// reset error.
|
2017-08-07 13:44:32 +00:00
|
|
|
defer func() {
|
|
|
|
io.Copy(ioutil.Discard, r.Body)
|
|
|
|
}()
|
|
|
|
|
2017-07-08 16:51:47 +00:00
|
|
|
// Checks if the current request is for a directory and not a file.
|
|
|
|
if strings.HasSuffix(r.URL.Path, "/") {
|
|
|
|
// If the method is PUT, we return 405 Method not Allowed, because
|
|
|
|
// POST should be used instead.
|
|
|
|
if r.Method == http.MethodPut {
|
|
|
|
return http.StatusMethodNotAllowed, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise we try to create the directory.
|
2017-08-11 08:33:47 +00:00
|
|
|
err := c.User.FileSystem.Mkdir(r.URL.Path, 0776)
|
2017-08-19 11:35:44 +00:00
|
|
|
return ErrorToHTTP(err, false), err
|
2017-07-08 16:51:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// If using POST method, we are trying to create a new file so it is not
|
2017-08-24 11:33:54 +00:00
|
|
|
// desirable to override an already existent file. Thus, we check
|
2017-07-08 16:51:47 +00:00
|
|
|
// if the file already exists. If so, we just return a 409 Conflict.
|
2017-08-24 11:33:54 +00:00
|
|
|
if r.Method == http.MethodPost && r.Header.Get("Action") != "override" {
|
2017-07-26 10:14:05 +00:00
|
|
|
if _, err := c.User.FileSystem.Stat(r.URL.Path); err == nil {
|
2017-07-08 16:51:47 +00:00
|
|
|
return http.StatusConflict, errors.New("There is already a file on that path")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-07 15:38:32 +00:00
|
|
|
// Fire the before trigger.
|
|
|
|
if err := c.Runner("before_upload", r.URL.Path, "", c.User); err != nil {
|
|
|
|
return http.StatusInternalServerError, err
|
|
|
|
}
|
|
|
|
|
2017-07-08 16:51:47 +00:00
|
|
|
// Create/Open the file.
|
2017-08-11 08:33:47 +00:00
|
|
|
f, err := c.User.FileSystem.OpenFile(r.URL.Path, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0776)
|
2017-07-08 16:51:47 +00:00
|
|
|
if err != nil {
|
2017-08-19 11:35:44 +00:00
|
|
|
return ErrorToHTTP(err, false), err
|
2017-07-08 16:51:47 +00:00
|
|
|
}
|
2017-07-23 19:30:55 +00:00
|
|
|
defer f.Close()
|
2017-07-08 16:51:47 +00:00
|
|
|
|
|
|
|
// Copies the new content for the file.
|
|
|
|
_, err = io.Copy(f, r.Body)
|
|
|
|
if err != nil {
|
2017-08-19 11:35:44 +00:00
|
|
|
return ErrorToHTTP(err, false), err
|
2017-07-08 16:51:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Gets the info about the file.
|
|
|
|
fi, err := f.Stat()
|
|
|
|
if err != nil {
|
2017-08-19 11:35:44 +00:00
|
|
|
return ErrorToHTTP(err, false), err
|
2017-07-08 16:51:47 +00:00
|
|
|
}
|
|
|
|
|
2017-08-09 14:06:28 +00:00
|
|
|
// Check if this instance has a Static Generator and handles publishing
|
|
|
|
// or scheduling if it's the case.
|
|
|
|
if c.StaticGen != nil {
|
|
|
|
code, err := resourcePublishSchedule(c, w, r)
|
|
|
|
if code != 0 {
|
|
|
|
return code, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-08 16:51:47 +00:00
|
|
|
// Writes the ETag Header.
|
|
|
|
etag := fmt.Sprintf(`"%x%x"`, fi.ModTime().UnixNano(), fi.Size())
|
|
|
|
w.Header().Set("ETag", etag)
|
2017-09-07 15:38:32 +00:00
|
|
|
|
|
|
|
// Fire the after trigger.
|
|
|
|
if err := c.Runner("after_upload", r.URL.Path, "", c.User); err != nil {
|
|
|
|
return http.StatusInternalServerError, err
|
|
|
|
}
|
|
|
|
|
2017-07-08 16:51:47 +00:00
|
|
|
return http.StatusOK, nil
|
|
|
|
}
|
|
|
|
|
2017-08-18 08:00:32 +00:00
|
|
|
func resourcePublishSchedule(c *fm.Context, w http.ResponseWriter, r *http.Request) (int, error) {
|
2017-08-09 14:06:28 +00:00
|
|
|
publish := r.Header.Get("Publish")
|
|
|
|
schedule := r.Header.Get("Schedule")
|
|
|
|
|
|
|
|
if publish != "true" && schedule == "" {
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if !c.User.AllowPublish {
|
|
|
|
return http.StatusForbidden, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if publish == "true" {
|
2017-08-10 14:04:07 +00:00
|
|
|
return resourcePublish(c, w, r)
|
2017-08-09 14:06:28 +00:00
|
|
|
}
|
|
|
|
|
2017-08-10 14:04:07 +00:00
|
|
|
t, err := time.Parse("2006-01-02T15:04", schedule)
|
|
|
|
if err != nil {
|
|
|
|
return http.StatusInternalServerError, err
|
|
|
|
}
|
|
|
|
|
2017-08-19 11:35:44 +00:00
|
|
|
c.Cron.AddFunc(t.Format("05 04 15 02 01 *"), func() {
|
2017-08-10 14:04:07 +00:00
|
|
|
_, err := resourcePublish(c, w, r)
|
|
|
|
if err != nil {
|
|
|
|
log.Print(err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
return http.StatusOK, nil
|
|
|
|
}
|
|
|
|
|
2017-08-18 08:00:32 +00:00
|
|
|
func resourcePublish(c *fm.Context, w http.ResponseWriter, r *http.Request) (int, error) {
|
2017-08-20 08:21:36 +00:00
|
|
|
path := filepath.Join(c.User.Scope, r.URL.Path)
|
2017-08-10 14:04:07 +00:00
|
|
|
|
|
|
|
// Before save command handler.
|
2017-09-07 15:38:32 +00:00
|
|
|
if err := c.Runner("before_publish", path, "", c.User); err != nil {
|
2017-08-10 14:04:07 +00:00
|
|
|
return http.StatusInternalServerError, err
|
|
|
|
}
|
|
|
|
|
|
|
|
code, err := c.StaticGen.Publish(c, w, r)
|
|
|
|
if err != nil {
|
|
|
|
return code, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Executed the before publish command.
|
2017-09-07 15:38:32 +00:00
|
|
|
if err := c.Runner("before_publish", path, "", c.User); err != nil {
|
2017-08-10 14:04:07 +00:00
|
|
|
return http.StatusInternalServerError, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return code, nil
|
2017-08-09 14:06:28 +00:00
|
|
|
}
|
|
|
|
|
2017-07-26 08:44:09 +00:00
|
|
|
// resourcePatchHandler is the entry point for resource handler.
|
2017-08-18 08:00:32 +00:00
|
|
|
func resourcePatchHandler(c *fm.Context, w http.ResponseWriter, r *http.Request) (int, error) {
|
2017-07-14 06:51:32 +00:00
|
|
|
if !c.User.AllowEdit {
|
|
|
|
return http.StatusForbidden, nil
|
|
|
|
}
|
|
|
|
|
2017-07-08 16:51:47 +00:00
|
|
|
dst := r.Header.Get("Destination")
|
2017-07-26 10:14:05 +00:00
|
|
|
action := r.Header.Get("Action")
|
2017-07-08 16:51:47 +00:00
|
|
|
dst, err := url.QueryUnescape(dst)
|
|
|
|
if err != nil {
|
2017-08-19 11:35:44 +00:00
|
|
|
return ErrorToHTTP(err, true), err
|
2017-07-08 16:51:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
src := r.URL.Path
|
|
|
|
|
|
|
|
if dst == "/" || src == "/" {
|
|
|
|
return http.StatusForbidden, nil
|
|
|
|
}
|
|
|
|
|
2017-07-26 10:14:05 +00:00
|
|
|
if action == "copy" {
|
2017-09-07 15:38:32 +00:00
|
|
|
// Fire the after trigger.
|
|
|
|
if err := c.Runner("before_copy", src, dst, c.User); err != nil {
|
|
|
|
return http.StatusInternalServerError, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Copy the file.
|
2017-07-26 10:14:05 +00:00
|
|
|
err = c.User.FileSystem.Copy(src, dst)
|
2017-09-07 15:38:32 +00:00
|
|
|
|
|
|
|
// Fire the after trigger.
|
|
|
|
if err := c.Runner("after_copy", src, dst, c.User); err != nil {
|
|
|
|
return http.StatusInternalServerError, err
|
|
|
|
}
|
2017-07-26 10:14:05 +00:00
|
|
|
} else {
|
2017-09-07 15:38:32 +00:00
|
|
|
// Fire the after trigger.
|
|
|
|
if err := c.Runner("before_rename", src, dst, c.User); err != nil {
|
|
|
|
return http.StatusInternalServerError, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Rename the file.
|
2017-07-26 10:14:05 +00:00
|
|
|
err = c.User.FileSystem.Rename(src, dst)
|
2017-09-07 15:38:32 +00:00
|
|
|
|
|
|
|
// Fire the after trigger.
|
|
|
|
if err := c.Runner("after_rename", src, dst, c.User); err != nil {
|
|
|
|
return http.StatusInternalServerError, err
|
|
|
|
}
|
2017-07-26 10:14:05 +00:00
|
|
|
}
|
|
|
|
|
2017-08-19 11:35:44 +00:00
|
|
|
return ErrorToHTTP(err, true), err
|
2017-07-08 16:51:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// handleSortOrder gets and stores for a Listing the 'sort' and 'order',
|
|
|
|
// and reads 'limit' if given. The latter is 0 if not given. Sets cookies.
|
|
|
|
func handleSortOrder(w http.ResponseWriter, r *http.Request, scope string) (sort string, order string, err error) {
|
|
|
|
sort = r.URL.Query().Get("sort")
|
|
|
|
order = r.URL.Query().Get("order")
|
|
|
|
|
|
|
|
// If the query 'sort' or 'order' is empty, use defaults or any values
|
|
|
|
// previously saved in Cookies.
|
|
|
|
switch sort {
|
|
|
|
case "":
|
|
|
|
sort = "name"
|
|
|
|
if sortCookie, sortErr := r.Cookie("sort"); sortErr == nil {
|
|
|
|
sort = sortCookie.Value
|
|
|
|
}
|
|
|
|
case "name", "size":
|
|
|
|
http.SetCookie(w, &http.Cookie{
|
|
|
|
Name: "sort",
|
|
|
|
Value: sort,
|
|
|
|
MaxAge: 31536000,
|
|
|
|
Path: scope,
|
|
|
|
Secure: r.TLS != nil,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
switch order {
|
|
|
|
case "":
|
|
|
|
order = "asc"
|
|
|
|
if orderCookie, orderErr := r.Cookie("order"); orderErr == nil {
|
|
|
|
order = orderCookie.Value
|
|
|
|
}
|
|
|
|
case "asc", "desc":
|
|
|
|
http.SetCookie(w, &http.Cookie{
|
|
|
|
Name: "order",
|
|
|
|
Value: order,
|
|
|
|
MaxAge: 31536000,
|
|
|
|
Path: scope,
|
|
|
|
Secure: r.TLS != nil,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|