diff options
Diffstat (limited to 'src/media-upload.js')
| -rw-r--r-- | src/media-upload.js | 23 |
1 files changed, 18 insertions, 5 deletions
diff --git a/src/media-upload.js b/src/media-upload.js index d7a99e1..7f020cf 100644 --- a/src/media-upload.js +++ b/src/media-upload.js @@ -20,6 +20,16 @@ export class MediaUploadError extends Error { } } +// Private Blossom hosts authorize the user's Nostr identity, which can differ +// from the wallet's sync key. Resolve once for the whole upload (including HLS). +export async function mediaUploadSigner(wallet, hook) { + const id = hook('nostrLoginIdentity'); + if (!id) return (event) => wallet.nostrSign(event); + const signer = id.signer || await hook('nostrLoginResume'); + if (!signer) throw new MediaUploadError('no-identity'); + return (event) => signer.signEvent(event); +} + async function fileHash(file) { const hash = sha256.create(); const chunk = 4 * 1024 * 1024; @@ -63,8 +73,10 @@ function putWithProgress(url, { body, headers, signal }, onProgress) { }); } +const hostOf = (server) => { try { return new URL(server).hostname.replace(/^blossom\./, ''); } catch { return server; } }; + // BUD-02 upload. `signEvent` is deliberately injected: the app can use its -// always-available wallet identity, while tests need no secret key. +// signed-in Nostr identity, while tests need no secret key. export async function uploadPublicMedia(file, signEvent, { servers = mediaServers(PUBLIC_MEDIA_SERVERS), fetcher = globalThis.fetch, @@ -87,7 +99,7 @@ export async function uploadPublicMedia(file, signEvent, { // upload timeout. Any HTTP answer, even an error, means it's there. if (i < servers.length - 1) { try { await fetcher(server.replace(/\/$/, '') + '/upload', { method: 'HEAD', signal: AbortSignal.timeout(5000) }); } - catch (error) { failures.push({ status: 0, reason: 'unreachable' }); continue; } + catch (error) { failures.push({ host: hostOf(server), status: 0, reason: 'unreachable' }); continue; } } try { const init = { @@ -106,12 +118,13 @@ export async function uploadPublicMedia(file, signEvent, { try { body = await response.json(); } catch {} const url = response.ok && responseUrl(body); if (url) return mediaUrl(url, server, file); - failures.push({ status: response.status, reason: response.headers.get('x-reason') || body?.message || '' }); + failures.push({ host: hostOf(server), status: response.status, reason: response.headers.get('x-reason') || body?.message || '' }); } catch (error) { - failures.push({ status: 0, reason: error?.message || '' }); + failures.push({ host: hostOf(server), status: 0, reason: error?.message || '' }); } } - const details = failures.map((x) => [x.status, x.reason].filter(Boolean).join(' ')).filter(Boolean).join('; '); + const details = failures.map((x) => x.host + ': ' + ([x.status, x.reason].filter(Boolean).join(' ') || '?')).join('; '); + if (failures.length && failures.every((x) => x.status === 401 || x.status === 403)) throw new MediaUploadError('unauthorized', details); if (failures.length && failures.every((x) => x.status === 413)) throw new MediaUploadError('too-big', details); if (failures.length && failures.every((x) => x.status === 415)) throw new MediaUploadError('unsupported', details); throw new MediaUploadError('failed', details); |
