filebrowser/http/share.go

125 lines
2.6 KiB
Go
Raw Normal View History

2017-08-18 08:00:32 +00:00
package http
import (
"encoding/hex"
"net/http"
"path/filepath"
"strconv"
"strings"
"time"
"github.com/asdine/storm"
2017-08-18 08:00:32 +00:00
fm "github.com/hacdias/filemanager"
)
2017-08-18 08:00:32 +00:00
func shareHandler(c *fm.Context, w http.ResponseWriter, r *http.Request) (int, error) {
r.URL.Path = sanitizeURL(r.URL.Path)
switch r.Method {
case http.MethodGet:
return shareGetHandler(c, w, r)
case http.MethodDelete:
return shareDeleteHandler(c, w, r)
case http.MethodPost:
return sharePostHandler(c, w, r)
}
return http.StatusNotImplemented, nil
}
2017-08-18 08:00:32 +00:00
func shareGetHandler(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-19 11:35:44 +00:00
s, err := c.Store.Share.GetByPath(path)
if err == storm.ErrNotFound {
return http.StatusNotFound, nil
}
if err != nil {
return http.StatusInternalServerError, err
}
for i, link := range s {
if link.Expires && link.ExpireDate.Before(time.Now()) {
2017-08-19 11:35:44 +00:00
c.Store.Share.Delete(link.Hash)
s = append(s[:i], s[i+1:]...)
}
}
return renderJSON(w, s)
}
2017-08-18 08:00:32 +00:00
func sharePostHandler(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-20 07:49:09 +00:00
var s *fm.ShareLink
expire := r.URL.Query().Get("expires")
unit := r.URL.Query().Get("unit")
if expire == "" {
2017-08-20 07:49:09 +00:00
var err error
s, err = c.Store.Share.GetPermanent(path)
if err == nil {
w.Write([]byte(c.RootURL() + "/share/" + s.Hash))
return 0, nil
}
}
2017-08-19 11:35:44 +00:00
bytes, err := fm.GenerateRandomBytes(32)
if err != nil {
return http.StatusInternalServerError, err
}
str := hex.EncodeToString(bytes)
2017-08-20 07:49:09 +00:00
s = &fm.ShareLink{
Path: path,
Hash: str,
Expires: expire != "",
}
if expire != "" {
num, err := strconv.Atoi(expire)
if err != nil {
return http.StatusInternalServerError, err
}
var add time.Duration
switch unit {
case "seconds":
add = time.Second * time.Duration(num)
case "minutes":
add = time.Minute * time.Duration(num)
case "days":
add = time.Hour * 24 * time.Duration(num)
default:
add = time.Hour * time.Duration(num)
}
s.ExpireDate = time.Now().Add(add)
}
2017-08-20 07:49:09 +00:00
if err := c.Store.Share.Save(s); err != nil {
return http.StatusInternalServerError, err
}
return renderJSON(w, s)
}
2017-08-18 08:00:32 +00:00
func shareDeleteHandler(c *fm.Context, w http.ResponseWriter, r *http.Request) (int, error) {
2017-08-19 11:35:44 +00:00
s, err := c.Store.Share.Get(strings.TrimPrefix(r.URL.Path, "/"))
if err == storm.ErrNotFound {
return http.StatusNotFound, nil
}
if err != nil {
return http.StatusInternalServerError, err
}
2017-08-19 11:35:44 +00:00
err = c.Store.Share.Delete(s.Hash)
if err != nil {
return http.StatusInternalServerError, err
}
return http.StatusOK, nil
}