SHA256
Полностью поддерживает английский язык
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
import { t, th } from '../i18n/index.js';
|
||||
import { formatNumber } from '../i18n/format.js';
|
||||
import { openMediaViewer } from '../components/media-viewer.js';
|
||||
import { buildArweaveDataUrl, sha256HexFromArrayBuffer, validateArweaveTxId, validateSha256Hex } from './arweave-file-service.js';
|
||||
import { resolvePublicFileUrl } from './public-file-service.js';
|
||||
@@ -20,13 +22,13 @@ export function escapeHtml(text) {
|
||||
// Размер файла по-русски: «83 Б», «93 КБ», «1,4 МБ»; после 10 – без дробной части.
|
||||
export function formatBytes(bytes) {
|
||||
const value = Number(bytes || 0);
|
||||
if (!Number.isFinite(value) || value <= 0) return '0 Б';
|
||||
for (const [unit, size] of [['ГБ', 1024 ** 3], ['МБ', 1024 ** 2], ['КБ', 1024]]) {
|
||||
if (!Number.isFinite(value) || value <= 0) return t('remaining.attachments.text_0_b');
|
||||
for (const [unit, size] of [[t('remaining.attachments.gb'), 1024 ** 3], [t('remaining.attachments.mb'), 1024 ** 2], [t('remaining.attachments.kb'), 1024]]) {
|
||||
if (value < size) continue;
|
||||
const amount = value / size;
|
||||
return `${amount.toLocaleString('ru-RU', { maximumFractionDigits: amount >= 10 ? 0 : 1 })} ${unit}`;
|
||||
return `${formatNumber(amount, { maximumFractionDigits: amount >= 10 ? 0 : 1 })} ${unit}`;
|
||||
}
|
||||
return `${Math.round(value)} Б`;
|
||||
return t('remaining.attachments.bytes', { count: Math.round(value) });
|
||||
}
|
||||
|
||||
function normalizeName(name) {
|
||||
@@ -42,9 +44,9 @@ export function normalizeAttachment(input = {}) {
|
||||
const sha256Hex = String(input.sha256 || input.sha256Hex || '').trim().toLowerCase();
|
||||
const size = Number(input.sz || input.size || input.sizeBytes || 0);
|
||||
const name = normalizeName(input.nm || input.name || input.fileName || 'file');
|
||||
if (txId && !validateArweaveTxId(txId)) throw new Error('Некорректный Transaction ID Arweave.');
|
||||
if (!validateSha256Hex(sha256Hex)) throw new Error('Некорректный SHA-256 файла.');
|
||||
if (!Number.isInteger(size) || size <= 0) throw new Error('Некорректный размер файла.');
|
||||
if (txId && !validateArweaveTxId(txId)) throw new Error(t('remaining.attachment_errors.invalid_arweave_transaction_id'));
|
||||
if (!validateSha256Hex(sha256Hex)) throw new Error(t('remaining.attachment_errors.invalid_file_sha_256'));
|
||||
if (!Number.isInteger(size) || size <= 0) throw new Error(t('remaining.attachment_errors.invalid_file_size'));
|
||||
const out = {
|
||||
v: 1,
|
||||
name,
|
||||
@@ -149,11 +151,11 @@ function createUnavailableBlock(messageTimestampMs, storage = 'arweave', detail
|
||||
const block = document.createElement('div');
|
||||
block.className = 'message-attachment-unavailable';
|
||||
if (storage === 'server') {
|
||||
block.textContent = 'Файл недоступен на сервере доступа автора.';
|
||||
block.textContent = t('remaining.attachments.the_file_is_unavailable_on_the_author_s_access_server');
|
||||
} else {
|
||||
block.textContent = isRecentMessage(messageTimestampMs)
|
||||
? 'Скорее всего файл недоступен, потому что сообщение было создано недавно и файл ещё не распространился в Arweave. Попробуйте повторить через несколько минут.'
|
||||
: 'Файл недоступен.';
|
||||
? t('remaining.attachments.the_file_may_not_have_propagated_to_arweave_yet_please_try')
|
||||
: t('remaining.attachments.file_unavailable');
|
||||
}
|
||||
if (detail) {
|
||||
const debug = document.createElement('pre');
|
||||
@@ -192,9 +194,9 @@ function createFileCard({ item, url, messageTimestampMs, slide }) {
|
||||
card.className = 'message-attachment-file-card';
|
||||
card.role = 'button';
|
||||
card.tabIndex = 0;
|
||||
card.title = 'Скачать файл';
|
||||
card.title = t('remaining.attachments.download_file');
|
||||
card.innerHTML = `
|
||||
<div class="message-attachment-file-ext">Файл</div>
|
||||
<div class="message-attachment-file-ext">${th('remaining.html_attachment_format.file_div')}
|
||||
<div class="message-attachment-file-main">
|
||||
<div class="message-attachment-name">${escapeHtml(item.name)}</div>
|
||||
<div class="message-attachment-meta">${escapeHtml(formatBytes(item.size))}</div>
|
||||
@@ -234,7 +236,7 @@ function createMediaSlide({ item, url, kind, messageTimestampMs, slide, onOpen }
|
||||
frame.className = `message-attachment-media-frame message-attachment-media-frame--${kind}`;
|
||||
frame.role = 'button';
|
||||
frame.tabIndex = 0;
|
||||
frame.title = kind === 'video' ? 'Открыть видео' : 'Открыть изображение';
|
||||
frame.title = kind === 'video' ? t('remaining.attachments.open_video') : t('remaining.attachments.open_image');
|
||||
frame.addEventListener('click', (event) => {
|
||||
event.stopPropagation();
|
||||
onOpen?.();
|
||||
@@ -377,7 +379,7 @@ async function resolveAttachmentDisplayUrl(item, options = {}) {
|
||||
if (!response.ok) throw new Error(`HTTP ${response.status} ${serverUrl}`);
|
||||
const buffer = await response.arrayBuffer();
|
||||
const actualSha = await sha256HexFromArrayBuffer(buffer);
|
||||
if (actualSha !== item.sha256) throw new Error(`SHA-256 скачанного файла не совпал с блокчейном: ${actualSha} != ${item.sha256} (${serverUrl})`);
|
||||
if (actualSha !== item.sha256) throw new Error(t('remaining.attachment_error.hash_mismatch', { actual: actualSha, expected: item.sha256, url: serverUrl }));
|
||||
const blob = new Blob([buffer], { type: mimeTypeForAttachment(item, kind) });
|
||||
return { url: URL.createObjectURL(blob), sourceUrl: serverUrl, objectUrl: true };
|
||||
}
|
||||
@@ -423,12 +425,12 @@ export function createAttachmentCarouselElement(attachments = [], {
|
||||
prev.type = 'button';
|
||||
prev.className = 'ui-button message-attachment-arrow message-attachment-arrow--prev';
|
||||
prev.textContent = '‹';
|
||||
prev.setAttribute('aria-label', 'Предыдущее вложение');
|
||||
prev.setAttribute('aria-label', t('remaining.attachments.previous_attachment'));
|
||||
const next = document.createElement('button');
|
||||
next.type = 'button';
|
||||
next.className = 'ui-button message-attachment-arrow message-attachment-arrow--next';
|
||||
next.textContent = '›';
|
||||
next.setAttribute('aria-label', 'Следующее вложение');
|
||||
next.setAttribute('aria-label', t('remaining.attachments.next_attachment'));
|
||||
const counter = document.createElement('div');
|
||||
counter.className = 'message-attachment-counter';
|
||||
wrap.classList.toggle('has-single-attachment', items.length <= 1);
|
||||
@@ -478,7 +480,7 @@ export function createAttachmentCarouselElement(attachments = [], {
|
||||
|
||||
const loading = document.createElement('div');
|
||||
loading.className = 'message-attachment-unavailable';
|
||||
loading.textContent = 'Загружаем файл…';
|
||||
loading.textContent = t('remaining.attachments.loading_file');
|
||||
slide.append(loading);
|
||||
|
||||
void getResolved(currentIndex)
|
||||
@@ -501,7 +503,7 @@ export function createAttachmentCarouselElement(attachments = [], {
|
||||
prev.hidden = items.length <= 1 || index <= 0;
|
||||
next.hidden = items.length <= 1 || index >= items.length - 1;
|
||||
counter.hidden = items.length <= 1;
|
||||
counter.textContent = items.length > 1 ? `${index + 1} из ${items.length}` : '';
|
||||
counter.textContent = items.length > 1 ? t('remaining.attachments.counter', { current: index + 1, total: items.length }) : '';
|
||||
};
|
||||
|
||||
const move = (delta) => {
|
||||
|
||||
Reference in New Issue
Block a user