fix(admin): preview public images through the admin thumb route when their URL fails
When a public image's thumb_url or url fails to load, the fileupload field fetches its preview once through the authenticated thumb route (session key in the header) and shows it from an object URL, revoked when the item is forgotten and on unmount. The preview is reported as unavailable only when that fetch or the object-URL image fails too. Protected fields behave as before.
This commit is contained in:
@@ -71,10 +71,18 @@ const draggedUid = ref<number | null>(null)
|
||||
const overUid = ref<number | null>(null)
|
||||
const captionUid = ref<number | null>(null)
|
||||
const captionOpen = ref(false)
|
||||
/** Protected thumbnails: item uid -> object URL. */
|
||||
/**
|
||||
* Thumbnails fetched through the admin thumb route: item uid -> object URL.
|
||||
* Covers protected files and public images in fallback.
|
||||
*/
|
||||
const thumbs = ref<Record<number, string>>({})
|
||||
/** Items whose preview could not be shown. */
|
||||
const broken = ref<Set<number>>(new Set())
|
||||
/**
|
||||
* Public images whose public URL failed to load; they are previewed through
|
||||
* the admin thumb route instead, like protected files.
|
||||
*/
|
||||
const fallback = ref<Set<number>>(new Set())
|
||||
const requested = new Set<number>()
|
||||
const transient: string[] = []
|
||||
const routes: FileRoutes | null = session ? session.routes(props.field.name) : null
|
||||
@@ -657,7 +665,7 @@ function previewOf(item: Item): string | null {
|
||||
if (!item.file || !isImage(item) || broken.value.has(item.uid)) {
|
||||
return null
|
||||
}
|
||||
if (!props.field.protected) {
|
||||
if (!props.field.protected && !fallback.value.has(item.uid)) {
|
||||
return item.file.thumb_url || item.file.url || null
|
||||
}
|
||||
return thumbs.value[item.uid] ?? null
|
||||
@@ -678,6 +686,11 @@ function forgetThumb(uid: number): void {
|
||||
thumbs.value = next
|
||||
}
|
||||
requested.delete(uid)
|
||||
if (fallback.value.has(uid)) {
|
||||
const next = new Set(fallback.value)
|
||||
next.delete(uid)
|
||||
fallback.value = next
|
||||
}
|
||||
}
|
||||
|
||||
async function loadThumb(item: Item): Promise<void> {
|
||||
@@ -710,7 +723,19 @@ watch(
|
||||
},
|
||||
)
|
||||
|
||||
/**
|
||||
* An img failed to load. A public image falls back once to the admin thumb
|
||||
* route (an application may not serve its public URL); a protected file, or
|
||||
* a fallback object URL that failed too, shows the preview as unavailable.
|
||||
*/
|
||||
function onPreviewError(item: Item): void {
|
||||
if (!props.field.protected && !fallback.value.has(item.uid)) {
|
||||
fallback.value = new Set([...fallback.value, item.uid])
|
||||
if (routes && typeof URL.createObjectURL === 'function') {
|
||||
void loadThumb(item)
|
||||
return
|
||||
}
|
||||
}
|
||||
broken.value = new Set([...broken.value, item.uid])
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user