2025-01-18 17:22:08 -05:00
|
|
|
package rest
|
|
|
|
|
|
|
|
import (
|
2025-01-19 21:51:39 -05:00
|
|
|
"errors"
|
2025-01-18 17:22:08 -05:00
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
2025-01-19 21:51:39 -05:00
|
|
|
"time"
|
2025-01-18 17:22:08 -05:00
|
|
|
|
|
|
|
"dynatron.me/x/stillbox/internal/forms"
|
2025-01-19 21:51:39 -05:00
|
|
|
"dynatron.me/x/stillbox/pkg/rbac"
|
|
|
|
"dynatron.me/x/stillbox/pkg/shares"
|
2025-01-18 17:22:08 -05:00
|
|
|
|
|
|
|
"github.com/go-chi/chi/v5"
|
|
|
|
)
|
|
|
|
|
2025-01-19 21:51:39 -05:00
|
|
|
var (
|
|
|
|
ErrBadShare = errors.New("bad share request type")
|
|
|
|
)
|
|
|
|
|
|
|
|
type ShareRequestType string
|
|
|
|
|
|
|
|
const (
|
|
|
|
ShareRequestCall ShareRequestType = "call"
|
|
|
|
ShareRequestIncident ShareRequestType = "incident"
|
|
|
|
ShareRequestIncidentM3U ShareRequestType = "m3u"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (rt ShareRequestType) IsValid() bool {
|
|
|
|
switch rt {
|
|
|
|
case ShareRequestCall, ShareRequestIncident, ShareRequestIncidentM3U:
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
type ShareHandlers map[shares.EntityType]http.Handler
|
2025-01-18 17:22:08 -05:00
|
|
|
type shareAPI struct {
|
|
|
|
baseURL *url.URL
|
2025-01-19 21:51:39 -05:00
|
|
|
|
|
|
|
router http.Handler
|
2025-01-18 17:22:08 -05:00
|
|
|
}
|
|
|
|
|
2025-01-19 21:51:39 -05:00
|
|
|
func newShareAPI(baseURL *url.URL, hand http.Handler) publicAPI {
|
|
|
|
return &shareAPI{
|
|
|
|
baseURL: baseURL,
|
|
|
|
router: hand,
|
|
|
|
}
|
2025-01-18 17:22:08 -05:00
|
|
|
}
|
|
|
|
|
2025-01-19 21:51:39 -05:00
|
|
|
func (sa *shareAPI) Subrouter() http.Handler {
|
2025-01-18 17:22:08 -05:00
|
|
|
r := chi.NewMux()
|
|
|
|
|
2025-01-19 21:51:39 -05:00
|
|
|
r.Post(`/create`, sa.createShare)
|
|
|
|
r.Delete(`/{id:[A-Za-z0-9_-]{20,}}`, sa.deleteShare)
|
2025-01-18 17:22:08 -05:00
|
|
|
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
2025-01-19 21:51:39 -05:00
|
|
|
func (sa *shareAPI) PublicRouter() http.Handler {
|
|
|
|
r := chi.NewMux()
|
2025-01-18 17:22:08 -05:00
|
|
|
|
2025-01-19 21:51:39 -05:00
|
|
|
r.Get(`/{type}/{id:[A-Za-z0-9_-]{20,}}`, sa.getShare)
|
|
|
|
r.Get(`/{type}/{id:[A-Za-z0-9_-]{20,}}*`, sa.getShare)
|
2025-01-18 17:22:08 -05:00
|
|
|
|
2025-01-19 21:51:39 -05:00
|
|
|
return r
|
2025-01-18 17:22:08 -05:00
|
|
|
}
|
|
|
|
|
2025-01-19 21:51:39 -05:00
|
|
|
func (sa *shareAPI) createShare(w http.ResponseWriter, r *http.Request) {
|
2025-01-18 17:22:08 -05:00
|
|
|
ctx := r.Context()
|
2025-01-19 21:51:39 -05:00
|
|
|
shs := shares.FromCtx(ctx)
|
|
|
|
|
|
|
|
p := shares.CreateShareParams{}
|
2025-01-18 17:22:08 -05:00
|
|
|
|
|
|
|
err := forms.Unmarshal(r, &p, forms.WithTag("json"), forms.WithAcceptBlank(), forms.WithOmitEmpty())
|
|
|
|
if err != nil {
|
|
|
|
wErr(w, r, badRequest(err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2025-01-19 21:51:39 -05:00
|
|
|
sh, err := shs.NewShare(ctx, p)
|
2025-01-18 17:22:08 -05:00
|
|
|
if err != nil {
|
|
|
|
wErr(w, r, autoError(err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2025-01-19 21:51:39 -05:00
|
|
|
respond(w, r, sh)
|
2025-01-18 17:22:08 -05:00
|
|
|
}
|
|
|
|
|
2025-01-19 21:51:39 -05:00
|
|
|
func (sa *shareAPI) deleteShare(w http.ResponseWriter, r *http.Request) {
|
2025-01-18 17:22:08 -05:00
|
|
|
}
|
|
|
|
|
2025-01-19 21:51:39 -05:00
|
|
|
func (sa *shareAPI) getShare(w http.ResponseWriter, r *http.Request) {
|
2025-01-18 17:22:08 -05:00
|
|
|
ctx := r.Context()
|
2025-01-19 21:51:39 -05:00
|
|
|
shs := shares.FromCtx(ctx)
|
2025-01-18 17:22:08 -05:00
|
|
|
|
2025-01-19 21:51:39 -05:00
|
|
|
rType, id, err := shareParams(w, r)
|
2025-01-18 17:22:08 -05:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2025-01-19 21:51:39 -05:00
|
|
|
if !rType.IsValid() {
|
|
|
|
wErr(w, r, autoError(ErrBadShare))
|
2025-01-18 17:22:08 -05:00
|
|
|
}
|
|
|
|
|
2025-01-19 21:51:39 -05:00
|
|
|
sh, err := shs.GetShare(ctx, id)
|
2025-01-18 17:22:08 -05:00
|
|
|
if err != nil {
|
|
|
|
wErr(w, r, autoError(err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2025-01-19 21:51:39 -05:00
|
|
|
if sh.Expiration != nil && sh.Expiration.Time().Before(time.Now()) {
|
|
|
|
wErr(w, r, autoError(shares.ErrNoShare))
|
2025-01-18 17:22:08 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2025-01-19 21:51:39 -05:00
|
|
|
ctx = rbac.CtxWithSubject(ctx, sh)
|
|
|
|
r = r.WithContext(ctx)
|
2025-01-18 17:22:08 -05:00
|
|
|
|
2025-01-19 21:51:39 -05:00
|
|
|
switch rType {
|
|
|
|
case ShareRequestCall, ShareRequestIncident:
|
|
|
|
r.URL.Path = fmt.Sprintf("/%s/%s", rType, sh.EntityID.String())
|
|
|
|
case ShareRequestIncidentM3U:
|
|
|
|
r.URL.Path = fmt.Sprintf("/incident/%s.m3u", sh.EntityID.String())
|
2025-01-18 17:22:08 -05:00
|
|
|
}
|
|
|
|
|
2025-01-19 21:51:39 -05:00
|
|
|
sa.router.ServeHTTP(w, r)
|
2025-01-18 17:22:08 -05:00
|
|
|
}
|
|
|
|
|
2025-01-19 21:51:39 -05:00
|
|
|
// idOnlyParam checks for a sole URL parameter, id, and writes an errorif this fails.
|
|
|
|
func shareParams(w http.ResponseWriter, r *http.Request) (rType ShareRequestType, rID string, err error) {
|
|
|
|
params := struct {
|
|
|
|
Type string `param:"type"`
|
|
|
|
ID string `param:"id"`
|
|
|
|
}{}
|
2025-01-18 17:22:08 -05:00
|
|
|
|
2025-01-19 21:51:39 -05:00
|
|
|
err = decodeParams(¶ms, r)
|
2025-01-18 17:22:08 -05:00
|
|
|
if err != nil {
|
2025-01-19 21:51:39 -05:00
|
|
|
wErr(w, r, badRequest(err))
|
|
|
|
return "", "", err
|
2025-01-18 17:22:08 -05:00
|
|
|
}
|
|
|
|
|
2025-01-19 21:51:39 -05:00
|
|
|
return ShareRequestType(params.Type), params.ID, nil
|
2025-01-18 17:22:08 -05:00
|
|
|
}
|