fix and update
This commit is contained in:
@@ -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'
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user