Always use proxy for all NIP-95 uploads to avoid all endpoint issues

This commit is contained in:
Nicolas Cantu 2025-12-29 00:32:01 +01:00
parent a4820da2df
commit 0533122919

View File

@ -124,42 +124,15 @@ export async function uploadNip95Media(file: File): Promise<MediaRef> {
let lastError: Error | null = null
for (const endpoint of endpoints) {
try {
// Try direct upload first
const url = await tryUploadEndpoint(endpoint, formData, false)
// Always use proxy to avoid CORS, 405, and name resolution issues
// Pass endpoint as query parameter to proxy
const proxyUrl = `/api/nip95-upload?endpoint=${encodeURIComponent(endpoint)}`
const url = await tryUploadEndpoint(proxyUrl, formData, true)
return { url, type: mediaType }
} catch (e) {
const error = e instanceof Error ? e : new Error(String(e))
const errorMessage = error.message
// If CORS error, network error, 405 error, or name resolution error, try via proxy
const isCorsError = errorMessage.includes('CORS') || errorMessage.includes('Failed to fetch')
const isMethodNotAllowed = errorMessage.includes('405') || errorMessage.includes('Method Not Allowed')
const isNameResolutionError = errorMessage.includes('ERR_NAME_NOT_RESOLVED') || errorMessage.includes('getaddrinfo')
const shouldUseProxy = isCorsError || isMethodNotAllowed || isNameResolutionError
if (shouldUseProxy) {
try {
console.log('Trying upload via proxy due to error:', endpoint, {
CORS: isCorsError,
'Method Not Allowed': isMethodNotAllowed,
'Name Resolution': isNameResolutionError,
})
// Pass endpoint as query parameter to proxy
const proxyUrl = `/api/nip95-upload?endpoint=${encodeURIComponent(endpoint)}`
const url = await tryUploadEndpoint(proxyUrl, formData, true)
return { url, type: mediaType }
} catch (proxyError) {
console.error('NIP-95 upload proxy error:', {
endpoint,
error: proxyError instanceof Error ? proxyError.message : String(proxyError),
fileSize: file.size,
fileType: file.type,
})
lastError = proxyError instanceof Error ? proxyError : new Error(String(proxyError))
continue
}
}
console.error('NIP-95 upload endpoint error:', {
endpoint,
error: errorMessage,