Обновить публичные идентификаторы файлов

This commit is contained in:
AidarKC
2026-11-15 15:45:33 +03:00
parent ac6a2e5b3f
commit 4a6e072946
65 changed files with 292 additions and 457 deletions
@@ -1,10 +1,6 @@
import { t } from '../i18n/index.js';
import {
buildArweaveDataUrl,
sha256HexFromArrayBuffer,
validateArweaveTxId,
validateSha256Hex,
} from './arweave-file-service.js';
import { buildArweaveDataUrl, validateArweaveTxId } from './arweave-file-service.js';
import { preparePublicFile } from './file-preparation-service.js';
import { validatePublicPiecesRoot } from './public-pieces-root.js';
const DB_NAME = 'shine-ui-avatar-cache';
const DB_VERSION = 1;
@@ -61,12 +57,9 @@ async function putRecord(record) {
});
}
async function verifyBlobSha256(blob, expectedSha256Hex) {
const expected = String(expectedSha256Hex || '').trim().toLowerCase();
if (!validateSha256Hex(expected)) return true;
if (!(blob instanceof Blob)) return false;
const actual = await sha256HexFromArrayBuffer(await blob.arrayBuffer());
return actual === expected;
async function verifyBlobPr(blob, expectedPr) {
if (!validatePublicPiecesRoot(expectedPr) || !(blob instanceof Blob)) return false;
return (await preparePublicFile(blob)).pr === expectedPr;
}
async function getAllRecords() {
@@ -160,62 +153,30 @@ function detectImageMime(bytes) {
return '';
}
async function getBlobFromCacheOrGateway({ gateway, txId, expectedSha256Hex = '' }) {
const expected = String(expectedSha256Hex || '').trim().toLowerCase();
async function getBlobFromCacheOrGateway({ gateway, txId, expectedPr }) {
if (!validatePublicPiecesRoot(expectedPr)) throw new Error('Некорректный PR аватара');
try {
const cached = await getRecord(txId);
if (cached?.blob instanceof Blob) {
if (!validateSha256Hex(expected)) return cached.blob;
const cacheSha = String(cached?.sha256Hex || '').trim().toLowerCase();
if (cacheSha && cacheSha === expected) {
return cached.blob;
}
const ok = await verifyBlobSha256(cached.blob, expected);
if (ok) {
return cached.blob;
}
// кэш повреждён или не совпадает с ожидаемым хэшем: удаляем и перекачиваем
if (await verifyBlobPr(cached.blob, expectedPr)) return cached.blob;
await deleteRecords([txId]);
}
} catch {
// ignore IndexedDB errors and fallback to fetch
}
} catch { /* cache is optional */ }
const blob = await fetchAvatarBlob({ gateway, txId });
if (validateSha256Hex(expected)) {
const ok = await verifyBlobSha256(blob, expected);
if (!ok) {
throw new Error('SHA256_MISMATCH');
}
}
const computedSha256Hex = await sha256HexFromArrayBuffer(await blob.arrayBuffer());
const record = {
txId,
blob,
contentType: String(blob.type || 'application/octet-stream'),
sizeBytes: Number(blob.size || 0),
sha256Hex: computedSha256Hex,
cachedAtMs: Date.now(),
};
if (!(await verifyBlobPr(blob, expectedPr))) throw new Error('AVATAR_PR_MISMATCH');
try {
await putRecord(record);
await putRecord({ txId, blob, contentType: String(blob.type || 'application/octet-stream'),
sizeBytes: Number(blob.size || 0), pr: expectedPr, cachedAtMs: Date.now() });
await ensureCacheLimits();
} catch {
// ignore cache write errors
}
} catch { /* cache is optional */ }
return blob;
}
export async function getCachedAvatarObjectUrl({ gateway, txId, expectedSha256Hex = '' }) {
export async function getCachedAvatarObjectUrl({ gateway, txId, expectedPr }) {
const cleanTxId = String(txId || '').trim();
if (!validateArweaveTxId(cleanTxId)) {
throw new Error(t('remaining.avatar_errors.invalid_arweave_transaction_id'));
}
const blob = await getBlobFromCacheOrGateway({
gateway,
txId: cleanTxId,
expectedSha256Hex,
});
if (!validateArweaveTxId(cleanTxId)) throw new Error(t('remaining.avatar_errors.invalid_arweave_transaction_id'));
const blob = await getBlobFromCacheOrGateway({ gateway, txId: cleanTxId, expectedPr });
return URL.createObjectURL(blob);
}