Аватары: убрать инициалы при наличии txId и усилить загрузку старых файлов

This commit is contained in:
AidarKC
2026-04-26 02:39:21 +03:00
parent 4c1aeeeac8
commit df7f38bd0a
4 changed files with 84 additions and 18 deletions
@@ -103,12 +103,47 @@ async function fetchAvatarBlob({ gateway, txId }) {
if (!response.ok) {
throw new Error(`Не удалось загрузить аватар (${response.status} ${response.statusText})`);
}
const blob = await response.blob();
const type = String(blob?.type || '').toLowerCase();
if (!type.startsWith('image/')) {
throw new Error('Arweave-файл не является изображением');
const sourceBlob = await response.blob();
const sourceType = String(sourceBlob?.type || '').toLowerCase();
if (sourceType.startsWith('image/')) {
return sourceBlob;
}
return blob;
const bytes = new Uint8Array(await sourceBlob.arrayBuffer());
const detectedType = detectImageMime(bytes);
if (detectedType) {
return new Blob([bytes], { type: detectedType });
}
// Старые аватары могли быть загружены без корректного Content-Type.
// Возвращаем исходный blob, чтобы браузер попробовал декодировать сам.
return sourceBlob;
}
function detectImageMime(bytes) {
if (!(bytes instanceof Uint8Array) || bytes.length < 12) return '';
if (
bytes[0] === 0x89
&& bytes[1] === 0x50
&& bytes[2] === 0x4e
&& bytes[3] === 0x47
&& bytes[4] === 0x0d
&& bytes[5] === 0x0a
&& bytes[6] === 0x1a
&& bytes[7] === 0x0a
) return 'image/png';
if (bytes[0] === 0xff && bytes[1] === 0xd8 && bytes[2] === 0xff) return 'image/jpeg';
if (
bytes[0] === 0x52
&& bytes[1] === 0x49
&& bytes[2] === 0x46
&& bytes[3] === 0x46
&& bytes[8] === 0x57
&& bytes[9] === 0x45
&& bytes[10] === 0x42
&& bytes[11] === 0x50
) return 'image/webp';
return '';
}
async function getBlobFromCacheOrGateway({ gateway, txId }) {