SHA256
WIP: добавить MVP загрузки вложений в Cloudflare R2
Работа ещё не готова: предпросмотр публичных R2-вложений в UI пока не завершён.
This commit is contained in:
@@ -42,27 +42,25 @@ function normalizeName(name) {
|
||||
export function normalizeAttachment(input = {}) {
|
||||
const txId = String(input.ar || input.txId || '').trim();
|
||||
const sha256Hex = String(input.sha256 || input.sha256Hex || '').trim().toLowerCase();
|
||||
const pr = String(input.pr || '').trim();
|
||||
const mm = String(input.mm || '').trim();
|
||||
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(t('remaining.attachment_errors.invalid_arweave_transaction_id'));
|
||||
if (!validateSha256Hex(sha256Hex)) throw new Error(t('remaining.attachment_errors.invalid_file_sha_256'));
|
||||
if (!/^[A-Za-z0-9_-]{43}$/.test(pr) && !validateSha256Hex(sha256Hex)) throw new Error('Некорректный идентификатор файла');
|
||||
if (pr && !/^[A-Za-z0-9_-]{43}$/.test(pr)) throw new Error('Некорректный Pieces Root');
|
||||
if (mm && (!/^[a-z0-9!#$&^_.+-]+\/[a-z0-9!#$&^_.+-]+$/i.test(mm) || mm.length > 127)) throw new Error('Некорректный MIME');
|
||||
if (!Number.isInteger(size) || size <= 0) throw new Error(t('remaining.attachment_errors.invalid_file_size'));
|
||||
const out = {
|
||||
v: 1,
|
||||
name,
|
||||
size,
|
||||
sha256: sha256Hex,
|
||||
ar: txId,
|
||||
uploadedAtMs: Number(input.uploadedAtMs || 0) || Date.now(),
|
||||
};
|
||||
return out;
|
||||
return { v: 1, name, size, sha256: sha256Hex, pr, mm, ar: txId, uploadedAtMs: Number(input.uploadedAtMs || 0) || Date.now() };
|
||||
}
|
||||
|
||||
export function buildAttachmentBlock(attachment) {
|
||||
const item = normalizeAttachment(attachment);
|
||||
const encodedName = encodeURIComponent(item.name);
|
||||
const id = item.pr ? `;pr=${item.pr}` : `;sha256=${item.sha256}`;
|
||||
const mmPart = item.mm ? `;mm=${item.mm}` : '';
|
||||
const arPart = item.ar ? `;ar=${item.ar}` : '';
|
||||
return `<S:att;v=1;nm=${encodedName};sz=${item.size};sha256=${item.sha256}${arPart}>`;
|
||||
return `<S:att;v=1;nm=${encodedName};sz=${item.size}${id}${mmPart}${arPart}>`;
|
||||
}
|
||||
|
||||
export function composeMessageWithAttachments(text, attachments = []) {
|
||||
@@ -103,6 +101,8 @@ export function parseMessageAttachments(rawText) {
|
||||
nm: decodeURIComponent(String(fields.nm || fields.name || 'file')),
|
||||
sz: fields.sz || fields.size,
|
||||
sha256: fields.sha256,
|
||||
pr: fields.pr,
|
||||
mm: fields.mm,
|
||||
ar: fields.ar,
|
||||
}));
|
||||
} catch {
|
||||
@@ -365,23 +365,14 @@ async function resolveAttachmentDisplayUrl(item, options = {}) {
|
||||
|
||||
const serverUrl = await resolvePublicFileUrl({
|
||||
sha256: item.sha256,
|
||||
pr: item.pr,
|
||||
authorLogin: options.authorLogin,
|
||||
solanaEndpoint: options.solanaEndpoint,
|
||||
fallbackServerLogin: options.fallbackServerLogin,
|
||||
});
|
||||
const kind = getAttachmentKind(item);
|
||||
// Видео подключаем напрямую. Иначе fetch + Blob + полный SHA-256 сначала
|
||||
// скачает ВСЕ байты и браузер не сможет запрашивать Range для просмотра.
|
||||
if (kind === 'video' || kind === 'file') return { url: serverUrl, objectUrl: false };
|
||||
|
||||
// Для изображений сохраняем прежнюю проверку SHA-256 после скачивания.
|
||||
const response = await fetch(serverUrl);
|
||||
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(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 };
|
||||
if (item.pr) return { url: serverUrl, objectUrl: false };
|
||||
// Public MVP: direct URL, no image downloads, media viewer or full SHA checks.
|
||||
return { url: serverUrl, objectUrl: false };
|
||||
}
|
||||
|
||||
export function createAttachmentCarouselElement(attachments = [], {
|
||||
@@ -410,6 +401,23 @@ export function createAttachmentCarouselElement(attachments = [], {
|
||||
wrap.addEventListener('click', (event) => event.stopPropagation());
|
||||
if (!items.length) return wrap;
|
||||
|
||||
// Public attachments are a simple file list. The DM renderer supplies resolveUrl
|
||||
// and retains its separate encrypted-file carousel/viewer unchanged.
|
||||
if (!resolveUrl) {
|
||||
wrap.className = 'message-attachments message-attachment-list';
|
||||
const options = { gateway, authorLogin, solanaEndpoint, fallbackServerLogin };
|
||||
for (const item of items) {
|
||||
const slot = document.createElement('div');
|
||||
slot.className = 'message-attachment-item';
|
||||
slot.textContent = t('remaining.attachments.loading_file');
|
||||
wrap.append(slot);
|
||||
void resolveAttachmentDisplayUrl(item, options)
|
||||
.then(({ url }) => slot.replaceChildren(createFileCard({ item, url, messageTimestampMs, slide: slot })))
|
||||
.catch(() => slot.replaceChildren(createUnavailableBlock(messageTimestampMs, attachmentStorage(item))));
|
||||
}
|
||||
return wrap;
|
||||
}
|
||||
|
||||
let index = 0;
|
||||
let pointerStartX = 0;
|
||||
let renderGeneration = 0;
|
||||
@@ -486,9 +494,10 @@ export function createAttachmentCarouselElement(attachments = [], {
|
||||
void getResolved(currentIndex)
|
||||
.then(({ url }) => {
|
||||
if (generation !== renderGeneration || currentIndex !== index || !wrap.isConnected) return;
|
||||
slide.replaceChildren(kind === 'file'
|
||||
slide.replaceChildren(!resolveUrl
|
||||
? createFileCard({ item, url, messageTimestampMs, slide })
|
||||
: createMediaSlide({ item, url, kind, messageTimestampMs, slide, onOpen: () => openViewer(currentIndex) }));
|
||||
: (kind === 'file' ? createFileCard({ item, url, messageTimestampMs, slide })
|
||||
: createMediaSlide({ item, url, kind, messageTimestampMs, slide, onOpen: () => openViewer(currentIndex) })));
|
||||
})
|
||||
.catch((error) => {
|
||||
if (generation !== renderGeneration || currentIndex !== index || !wrap.isConnected) return;
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
import { bytesToBase64Url } from './crypto-utils.js';
|
||||
import { computeTorrentV2PieceRoot, TorrentV2PieceAccumulator, TORRENT_V2_PIECE_BYTES } from './torrent-v2-service.js';
|
||||
|
||||
export const R2_MAX_FILE_BYTES = 500 * 1024 * 1024;
|
||||
const mimeByExtension = { mp4: 'video/mp4', webm: 'video/webm', jpg: 'image/jpeg', jpeg: 'image/jpeg', png: 'image/png', webp: 'image/webp', gif: 'image/gif', pdf: 'application/pdf', mp3: 'audio/mpeg', zip: 'application/zip', txt: 'text/plain' };
|
||||
export function fileMime(file) {
|
||||
const extension = String(file?.name || '').toLowerCase().split('.').pop();
|
||||
const value = String(file?.type || mimeByExtension[extension] || 'application/octet-stream').trim();
|
||||
return /^[a-z0-9!#$&^_.+-]+\/[a-z0-9!#$&^_.+-]+$/i.test(value) && value.length <= 127 ? value : 'application/octet-stream';
|
||||
}
|
||||
export async function preparePublicFile(file, { onProgress = null } = {}) {
|
||||
if (!(file instanceof Blob) || !file.size) throw new Error('Выберите непустой файл');
|
||||
const accumulator = new TorrentV2PieceAccumulator();
|
||||
for (let offset = 0; offset < file.size; offset += TORRENT_V2_PIECE_BYTES) {
|
||||
const bytes = new Uint8Array(await file.slice(offset, offset + TORRENT_V2_PIECE_BYTES).arrayBuffer());
|
||||
const piece = await computeTorrentV2PieceRoot(bytes, { padToPieceLength: file.size > TORRENT_V2_PIECE_BYTES });
|
||||
await accumulator.addPieceRoot(piece);
|
||||
onProgress?.(Math.min(file.size, offset + bytes.byteLength), file.size);
|
||||
}
|
||||
const root = await accumulator.finalize();
|
||||
return { file, name: String(file.name || 'file'), size: file.size, mm: fileMime(file), pr: bytesToBase64Url(root) };
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
import { t } from '../i18n/index.js';
|
||||
import { preparePublicFile, R2_MAX_FILE_BYTES } from './file-preparation-service.js';
|
||||
import { importPkcs8Ed25519, signBase64 } from './crypto-utils.js';
|
||||
import { sha256HexFromArrayBuffer, validateSha256Hex } from './arweave-file-service.js';
|
||||
import { loadSessionMaterial } from './key-vault.js';
|
||||
@@ -73,7 +74,8 @@ async function createUploadContext({ login, sessionId, wsUrl }) {
|
||||
};
|
||||
}
|
||||
|
||||
export async function uploadPublicFile({ file, login, sessionId, wsUrl, onStatus = null } = {}) {
|
||||
export async function uploadPublicFile({ file, login, sessionId, wsUrl, onStatus = null, purpose = 'attachment' } = {}) {
|
||||
if (purpose !== 'avatar') return uploadR2PublicFile({ file, login, sessionId, wsUrl, onStatus });
|
||||
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'));
|
||||
|
||||
@@ -155,11 +157,13 @@ export async function uploadPublicFile({ file, login, sessionId, wsUrl, onStatus
|
||||
}
|
||||
|
||||
export async function resolvePublicFileUrl({
|
||||
pr,
|
||||
sha256,
|
||||
authorLogin,
|
||||
solanaEndpoint,
|
||||
fallbackServerLogin = '',
|
||||
} = {}) {
|
||||
if (pr) return publicR2Url(pr);
|
||||
// Сначала – сервер, к которому подключены сейчас (у большинства читателей он же сервер автора),
|
||||
// затем сервер сборки по умолчанию; только потом ищем сервер автора через его PDA.
|
||||
const nearbyBases = [...new Set([state.entrySettings?.shineServerHttp, DEFAULT_SHINE_SERVER_HTTP]
|
||||
@@ -185,3 +189,50 @@ export async function resolvePublicFileUrl({
|
||||
accessServerCache.set(cacheKey, { atMs: Date.now(), httpBase: resolved.httpBase });
|
||||
return publicFileUrl(resolved.httpBase, sha256);
|
||||
}
|
||||
|
||||
/** Uploads one public attachment directly from the browser to Cloudflare R2. */
|
||||
async function uploadR2PublicFile({ file, login, sessionId, wsUrl, onStatus }) {
|
||||
if (!(file instanceof File) || file.size < 1) throw new Error('Выберите непустой файл');
|
||||
if (file.size > R2_MAX_FILE_BYTES) throw new Error('Превышен лимит 500 МиБ');
|
||||
onStatus?.('Подготовка файла…');
|
||||
const prepared = await preparePublicFile(file);
|
||||
const context = await createUploadContext({ login, sessionId, wsUrl });
|
||||
const timeMs = Date.now();
|
||||
const preimage = `PUBLIC_R2_PREPARE_V1:${context.sessionId}:${prepared.pr}:${prepared.size}:${prepared.mm}:${timeMs}`;
|
||||
const signature = await signBase64(context.privateKey, preimage);
|
||||
onStatus?.('Проверка файла в R2…');
|
||||
const response = await fetch(`${context.serverBase}/api/files/prepare`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'X-Shine-Session-Id': context.sessionId,
|
||||
'X-Shine-Time-Ms': String(timeMs),
|
||||
'X-Shine-Signature': signature,
|
||||
},
|
||||
body: JSON.stringify({ pr: prepared.pr, size: prepared.size, mm: prepared.mm }),
|
||||
});
|
||||
if (!response.ok) throw new Error((await readErrorMessage(response)) || `R2 prepare HTTP ${response.status}`);
|
||||
const payload = await response.json();
|
||||
if (!payload.exists) {
|
||||
if (!payload.uploadUrl) throw new Error('Сервер не предоставил адрес загрузки');
|
||||
onStatus?.('Загрузка в Cloudflare R2…');
|
||||
const put = await fetch(payload.uploadUrl, {
|
||||
method: 'PUT',
|
||||
headers: payload.requiredHeaders || { 'Content-Type': prepared.mm, 'If-None-Match': '*' },
|
||||
body: file,
|
||||
});
|
||||
if (!put.ok && put.status !== 412) throw new Error(`Cloudflare R2: HTTP ${put.status}`);
|
||||
if (put.status === 412) {
|
||||
const check = await fetch(payload.fileUrl, { method: 'HEAD', cache: 'no-store' });
|
||||
if (!check.ok || Number(check.headers.get('Content-Length')) !== file.size) throw new Error('Конфликт при загрузке объекта R2');
|
||||
}
|
||||
}
|
||||
return { v: 1, name: prepared.name, size: prepared.size, pr: prepared.pr, mm: prepared.mm,
|
||||
ar: '', uploadedAtMs: Date.now(), alreadyExists: Boolean(payload.exists) };
|
||||
}
|
||||
|
||||
export function publicR2Url(pr) {
|
||||
const root = String(pr || '').trim();
|
||||
if (!/^[A-Za-z0-9_-]{43}$/.test(root)) throw new Error('Некорректный Pieces Root');
|
||||
return `https://files.shineup.me/${root}`;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user