Always use proxy for all NIP-95 uploads to avoid all endpoint issues
This commit is contained in:
parent
a4820da2df
commit
0533122919
35
lib/nip95.ts
35
lib/nip95.ts
@ -124,41 +124,14 @@ export async function uploadNip95Media(file: File): Promise<MediaRef> {
|
|||||||
let lastError: Error | null = null
|
let lastError: Error | null = null
|
||||||
for (const endpoint of endpoints) {
|
for (const endpoint of endpoints) {
|
||||||
try {
|
try {
|
||||||
// Try direct upload first
|
// Always use proxy to avoid CORS, 405, and name resolution issues
|
||||||
const url = await tryUploadEndpoint(endpoint, formData, false)
|
|
||||||
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
|
// Pass endpoint as query parameter to proxy
|
||||||
const proxyUrl = `/api/nip95-upload?endpoint=${encodeURIComponent(endpoint)}`
|
const proxyUrl = `/api/nip95-upload?endpoint=${encodeURIComponent(endpoint)}`
|
||||||
const url = await tryUploadEndpoint(proxyUrl, formData, true)
|
const url = await tryUploadEndpoint(proxyUrl, formData, true)
|
||||||
return { url, type: mediaType }
|
return { url, type: mediaType }
|
||||||
} catch (proxyError) {
|
} catch (e) {
|
||||||
console.error('NIP-95 upload proxy error:', {
|
const error = e instanceof Error ? e : new Error(String(e))
|
||||||
endpoint,
|
const errorMessage = error.message
|
||||||
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:', {
|
console.error('NIP-95 upload endpoint error:', {
|
||||||
endpoint,
|
endpoint,
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user