mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-29 20:34:28 +00:00
fix(webui): show clear toast when upload fails due to localhost s3 endpoint (#507)
* fix(webui): show clear toast when upload fails due to localhost s3 endpoint * fix(webui): center empty state in file browser list area
This commit is contained in:
@@ -5,6 +5,7 @@ import type { PresignedUrl } from '@shumai/dtos'
|
||||
import { client } from '@/ui/api/client'
|
||||
import { useUploadStore } from '@/ui/stores/upload'
|
||||
import { toast } from 'sonner'
|
||||
import { m } from '@/ui/paraglide/messages.js'
|
||||
import { acquireWakeLock, releaseWakeLock } from './wake-lock'
|
||||
|
||||
export type FileWithId = {
|
||||
@@ -18,6 +19,7 @@ export interface ActiveUploadFile {
|
||||
fileId: string
|
||||
key?: string
|
||||
uploadId?: string
|
||||
url?: string
|
||||
}
|
||||
|
||||
export const activeUploads = new Map<string, ActiveUploadFile>()
|
||||
@@ -63,6 +65,21 @@ if (typeof window !== 'undefined') {
|
||||
})
|
||||
}
|
||||
|
||||
const LOCALHOST_HOSTS = new Set(['localhost', '127.0.0.1', '::1', '[::1]', '0.0.0.0'])
|
||||
|
||||
export function isLocalhostStorageMismatch(uploadUrlString?: string): boolean {
|
||||
if (!uploadUrlString || typeof window === 'undefined') return false
|
||||
try {
|
||||
const uploadUrl = new URL(uploadUrlString, window.location.origin)
|
||||
const isUploadLocalhost = LOCALHOST_HOSTS.has(uploadUrl.hostname)
|
||||
const isWindowLocalhost = LOCALHOST_HOSTS.has(window.location.hostname)
|
||||
|
||||
return isUploadLocalhost && !isWindowLocalhost
|
||||
} catch {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
export function isNetworkIssue(error?: unknown): boolean {
|
||||
if (typeof navigator !== 'undefined' && !navigator.onLine) {
|
||||
return true
|
||||
@@ -76,7 +93,7 @@ export function isNetworkIssue(error?: unknown): boolean {
|
||||
msg.includes('network error') ||
|
||||
msg.includes('failed to fetch') ||
|
||||
msg.includes('networkerror') ||
|
||||
msg.includes('load failed') ||
|
||||
(msg.includes('load failed') && !msg.includes('upload failed')) ||
|
||||
msg.includes('err_internet_disconnected')
|
||||
) {
|
||||
return true
|
||||
@@ -171,6 +188,12 @@ export async function uploadFilesWithUppy({
|
||||
}
|
||||
|
||||
const data = (await res.json()) as { url: string }
|
||||
if (fileId) {
|
||||
const active = activeUploads.get(fileId)
|
||||
if (active) {
|
||||
active.url = data.url
|
||||
}
|
||||
}
|
||||
return { url: data.url }
|
||||
},
|
||||
})
|
||||
@@ -268,7 +291,7 @@ export async function uploadFilesWithUppy({
|
||||
console.error('Failed to confirm upload:', err)
|
||||
failFile(taskId, fileId)
|
||||
decrement()
|
||||
toast.error(`Failed to confirm upload: ${file.name}`)
|
||||
toast.error(m.failed_confirm_upload_with_name({ name: file.name }))
|
||||
await onFileFinished?.(fileId)
|
||||
throw err
|
||||
}
|
||||
@@ -283,11 +306,21 @@ export async function uploadFilesWithUppy({
|
||||
) => {
|
||||
const fileId = file.meta.fileId as string
|
||||
if (!activeUploads.has(fileId)) return
|
||||
const active = activeUploads.get(fileId)
|
||||
const uploadUrl = (file.meta?.url as string | undefined) || active?.url
|
||||
|
||||
activeUploads.delete(fileId)
|
||||
failFile(taskId, fileId)
|
||||
decrement()
|
||||
|
||||
toast.error(`Failed to upload file: ${file.name}`)
|
||||
if (isLocalhostStorageMismatch(uploadUrl)) {
|
||||
toast.error(m.upload_failed_s3_endpoint_localhost(), {
|
||||
id: 's3-endpoint-localhost-mismatch',
|
||||
duration: 10000,
|
||||
})
|
||||
} else {
|
||||
toast.error(m.failed_upload_file_with_name({ name: file.name }))
|
||||
}
|
||||
|
||||
const errorPromise = (async () => {
|
||||
try {
|
||||
@@ -295,7 +328,8 @@ export async function uploadFilesWithUppy({
|
||||
param: { teamId, taskId },
|
||||
json: {
|
||||
fileId,
|
||||
errorMessage: (error as Error)?.message || 'Upload failed',
|
||||
errorMessage:
|
||||
(error as Error)?.message || (typeof error === 'string' ? error : 'Upload failed'),
|
||||
},
|
||||
})
|
||||
} catch (err) {
|
||||
@@ -311,6 +345,19 @@ export async function uploadFilesWithUppy({
|
||||
if (!file) return
|
||||
console.error(`[Uploader] upload-error for ${file.name}:`, error)
|
||||
|
||||
const fileId = file.meta?.fileId as string | undefined
|
||||
const uploadUrl =
|
||||
(file.meta?.url as string | undefined) ||
|
||||
(fileId ? activeUploads.get(fileId)?.url : undefined)
|
||||
|
||||
if (isLocalhostStorageMismatch(uploadUrl)) {
|
||||
console.warn(
|
||||
`[Uploader] Localhost storage endpoint mismatch detected for ${file.name}. Failing permanently without retry.`,
|
||||
)
|
||||
handlePermanentError(file, error)
|
||||
return
|
||||
}
|
||||
|
||||
if (isNetworkIssue(error)) {
|
||||
console.warn(
|
||||
`[Uploader] Network error detected for ${file.name}. Keeping upload active to resume when connection is restored.`,
|
||||
@@ -357,6 +404,7 @@ export async function uploadFilesWithUppy({
|
||||
taskId,
|
||||
fileId,
|
||||
key,
|
||||
url,
|
||||
})
|
||||
|
||||
increment()
|
||||
@@ -385,7 +433,19 @@ export async function uploadFilesWithUppy({
|
||||
const failedFiles = result?.failed || []
|
||||
const isOffline = typeof navigator !== 'undefined' && !navigator.onLine
|
||||
const hasNetworkFailures =
|
||||
isOffline || failedFiles.some((f) => isNetworkIssue((f as { error?: unknown })?.error))
|
||||
isOffline ||
|
||||
failedFiles.some((f) => {
|
||||
const err = (f as { error?: unknown })?.error
|
||||
const fileMeta = (f as { meta?: Record<string, unknown> })?.meta
|
||||
const fId = fileMeta?.fileId as string | undefined
|
||||
const uUrl =
|
||||
(fileMeta?.url as string | undefined) ||
|
||||
(fId ? activeUploads.get(fId)?.url : undefined)
|
||||
if (isLocalhostStorageMismatch(uUrl)) {
|
||||
return false
|
||||
}
|
||||
return isNetworkIssue(err)
|
||||
})
|
||||
|
||||
if (hasNetworkFailures) {
|
||||
console.log(
|
||||
|
||||
Reference in New Issue
Block a user