Files
IM-web-new/src/services/mediaProxy.js
T

57 lines
1.8 KiB
JavaScript

function configuredProxyHosts() {
const configured = window.JC_WEB_IM_CONFIG?.mediaProxyHosts;
const values = Array.isArray(configured)
? configured
: String(configured || '').split(/[\s,]+/);
return new Set(
['zhiyanim.dfpnm.cn', ...values]
.map(value => String(value || '').trim().toLowerCase().replace(/\.$/, ''))
.filter(Boolean)
);
}
export function imageDisplayUrl(value) {
const source = String(value || '').trim();
if (!source || source.startsWith('blob:') || source.startsWith('data:')) {
return source;
}
try {
const url = new URL(source, window.location.origin);
if (url.protocol !== 'https:' || !configuredProxyHosts().has(url.hostname.toLowerCase())) {
return source;
}
const proxyUrl = new URL('/api/media_proxy.php', window.location.origin);
proxyUrl.searchParams.set('url', url.toString());
return proxyUrl.toString();
} catch (error) {
return source;
}
}
export function videoDisplayUrl(value) {
const source = String(value || '').trim();
if (!source || source.startsWith('blob:') || source.startsWith('data:')) return source;
const streamUrl = new URL('/api/media_stream.php', window.location.origin);
streamUrl.searchParams.set('url', source);
return streamUrl.toString();
}
export function useVideoOriginalFallback(event, value) {
const video = event?.currentTarget;
const source = String(value || '').trim();
if (!video || !source || video.dataset.originalFallback === '1') return;
video.dataset.originalFallback = '1';
video.src = source;
video.load();
}
export function useImageProxyFallback(event, value) {
const image = event?.currentTarget;
if (!image) return;
const fallback = imageDisplayUrl(value);
if (!fallback || fallback === value || image.dataset.proxyFallback === '1') return;
image.dataset.proxyFallback = '1';
image.src = fallback;
}