diff options
Diffstat (limited to 'src/media-upload.js')
| -rw-r--r-- | src/media-upload.js | 10 |
1 files changed, 6 insertions, 4 deletions
diff --git a/src/media-upload.js b/src/media-upload.js index f851943..7f020cf 100644 --- a/src/media-upload.js +++ b/src/media-upload.js @@ -73,6 +73,8 @@ 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 // signed-in Nostr identity, while tests need no secret key. export async function uploadPublicMedia(file, signEvent, { @@ -97,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 = { @@ -116,12 +118,12 @@ 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); |
