Skip to content

Commit 14c4486

Browse files
committed
fix: preview field: instead of always guessing content type based on url, do a HEAD to upstream which can return correct value from metadata even if URL has file without extension. Both ways are cross-fallback so if any of them will not work other one will do a best to display image correctly
1 parent 6cf3c74 commit 14c4486

File tree

1 file changed

+26
-2
lines changed

1 file changed

+26
-2
lines changed

custom/preview.vue

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,30 @@ const props = defineProps({
7171
meta: Object,
7272
})
7373
74+
const trueContentType = ref(null);
75+
76+
onMounted(async () => {
77+
// try to get HEAD request
78+
try {
79+
const response = await fetch(url.value, {
80+
method: 'HEAD',
81+
});
82+
const ct = response.headers.get('Content-Type');
83+
if (ct) {
84+
trueContentType.value = ct;
85+
}
86+
} catch (e) {
87+
console.error('fetch error for getting content type, please check CORS allowed for HEAD request', e);
88+
}
89+
});
90+
91+
const contentType = computed(() => {
92+
if (trueContentType.value) {
93+
return trueContentType.value;
94+
}
95+
return guessedContentType.value;
96+
});
97+
7498
const route = useRoute();
7599
const url = computed(() => {
76100
return props.record[`previewUrl_${props.meta.pluginInstanceId}`];
@@ -95,11 +119,11 @@ const minWidth = computed(() => {
95119
});
96120
// since we have no way to know the content type of the file, we will try to guess it from extension
97121
// for better experience probably we should check whether user saves content type in the database and use it here
98-
const contentType = computed(() => {
122+
const guessedContentType = computed(() => {
99123
if (!url.value) {
100124
return null;
101125
}
102-
const u = new URL(url.value);
126+
const u = new URL(url.value, url.value.startsWith('http') ? undefined : location.origin);
103127
return guessContentType(u.pathname);
104128
});
105129

0 commit comments

Comments
 (0)