Add proxy endpoint to fix CORS issues for file previews

- Create new `/api/files/proxy-preview` endpoint that fetches and serves files directly
- Update FilePreviewModal to use proxy endpoint for images and PDFs
- Set appropriate headers for inline display and caching
- Remove trailing slash from folder display names in file list
This commit is contained in:
2025-06-04 17:19:52 +02:00
parent 7d5b39b29d
commit 9e9c667d1f
3 changed files with 59 additions and 7 deletions

View File

@@ -155,13 +155,16 @@ const loadPreview = async () => {
previewUrl.value = '';
try {
const response = await $fetch('/api/files/preview', {
params: { fileName: props.file.name },
});
previewUrl.value = response.url;
// For images and PDFs, use the proxy endpoint to avoid CORS issues
if (isImage.value || isPdf.value) {
// Use the proxy endpoint that serves the file directly
previewUrl.value = `/api/files/proxy-preview?fileName=${encodeURIComponent(props.file.name)}`;
// The loading state will be handled by the image/iframe onload event
} else {
throw new Error('File type does not support preview');
}
} catch (err: any) {
error.value = err.data?.statusMessage || 'Failed to load preview';
error.value = err.data?.statusMessage || err.message || 'Failed to load preview';
loading.value = false;
}
};