78 lines
2.5 KiB
JavaScript
78 lines
2.5 KiB
JavaScript
const REASON_MAP = {
|
|
duplicate_hash: {
|
|
type: 'error',
|
|
message: 'This file already exists in Skinbase. Please upload a different file.',
|
|
},
|
|
validation_failed: {
|
|
type: 'error',
|
|
message: 'Upload validation failed. Please check the file and try again.',
|
|
},
|
|
scan_failed: {
|
|
type: 'error',
|
|
message: 'Upload scan failed. Please try another file.',
|
|
},
|
|
quota_exceeded: {
|
|
type: 'warning',
|
|
message: 'Upload limit reached. Please wait before uploading again.',
|
|
},
|
|
}
|
|
|
|
function normalizeType(value, fallback = 'error') {
|
|
const normalized = String(value || '').toLowerCase()
|
|
if (normalized === 'success' || normalized === 'warning' || normalized === 'error') return normalized
|
|
return fallback
|
|
}
|
|
|
|
export function mapUploadErrorNotice(error, fallback = 'Upload failed.') {
|
|
const status = Number(error?.response?.status || 0)
|
|
const payload = error?.response?.data || {}
|
|
const reason = String(payload?.reason || '').toLowerCase()
|
|
const mapped = REASON_MAP[reason]
|
|
const errorCode = String(error?.code || '').toUpperCase()
|
|
const rawMessage = typeof error?.message === 'string' ? error.message.trim() : ''
|
|
const timedOut = errorCode === 'ECONNABORTED' || /timeout/i.test(rawMessage)
|
|
const requestTooLarge = status === 413
|
|
|
|
const type = mapped?.type
|
|
? mapped.type
|
|
: normalizeType(payload?.type || payload?.level, requestTooLarge ? 'warning' : (status >= 500 ? 'error' : 'warning'))
|
|
|
|
const message =
|
|
(requestTooLarge ? 'Server rejected this upload chunk as too large. Retrying with smaller chunks may help, or increase Nginx/PHP upload limits.' : '') ||
|
|
(timedOut ? 'Upload request timed out before the server responded. Check Nginx/PHP-FPM body handling and try again.' : '') ||
|
|
mapped?.message ||
|
|
(typeof payload?.message === 'string' && payload.message.trim()) ||
|
|
rawMessage ||
|
|
fallback
|
|
|
|
return {
|
|
type,
|
|
message,
|
|
reason,
|
|
status,
|
|
}
|
|
}
|
|
|
|
export function mapUploadResultNotice(payload, options = {}) {
|
|
const {
|
|
fallbackType = 'success',
|
|
fallbackMessage = 'Operation completed successfully.',
|
|
} = options
|
|
|
|
const reason = String(payload?.reason || '').toLowerCase()
|
|
const mapped = REASON_MAP[reason]
|
|
|
|
const type = mapped?.type || normalizeType(payload?.type || payload?.level, fallbackType)
|
|
const message =
|
|
mapped?.message ||
|
|
(typeof payload?.message === 'string' && payload.message.trim()) ||
|
|
fallbackMessage
|
|
|
|
return {
|
|
type,
|
|
message,
|
|
reason,
|
|
status: Number(payload?.status_code || 0),
|
|
}
|
|
}
|