mirror of
https://github.com/filebrowser/filebrowser.git
synced 2024-06-07 23:00:43 +00:00
01929c72ea
Former-commit-id: a54bc7c8a0fb700d4f90f78c54d6cb028e8acea7 [formerly a0946a1d5b82ce5681684bc0f871bd892c4d6c3f] [formerly 6b23b0abbe5cde6a6348f982975d15a1161359de [formerly 0e7abaa7fb
]]
Former-commit-id: cf9ea1110bcb8b5eb30ecd25db1928659ecea922 [formerly 38cebeff1f9e6b03760f7b277a3941f7bd733fb5]
Former-commit-id: db44c1d5ff8fed361849b5bf1fe48e04c75d7ce7
108 lines
2.3 KiB
Go
108 lines
2.3 KiB
Go
package http
|
|
|
|
import (
|
|
"crypto/rand"
|
|
"encoding/base64"
|
|
"net/http"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/filebrowser/filebrowser/v2/errors"
|
|
"github.com/filebrowser/filebrowser/v2/share"
|
|
)
|
|
|
|
func withPermShare(fn handleFunc) handleFunc {
|
|
return withUser(func(w http.ResponseWriter, r *http.Request, d *data) (int, error) {
|
|
if !d.user.Perm.Share {
|
|
return http.StatusForbidden, nil
|
|
}
|
|
|
|
return fn(w, r, d)
|
|
})
|
|
}
|
|
|
|
var shareGetsHandler = withPermShare(func(w http.ResponseWriter, r *http.Request, d *data) (int, error) {
|
|
s, err := d.store.Share.Gets(r.URL.Path, d.user.ID)
|
|
if err == errors.ErrNotExist {
|
|
return renderJSON(w, r, []*share.Link{})
|
|
}
|
|
|
|
if err != nil {
|
|
return http.StatusInternalServerError, err
|
|
}
|
|
|
|
return renderJSON(w, r, s)
|
|
})
|
|
|
|
var shareDeleteHandler = withPermShare(func(w http.ResponseWriter, r *http.Request, d *data) (int, error) {
|
|
hash := strings.TrimSuffix(r.URL.Path, "/")
|
|
hash = strings.TrimPrefix(hash, "/")
|
|
|
|
if hash == "" {
|
|
return http.StatusBadRequest, nil
|
|
}
|
|
|
|
err := d.store.Share.Delete(hash)
|
|
return errToStatus(err), err
|
|
})
|
|
|
|
var sharePostHandler = withPermShare(func(w http.ResponseWriter, r *http.Request, d *data) (int, error) {
|
|
var s *share.Link
|
|
rawExpire := r.URL.Query().Get("expires")
|
|
unit := r.URL.Query().Get("unit")
|
|
|
|
if rawExpire == "" {
|
|
var err error
|
|
s, err = d.store.Share.GetPermanent(r.URL.Path, d.user.ID)
|
|
if err == nil {
|
|
w.Write([]byte(d.server.BaseURL + "/share/" + s.Hash))
|
|
return 0, nil
|
|
}
|
|
}
|
|
|
|
bytes := make([]byte, 6)
|
|
_, err := rand.Read(bytes)
|
|
if err != nil {
|
|
return http.StatusInternalServerError, err
|
|
}
|
|
|
|
str := base64.URLEncoding.EncodeToString(bytes)
|
|
|
|
var expire int64 = 0
|
|
|
|
if rawExpire != "" {
|
|
num, err := strconv.Atoi(rawExpire)
|
|
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)
|
|
}
|
|
|
|
expire = time.Now().Add(add).Unix()
|
|
}
|
|
|
|
s = &share.Link{
|
|
Path: r.URL.Path,
|
|
Hash: str,
|
|
Expire: expire,
|
|
UserID: d.user.ID,
|
|
}
|
|
|
|
if err := d.store.Share.Save(s); err != nil {
|
|
return http.StatusInternalServerError, err
|
|
}
|
|
|
|
return renderJSON(w, r, s)
|
|
})
|