fix and update

This commit is contained in:
lendry
2026-06-30 19:47:26 +03:00
parent d41c9d1121
commit 6ee3ffe0a5
6 changed files with 57 additions and 11 deletions

View File

@@ -3,6 +3,8 @@ import { ensureApiGatewayReady } from '@/lib/api';
const GATEWAY_RETRY_STATUS = new Set([502, 503, 504]);
const MEDIA_FETCH_MAX_ATTEMPTS = 8;
const MEDIA_SLOW_LOAD_WARN_MS = 8_000;
const MEDIA_STREAM_PATH = '/media/stream/';
const SAME_ORIGIN_API_PREFIX = '/idp-api';
function sleep(ms: number): Promise<void> {
return new Promise((resolve) => setTimeout(resolve, ms));
@@ -14,6 +16,23 @@ function warnSlowMediaLoad(label: string, startedAt: number, error: unknown) {
console.warn(`[media] ${label}: не удалось загрузить за ${Math.round((Date.now() - startedAt) / 1000)} с (${detail})`);
}
export function resolveBrowserMediaUrl(accessUrl: string): string {
if (typeof window === 'undefined') return accessUrl;
try {
const url = new URL(accessUrl, window.location.origin);
const streamIndex = url.pathname.indexOf(MEDIA_STREAM_PATH);
if (streamIndex === -1) {
return accessUrl;
}
const streamPath = url.pathname.slice(streamIndex);
return `${window.location.origin}${SAME_ORIGIN_API_PREFIX}${streamPath}${url.search}`;
} catch {
return accessUrl;
}
}
export async function fetchMediaBlobWithRetry(
accessUrl: string,
token: string,
@@ -21,12 +40,13 @@ export async function fetchMediaBlobWithRetry(
): Promise<Blob> {
const startedAt = Date.now();
const label = options?.label ?? 'изображение';
const mediaUrl = resolveBrowserMediaUrl(accessUrl);
let lastError: unknown;
for (let attempt = 0; attempt < MEDIA_FETCH_MAX_ATTEMPTS; attempt += 1) {
try {
await ensureApiGatewayReady();
const response = await fetch(accessUrl, {
const response = await fetch(mediaUrl, {
headers: { Authorization: `Bearer ${token}` },
cache: 'no-store'
});