docs: record auth hash alignment and validation results
This commit is contained in:
1 parent
22887fef03
commit
f7819904b9
51 files changed
+1792
-542
No files matched your search
@@ -43,6 +43,7 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/go-chi/chi/v5"
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
|
||||
@@ -335,13 +336,25 @@ func (c *Core) mediaView(w http.ResponseWriter, r *http.Request) {
|
||||
MediaKey string `json:"mediaKey"`
|
||||
}
|
||||
if r.Body != nil && r.ContentLength != 0 {
|
||||
_ = decodeRequestJSON(r, &input)
|
||||
if err := decodeRequestJSON(r, &input); err != nil {
|
||||
writeError(w, http.StatusBadRequest, "invalid JSON body")
|
||||
return
|
||||
}
|
||||
}
|
||||
key := strings.TrimSpace(firstNonEmpty(input.MediaKey, r.URL.Query().Get("mediaKey")))
|
||||
if key == "" {
|
||||
writeError(w, http.StatusBadRequest, "missing mediaKey")
|
||||
return
|
||||
}
|
||||
exists, err := c.mediaExists(r.Context(), key)
|
||||
if err != nil {
|
||||
writeError(w, http.StatusInternalServerError, "media lookup failed")
|
||||
return
|
||||
}
|
||||
if !exists {
|
||||
writeError(w, http.StatusNotFound, "media not found")
|
||||
return
|
||||
}
|
||||
var userID any
|
||||
if user != nil {
|
||||
userID = user.ID
|
||||
@@ -368,15 +381,25 @@ func (c *Core) mediaViewSession(w http.ResponseWriter, r *http.Request) {
|
||||
if duration <= 0 {
|
||||
duration = input.Duration
|
||||
}
|
||||
if strings.TrimSpace(input.MediaKey) == "" || duration <= 0 {
|
||||
writeError(w, http.StatusBadRequest, "mediaKey and positive duration are required")
|
||||
key := strings.TrimSpace(input.MediaKey)
|
||||
if key == "" || duration <= 0 || duration > maxViewSessionDurationMs {
|
||||
writeError(w, http.StatusBadRequest, "mediaKey and duration between 1ms and 24h are required")
|
||||
return
|
||||
}
|
||||
exists, err := c.mediaExists(r.Context(), key)
|
||||
if err != nil {
|
||||
writeError(w, http.StatusInternalServerError, "media lookup failed")
|
||||
return
|
||||
}
|
||||
if !exists {
|
||||
writeError(w, http.StatusNotFound, "media not found")
|
||||
return
|
||||
}
|
||||
var userID any
|
||||
if user != nil {
|
||||
userID = user.ID
|
||||
}
|
||||
_, err := c.db.Exec(r.Context(), `INSERT INTO media_view_sessions (media_key, user_id, duration_ms) VALUES ($1, $2, $3)`, strings.TrimSpace(input.MediaKey), userID, duration)
|
||||
_, err = c.db.Exec(r.Context(), `INSERT INTO media_view_sessions (media_key, user_id, duration_ms) VALUES ($1, $2, $3)`, key, userID, duration)
|
||||
if err != nil {
|
||||
writeError(w, http.StatusInternalServerError, "view session could not be recorded")
|
||||
return
|
||||
@@ -384,6 +407,15 @@ func (c *Core) mediaViewSession(w http.ResponseWriter, r *http.Request) {
|
||||
writeJSON(w, http.StatusOK, map[string]any{"success": true, "data": map[string]any{"ok": true}})
|
||||
}
|
||||
|
||||
const maxViewSessionDurationMs int64 = 24 * 60 * 60 * 1000
|
||||
|
||||
func (c *Core) mediaExists(ctx context.Context, identifier string) (bool, error) {
|
||||
var exists bool
|
||||
err := c.db.QueryRow(ctx, `SELECT EXISTS (SELECT 1 FROM media WHERE media_key=$1 OR id=$2)`,
|
||||
identifier, mediaIDArg(identifier)).Scan(&exists)
|
||||
return exists, err
|
||||
}
|
||||
|
||||
func (c *Core) mediaLiked(w http.ResponseWriter, r *http.Request) {
|
||||
user := sharedauth.UserFromContext(r.Context())
|
||||
rows, err := c.db.Query(r.Context(), `SELECT media_key, created_at FROM media_stats WHERE user_id = $1 AND stat_type = 'like' ORDER BY created_at DESC`, user.ID)
|
||||
@@ -793,9 +825,18 @@ func (c *Core) isHiddenMedia(ctx context.Context, identifier string) bool {
|
||||
return err != nil || hidden
|
||||
}
|
||||
|
||||
// findMedia looks up one media item by media key or numeric id. Hidden media is only returned
|
||||
// when includeHidden is set.
|
||||
func (c *Core) findMedia(ctx context.Context, identifier string, includeHidden bool) (MediaRecord, error) {
|
||||
// MediaLookupOptions controls which otherwise-hidden records a media lookup may return.
|
||||
type MediaLookupOptions struct {
|
||||
// IncludeHidden lets moderators look up media with active = FALSE.
|
||||
IncludeHidden bool
|
||||
// RequireContentScannerProcessed limits the result to items imported by ContentScanner.
|
||||
RequireContentScannerProcessed bool
|
||||
}
|
||||
|
||||
// FindMedia looks up one media item by media key or numeric id. Shared media routes
|
||||
// preserve moderator access to hidden items; ContentScanner-backed consumers can also
|
||||
// request the same processed-only filtering used by MediaViewer's library routes.
|
||||
func (c *Core) FindMedia(ctx context.Context, identifier string, options MediaLookupOptions) (MediaRecord, error) {
|
||||
identifier = strings.TrimSpace(identifier)
|
||||
var record MediaRecord
|
||||
row := c.db.QueryRow(ctx, `
|
||||
@@ -804,7 +845,8 @@ func (c *Core) findMedia(ctx context.Context, identifier string, includeHidden b
|
||||
COALESCE(width, 0), COALESCE(height, 0), COALESCE(duration_ms, 0), COALESCE(size_bytes, 0),
|
||||
COALESCE(is_vr, FALSE), COALESCE(has_funscript, FALSE), funscript_action_count,
|
||||
funscript_avg_speed, funscript_max_speed, COALESCE(speed, 0), COALESCE(modified_at, created_at)
|
||||
FROM media WHERE (media_key=$1 OR id=$2) AND (active OR $3) LIMIT 1`, identifier, mediaIDArg(identifier), includeHidden)
|
||||
FROM media WHERE (media_key=$1 OR id=$2) AND (active OR $3) AND (NOT $4 OR content_scanner_processed) LIMIT 1`,
|
||||
identifier, mediaIDArg(identifier), options.IncludeHidden, options.RequireContentScannerProcessed)
|
||||
var title, description string
|
||||
var width, height int
|
||||
var duration, size int64
|
||||
@@ -850,6 +892,12 @@ func (c *Core) findMedia(ctx context.Context, identifier string, includeHidden b
|
||||
return record, nil
|
||||
}
|
||||
|
||||
// findMedia keeps the shared routes on the same lookup implementation used by
|
||||
// media-family applications that need ContentScanner's processed-only filter.
|
||||
func (c *Core) findMedia(ctx context.Context, identifier string, includeHidden bool) (MediaRecord, error) {
|
||||
return c.FindMedia(ctx, identifier, MediaLookupOptions{IncludeHidden: includeHidden})
|
||||
}
|
||||
|
||||
// ToJSON is the shared wire shape for a media record, matching every
|
||||
// mediacore route's own response shape (id/mediaKey/filename/url/thumbnail/
|
||||
// etc.) - services with their own media listing routes (e.g. MediaViewer's
|
||||
@@ -875,11 +923,28 @@ func (record MediaRecord) ToJSON() map[string]any {
|
||||
}
|
||||
}
|
||||
|
||||
// SettingValue reads one key from the shared `settings` table, returning
|
||||
// fallback if it is unset or the read fails.
|
||||
func SettingValue(ctx context.Context, db *pgxpool.Pool, key, fallback string) string {
|
||||
// SettingValue reads one key from the shared `settings` table. An absent or empty
|
||||
// setting returns fallback; database errors are returned to the caller.
|
||||
func SettingValue(ctx context.Context, db *pgxpool.Pool, key, fallback string) (string, error) {
|
||||
var value string
|
||||
if err := db.QueryRow(ctx, `SELECT COALESCE(value, '') FROM settings WHERE key=$1`, key).Scan(&value); err != nil || value == "" {
|
||||
err := db.QueryRow(ctx, `SELECT COALESCE(value, '') FROM settings WHERE key=$1`, key).Scan(&value)
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return fallback, nil
|
||||
}
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if value == "" {
|
||||
return fallback, nil
|
||||
}
|
||||
return value, nil
|
||||
}
|
||||
|
||||
// SettingValueOrFallback preserves the old fallback-on-error behavior for callers
|
||||
// whose API cannot return a read error. New request paths should use SettingValue.
|
||||
func SettingValueOrFallback(ctx context.Context, db *pgxpool.Pool, key, fallback string) string {
|
||||
value, err := SettingValue(ctx, db, key, fallback)
|
||||
if err != nil {
|
||||
return fallback
|
||||
}
|
||||
return value
|
||||
@@ -888,7 +953,7 @@ func SettingValue(ctx context.Context, db *pgxpool.Pool, key, fallback string) s
|
||||
// SettingBool reads a boolean-ish setting ("1"/"true"/"yes"/"on" are true,
|
||||
// anything else is fallback).
|
||||
func SettingBool(ctx context.Context, db *pgxpool.Pool, key string, fallback bool) bool {
|
||||
value := strings.ToLower(strings.TrimSpace(SettingValue(ctx, db, key, "")))
|
||||
value := strings.ToLower(strings.TrimSpace(SettingValueOrFallback(ctx, db, key, "")))
|
||||
if value == "" {
|
||||
return fallback
|
||||
}
|
||||
@@ -922,19 +987,19 @@ var carrierNodeInstancePattern = regexp.MustCompile(`^carriernode(_\d+)?$`)
|
||||
// Returns fallback if no carriernode* instance is registered (or none has a URL) yet.
|
||||
func CarrierNodeURLs(ctx context.Context, db *pgxpool.Pool, fallback []string) []string {
|
||||
var registered []string
|
||||
_ = json.Unmarshal([]byte(SettingValue(ctx, db, "configured_central", "")), ®istered)
|
||||
_ = json.Unmarshal([]byte(SettingValueOrFallback(ctx, db, "configured_central", "")), ®istered)
|
||||
var urls []string
|
||||
for _, project := range registered {
|
||||
key := strings.ToLower(strings.TrimSpace(project))
|
||||
if !carrierNodeInstancePattern.MatchString(key) {
|
||||
continue
|
||||
}
|
||||
if url := strings.TrimSpace(SettingValue(ctx, db, key+"_url", "")); url != "" {
|
||||
if url := strings.TrimSpace(SettingValueOrFallback(ctx, db, key+"_url", "")); url != "" {
|
||||
urls = append(urls, url)
|
||||
}
|
||||
}
|
||||
if len(urls) == 0 {
|
||||
if url := strings.TrimSpace(SettingValue(ctx, db, "carriernode_url", "")); url != "" {
|
||||
if url := strings.TrimSpace(SettingValueOrFallback(ctx, db, "carriernode_url", "")); url != "" {
|
||||
urls = []string{url}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user