SHA256
Полностью поддерживает английский язык
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
import { t } from '../i18n/index.js';
|
||||
import { importPkcs8Ed25519, signBase64 } from './crypto-utils.js';
|
||||
import { sha256HexFromArrayBuffer, validateSha256Hex } from './arweave-file-service.js';
|
||||
import { loadSessionMaterial } from './key-vault.js';
|
||||
@@ -24,14 +25,14 @@ function wsUrlToHttpBase(wsUrl = '') {
|
||||
if (parsed.protocol === 'wss:') parsed.protocol = 'https:';
|
||||
else if (parsed.protocol === 'ws:') parsed.protocol = 'http:';
|
||||
if (parsed.protocol !== 'http:' && parsed.protocol !== 'https:') {
|
||||
throw new Error('Не удалось определить HTTP-адрес сервера доступа.');
|
||||
throw new Error(t('remaining.file_errors.could_not_determine_the_access_server_http_address'));
|
||||
}
|
||||
return parsed.origin;
|
||||
}
|
||||
|
||||
function publicFileUrl(serverBase, sha256) {
|
||||
const cleanSha = String(sha256 || '').trim().toLowerCase();
|
||||
if (!validateSha256Hex(cleanSha)) throw new Error('Некорректный SHA-256 файла.');
|
||||
if (!validateSha256Hex(cleanSha)) throw new Error(t('remaining.file_errors.invalid_file_sha_256'));
|
||||
return `${String(serverBase || '').replace(/\/$/, '')}/files/${cleanSha}`;
|
||||
}
|
||||
|
||||
@@ -57,13 +58,13 @@ async function readErrorMessage(response) {
|
||||
async function createUploadContext({ login, sessionId, wsUrl }) {
|
||||
const cleanLogin = String(login || '').trim();
|
||||
const cleanSessionId = String(sessionId || '').trim();
|
||||
if (!cleanLogin || !cleanSessionId) throw new Error('Нет активной пользовательской сессии.');
|
||||
if (!cleanLogin || !cleanSessionId) throw new Error(t('remaining.file_errors.no_active_user_session'));
|
||||
const sessionMaterial = await loadSessionMaterial(cleanLogin);
|
||||
if (!sessionMaterial?.sessionPrivPkcs8) {
|
||||
throw new Error('На устройстве нет session key для загрузки файла.');
|
||||
throw new Error(t('remaining.file_errors.session_key_for_uploading_files_is_missing_on_this_dev'));
|
||||
}
|
||||
if (sessionMaterial.sessionId && String(sessionMaterial.sessionId) !== cleanSessionId) {
|
||||
throw new Error('Сохранённый session key относится к другой сессии.');
|
||||
throw new Error(t('remaining.file_errors.saved_session_key_belongs_to_a_different_session'));
|
||||
}
|
||||
return {
|
||||
sessionId: cleanSessionId,
|
||||
@@ -73,16 +74,16 @@ async function createUploadContext({ login, sessionId, wsUrl }) {
|
||||
}
|
||||
|
||||
export async function uploadPublicFile({ file, login, sessionId, wsUrl, onStatus = null } = {}) {
|
||||
if (!(file instanceof File)) throw new Error('Файл не выбран.');
|
||||
if (Number(file.size || 0) <= 0) throw new Error('Нельзя загрузить пустой файл.');
|
||||
if (!(file instanceof File)) throw new Error(t('remaining.file_upload.no_file_selected'));
|
||||
if (Number(file.size || 0) <= 0) throw new Error(t('remaining.file_upload.cannot_upload_an_empty_file'));
|
||||
|
||||
onStatus?.('Считаем SHA-256…');
|
||||
onStatus?.(t('remaining.file_upload.calculating_sha_256'));
|
||||
const bytes = await file.arrayBuffer();
|
||||
const sha256 = await sha256HexFromArrayBuffer(bytes);
|
||||
const context = await createUploadContext({ login, sessionId, wsUrl });
|
||||
const url = publicFileUrl(context.serverBase, sha256);
|
||||
|
||||
onStatus?.('Проверяем, есть ли файл на сервере…');
|
||||
onStatus?.(t('remaining.file_upload.checking_whether_the_file_is_on_the_server'));
|
||||
try {
|
||||
const existing = await fetch(url, { method: 'HEAD', cache: 'no-store' });
|
||||
if (existing.ok) {
|
||||
@@ -111,7 +112,7 @@ export async function uploadPublicFile({ file, login, sessionId, wsUrl, onStatus
|
||||
timeMs,
|
||||
}));
|
||||
|
||||
onStatus?.('Загружаем на сервер доступа…');
|
||||
onStatus?.(t('remaining.file_upload.uploading_to_access_server'));
|
||||
let response;
|
||||
try {
|
||||
response = await fetch(url, {
|
||||
@@ -170,7 +171,7 @@ export async function resolvePublicFileUrl({
|
||||
}
|
||||
|
||||
const cleanLogin = String(authorLogin || '').trim().toLowerCase();
|
||||
if (!cleanLogin) throw new Error('Не указан автор server-вложения.');
|
||||
if (!cleanLogin) throw new Error(t('remaining.file_errors.server_attachment_author_is_missing'));
|
||||
const cacheKey = `${cleanLogin}|${String(solanaEndpoint || '').trim()}|${String(fallbackServerLogin || '').trim().toLowerCase()}`;
|
||||
const cached = accessServerCache.get(cacheKey);
|
||||
if (cached && Date.now() - cached.atMs < ACCESS_SERVER_CACHE_TTL_MS) {
|
||||
|
||||
Reference in New Issue
Block a user