Alert media: images/GIFs/videos/sounds tied to donation amount or reward; release tag collision fix
- Alerts\Resolver/Media/Template/AlertMedia/DefInput/DefRepository; Alerts admin screen with Media Library picker and live preview - Overlay: media resolved server-side from attachment IDs (MIME whitelist), playback with timeouts and autoplay-failure fallbacks - Workflow: pick the next free minute if the version tag already exists (two pushes in one minute failed 'Create release') Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
1201db175b
commit
2c16a9ebbb
22 files changed
+1591
-22
No files matched your search
@@ -0,0 +1,76 @@
|
||||
<?php
|
||||
declare( strict_types=1 );
|
||||
|
||||
namespace StreamGoals\Alerts;
|
||||
|
||||
/** Pure helpers: MIME whitelist, type classification and duration clamping for alert media. */
|
||||
final class Media {
|
||||
|
||||
/** Exact MIME => kind. Everything else (svg, html, ...) is rejected. */
|
||||
public const MIMES = array(
|
||||
'image/png' => 'image',
|
||||
'image/jpeg' => 'image',
|
||||
'image/gif' => 'image',
|
||||
'image/webp' => 'image',
|
||||
'video/mp4' => 'video',
|
||||
'video/webm' => 'video',
|
||||
'audio/mpeg' => 'audio',
|
||||
'audio/ogg' => 'audio',
|
||||
'audio/wav' => 'audio',
|
||||
);
|
||||
|
||||
public const WARN_BYTES = 26214400; // 25 MB
|
||||
public const MIN_DURATION_MS = 1000;
|
||||
public const MAX_DURATION_MS = 60000;
|
||||
public const DEFAULT_MS = 6000;
|
||||
public const VIDEO_CAP_MS = 30000;
|
||||
|
||||
/** 'image' | 'video' | 'audio' for an allowed MIME, else null. Strict: no parameters, no case games. */
|
||||
public static function kind( mixed $mime ): ?string {
|
||||
if ( ! is_string( $mime ) ) {
|
||||
return null;
|
||||
}
|
||||
return self::MIMES[ strtolower( $mime ) ] ?? null;
|
||||
}
|
||||
|
||||
public static function isAllowed( mixed $mime ): bool {
|
||||
return null !== self::kind( $mime );
|
||||
}
|
||||
|
||||
/** 'image' | 'video' for a visual attachment MIME, else null (audio is not a visual). */
|
||||
public static function visualType( mixed $mime ): ?string {
|
||||
$k = self::kind( $mime );
|
||||
return 'image' === $k || 'video' === $k ? $k : null;
|
||||
}
|
||||
|
||||
public static function isSound( mixed $mime ): bool {
|
||||
return 'audio' === self::kind( $mime );
|
||||
}
|
||||
|
||||
public static function clampDurationMs( mixed $ms, int $default = self::DEFAULT_MS ): int {
|
||||
if ( ! is_numeric( $ms ) ) {
|
||||
return $default;
|
||||
}
|
||||
return max( self::MIN_DURATION_MS, min( self::MAX_DURATION_MS, (int) $ms ) );
|
||||
}
|
||||
|
||||
/** Whole seconds (1-60) to clamped milliseconds; null when the input is not a number. */
|
||||
public static function secondsToMs( mixed $s ): ?int {
|
||||
if ( ! is_scalar( $s ) || ! preg_match( '/^\s*\d{1,4}(\.\d+)?\s*$/', (string) $s ) ) {
|
||||
return null;
|
||||
}
|
||||
return self::clampDurationMs( (int) round( (float) $s * 1000 ) );
|
||||
}
|
||||
|
||||
public static function clampVolume( mixed $v, int $default = 80 ): int {
|
||||
return is_numeric( $v ) ? max( 0, min( 100, (int) $v ) ) : $default;
|
||||
}
|
||||
|
||||
/** Only plain http(s) URLs are ever handed to the overlay. */
|
||||
public static function safeUrl( mixed $url ): ?string {
|
||||
if ( ! is_string( $url ) || '' === $url || strlen( $url ) > 2000 || preg_match( '/[\x00-\x20\x7F"\'<>\\\\]/', $url ) ) {
|
||||
return null;
|
||||
}
|
||||
return preg_match( '#^https?://[^/]#i', $url ) ? $url : null;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user