This commit is contained in:
2025-06-10 15:21:42 +02:00
parent 4b6d3fd991
commit c6b4c716a8
5 changed files with 170 additions and 36 deletions

View File

@@ -1,9 +1,10 @@
import { getDownloadUrl } from '~/server/utils/minio';
import { getMinioClient } from '~/server/utils/minio';
export default defineEventHandler(async (event) => {
try {
const query = getQuery(event);
const fileName = query.fileName as string;
const bucket = (query.bucket as string) || 'client-portal'; // Support bucket parameter
if (!fileName) {
throw createError({
@@ -18,10 +19,26 @@ export default defineEventHandler(async (event) => {
for (let attempt = 0; attempt < 3; attempt++) {
try {
console.log(`[proxy-download] Attempting to download ${fileName} (attempt ${attempt + 1}/3)`);
console.log(`[proxy-download] Attempting to download ${fileName} from bucket ${bucket} (attempt ${attempt + 1}/3)`);
// Get the download URL from MinIO
const url = await getDownloadUrl(fileName);
// Get the download URL from MinIO with the correct bucket
const client = getMinioClient();
// Extract just the filename for the download header
let filename = fileName.split('/').pop() || fileName;
// Remove timestamp prefix if present
const timestampMatch = filename.match(/^\d{10,}-(.+)$/);
if (timestampMatch) {
filename = timestampMatch[1];
}
// Generate presigned URL with download headers
const responseHeaders = {
'response-content-disposition': `attachment; filename="${filename}"`,
};
const url = await client.presignedGetObject(bucket, fileName, 60 * 60, responseHeaders);
// Fetch the file from MinIO with timeout
const controller = new AbortController();

View File

@@ -1,12 +1,13 @@
import { getDownloadUrl } from '~/server/utils/minio';
import { getMinioClient } from '~/server/utils/minio';
import mime from 'mime-types';
export default defineEventHandler(async (event) => {
try {
const query = getQuery(event);
const fileName = query.fileName as string;
const bucket = (query.bucket as string) || 'client-portal'; // Support bucket parameter
console.log('Proxy preview requested for:', fileName);
console.log('Proxy preview requested for:', fileName, 'in bucket:', bucket);
if (!fileName) {
throw createError({
@@ -19,9 +20,10 @@ export default defineEventHandler(async (event) => {
const contentType = mime.lookup(fileName) || 'application/octet-stream';
console.log('Content type:', contentType);
// Get the download URL
const url = await getDownloadUrl(fileName);
console.log('MinIO URL obtained');
// Get the download URL with the correct bucket
const client = getMinioClient();
const url = await client.presignedGetObject(bucket, fileName, 60 * 60);
console.log('MinIO URL obtained for bucket:', bucket);
// Fetch the file content from MinIO
const response = await fetch(url);
@@ -44,7 +46,7 @@ export default defineEventHandler(async (event) => {
setHeader(event, 'Content-Type', contentType);
setHeader(event, 'Content-Disposition', `inline; filename="${fileName.split('/').pop()}"`);
setHeader(event, 'Cache-Control', 'public, max-age=3600');
setHeader(event, 'Content-Length', String(buffer.length));
setHeader(event, 'Content-Length', buffer.length);
// For PDF files, add additional headers
if (contentType === 'application/pdf') {