ci: expand CI checks and make consumer sync opt-in
- Add Go, npm, Python, and Java validation to CI - Run Go tests against PostgreSQL - Record performance fixes in the report
This commit is contained in:
1 parent
199a5efaef
commit
22887fef03
100 files changed
+9740
-429
No files matched your search
@@ -51,6 +51,10 @@ import (
|
||||
"repos.gfranz.ch/Gabriel20xx/shared/backend/syncsocket"
|
||||
)
|
||||
|
||||
// maxPlaylistReorderItems caps one reorder request. The reorder runs as a single statement, so the
|
||||
// cap bounds its size; a playlist longer than this is reordered in more than one request.
|
||||
const maxPlaylistReorderItems = 10000
|
||||
|
||||
// Config wires a Core to the rest of one service's dependencies.
|
||||
type Config struct {
|
||||
DB *pgxpool.Pool
|
||||
@@ -157,6 +161,8 @@ func (c *Core) putSyncState(w http.ResponseWriter, r *http.Request) {
|
||||
state["playAt"] = playAt
|
||||
}
|
||||
c.sync.SetHTTPState(sessionID, state)
|
||||
// Broadcast queues immutable messages without waiting for network writes, so a slow
|
||||
// websocket peer does not hold this response or create one goroutine per request.
|
||||
c.sync.Broadcast(sessionID)
|
||||
writeJSON(w, http.StatusOK, map[string]any{"success": true, "state": state})
|
||||
}
|
||||
@@ -194,7 +200,12 @@ func (c *Core) streamMediaIdentifier(w http.ResponseWriter, r *http.Request, ide
|
||||
writeError(w, http.StatusNotFound, "media not found")
|
||||
return
|
||||
}
|
||||
c.carrierNode.Proxy(w, r, "/media/"+urlPath(record.FilePath))
|
||||
upstream, ok := upstreamMediaPath(record.FilePath)
|
||||
if !ok {
|
||||
writeError(w, http.StatusNotFound, "media not found")
|
||||
return
|
||||
}
|
||||
c.carrierNode.Proxy(w, r, "/media/"+upstream)
|
||||
}
|
||||
|
||||
func (c *Core) thumbnailMediaByQuery(w http.ResponseWriter, r *http.Request) {
|
||||
@@ -222,9 +233,9 @@ func (c *Core) thumbnailMediaIdentifier(w http.ResponseWriter, r *http.Request,
|
||||
SELECT t.id::text
|
||||
FROM thumbnails t
|
||||
JOIN media m ON m.id = t.media_id OR t.source_path = COALESCE(NULLIF(m.file_path, ''), m.media_key)
|
||||
WHERE m.media_key=$1 OR CAST(m.id AS TEXT)=$1
|
||||
WHERE m.media_key=$1 OR m.id=$2
|
||||
ORDER BY t.created_at DESC
|
||||
LIMIT 1`, id).Scan(&thumbnailID); err == nil && SafeRelative(thumbnailID) {
|
||||
LIMIT 1`, id, mediaIDArg(id)).Scan(&thumbnailID); err == nil && SafeRelative(thumbnailID) {
|
||||
// A row whose file CarrierNode no longer has falls through to the original image below,
|
||||
// rather than proxying CarrierNode's 404.
|
||||
if c.carrierNode.ProxyThumbnail(w, r, c.serviceName, thumbnailID) {
|
||||
@@ -236,7 +247,12 @@ func (c *Core) thumbnailMediaIdentifier(w http.ResponseWriter, r *http.Request,
|
||||
writeError(w, http.StatusNotFound, "thumbnail not found")
|
||||
return
|
||||
}
|
||||
c.carrierNode.Proxy(w, r, "/media/"+urlPath(record.FilePath))
|
||||
upstream, ok := upstreamMediaPath(record.FilePath)
|
||||
if !ok {
|
||||
writeError(w, http.StatusNotFound, "thumbnail not found")
|
||||
return
|
||||
}
|
||||
c.carrierNode.Proxy(w, r, "/media/"+upstream)
|
||||
}
|
||||
|
||||
func (c *Core) mediaState(w http.ResponseWriter, r *http.Request) {
|
||||
@@ -390,9 +406,9 @@ func (c *Core) mediaLiked(w http.ResponseWriter, r *http.Request) {
|
||||
func (c *Core) mediaTags(w http.ResponseWriter, r *http.Request) {
|
||||
key := strings.TrimSpace(chi.URLParam(r, "id"))
|
||||
rows, err := c.db.Query(r.Context(), `
|
||||
SELECT tag, COALESCE(SUM(vote), 0) AS score
|
||||
SELECT mt.tag, COALESCE(SUM(mv.vote), 0) AS score
|
||||
FROM media_tags mt LEFT JOIN media_tag_votes mv ON mv.media_key = mt.media_key AND LOWER(mv.tag) = LOWER(mt.tag)
|
||||
WHERE mt.media_key = $1 GROUP BY tag ORDER BY score DESC, LOWER(tag)`, key)
|
||||
WHERE mt.media_key = $1 GROUP BY mt.tag ORDER BY score DESC, LOWER(mt.tag)`, key)
|
||||
if err != nil {
|
||||
writeJSON(w, http.StatusOK, map[string]any{"tags": []any{}})
|
||||
return
|
||||
@@ -723,8 +739,31 @@ func (c *Core) reorderPlaylist(w http.ResponseWriter, r *http.Request) {
|
||||
if len(ids) == 0 {
|
||||
ids = input.ItemIDs
|
||||
}
|
||||
for position, itemID := range ids {
|
||||
_, _ = c.db.Exec(r.Context(), `UPDATE playlist_items SET position=$1 WHERE id=$2 AND playlist_id=$3 AND EXISTS (SELECT 1 FROM playlists WHERE id=$3 AND user_id=$4)`, position, itemID, id, user.ID)
|
||||
if len(ids) > maxPlaylistReorderItems {
|
||||
writeError(w, http.StatusBadRequest, "too many items to reorder")
|
||||
return
|
||||
}
|
||||
positions := make([]int32, len(ids))
|
||||
for position := range ids {
|
||||
positions[position] = int32(position)
|
||||
}
|
||||
tx, err := c.db.Begin(r.Context())
|
||||
if err != nil {
|
||||
writeError(w, http.StatusInternalServerError, "could not reorder playlist")
|
||||
return
|
||||
}
|
||||
defer func() { _ = tx.Rollback(r.Context()) }()
|
||||
if _, err := tx.Exec(r.Context(), `
|
||||
UPDATE playlist_items AS i SET position=v.position
|
||||
FROM unnest($1::bigint[], $2::int[]) AS v(item_id, position)
|
||||
WHERE i.id=v.item_id AND i.playlist_id=$3
|
||||
AND EXISTS (SELECT 1 FROM playlists WHERE id=$3 AND user_id=$4)`, ids, positions, id, user.ID); err != nil {
|
||||
writeError(w, http.StatusInternalServerError, "could not reorder playlist")
|
||||
return
|
||||
}
|
||||
if err := tx.Commit(r.Context()); err != nil {
|
||||
writeError(w, http.StatusInternalServerError, "could not reorder playlist")
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusOK, success(map[string]any{"ok": true}, "Playlist reordered"))
|
||||
}
|
||||
@@ -749,8 +788,8 @@ func canSeeHidden(r *http.Request) bool {
|
||||
func (c *Core) isHiddenMedia(ctx context.Context, identifier string) bool {
|
||||
var hidden bool
|
||||
err := c.db.QueryRow(ctx, `
|
||||
SELECT EXISTS (SELECT 1 FROM media WHERE NOT active AND (media_key=$1 OR CAST(id AS TEXT)=$1))`,
|
||||
strings.TrimSpace(identifier)).Scan(&hidden)
|
||||
SELECT EXISTS (SELECT 1 FROM media WHERE NOT active AND (media_key=$1 OR id=$2))`,
|
||||
strings.TrimSpace(identifier), mediaIDArg(strings.TrimSpace(identifier))).Scan(&hidden)
|
||||
return err != nil || hidden
|
||||
}
|
||||
|
||||
@@ -765,7 +804,7 @@ 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 CAST(id AS TEXT)=$1) AND (active OR $2) LIMIT 1`, identifier, includeHidden)
|
||||
FROM media WHERE (media_key=$1 OR id=$2) AND (active OR $3) LIMIT 1`, identifier, mediaIDArg(identifier), includeHidden)
|
||||
var title, description string
|
||||
var width, height int
|
||||
var duration, size int64
|
||||
@@ -1029,6 +1068,46 @@ func urlPath(value string) string {
|
||||
return value
|
||||
}
|
||||
|
||||
// upstreamMediaPath returns the escaped CarrierNode path for a stored file path. It reports false
|
||||
// when the stored path is not a safe relative path, or when it contains a query or fragment marker
|
||||
// that CarrierNode would read as the end of the path.
|
||||
func upstreamMediaPath(filePath string) (string, bool) {
|
||||
relative := urlPath(filePath)
|
||||
if !SafeRelative(relative) || strings.ContainsAny(relative, "?#") {
|
||||
return "", false
|
||||
}
|
||||
// Escape each segment so percent-escapes and other reserved characters in a stored name
|
||||
// reach CarrierNode as literal text, not as an escape or a traversal it decodes later.
|
||||
segments := strings.Split(relative, "/")
|
||||
for i, segment := range segments {
|
||||
segments[i] = url.PathEscape(segment)
|
||||
}
|
||||
return strings.Join(segments, "/"), true
|
||||
}
|
||||
|
||||
// canonicalMediaID returns the numeric media id that identifier spells exactly. Leading zeros, signs
|
||||
// and whitespace are not canonical, so they never match an id.
|
||||
func canonicalMediaID(identifier string) (int64, bool) {
|
||||
if identifier == "" || strings.Trim(identifier, "0123456789") != "" {
|
||||
return 0, false
|
||||
}
|
||||
id, err := strconv.ParseInt(identifier, 10, 64)
|
||||
if err != nil || strconv.FormatInt(id, 10) != identifier {
|
||||
return 0, false
|
||||
}
|
||||
return id, true
|
||||
}
|
||||
|
||||
// mediaIDArg is the id query argument for identifier: the id when identifier is a canonical number,
|
||||
// otherwise NULL, which matches no row. Comparing the id column directly, instead of casting it to
|
||||
// text, lets the planner use the primary-key index for the id arm.
|
||||
func mediaIDArg(identifier string) any {
|
||||
if id, ok := canonicalMediaID(identifier); ok {
|
||||
return id
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func firstNonEmpty(values ...string) string {
|
||||
for _, value := range values {
|
||||
if strings.TrimSpace(value) != "" {
|
||||
|
||||
Reference in new issue
Block a user