This commit is contained in:
2025-06-10 14:52:39 +02:00
parent bb1a237961
commit 8c0d8cae69
5 changed files with 136 additions and 114 deletions

View File

@@ -22,53 +22,43 @@ export default defineEventHandler(async (event) => {
// If requested, also include email attachments from client-emails bucket
if (includeEmailAttachments && currentUserEmail) {
try {
// Get the user's full name from their interests
const { getInterests } = await import('~/server/utils/nocodb');
const interests = await getInterests();
const userInterest = interests.list?.find((interest: any) =>
interest['Email Address']?.toLowerCase() === currentUserEmail.toLowerCase()
);
// Create the folder name from the user's email
const username = currentUserEmail.split('@')[0]
.toLowerCase()
.replace(/[^a-z0-9-]/g, '');
if (userInterest && userInterest['Full Name']) {
// Create the folder name from the user's name
const clientName = userInterest['Full Name']
.toLowerCase()
.replace(/\s+/g, '-')
.replace(/[^a-z0-9-]/g, '');
const attachmentFolder = `${clientName}-attachments`;
// List files from the user's attachment folder
const attachmentFiles = await listFilesFromBucket('client-emails', attachmentFolder + '/', true);
// Add these files with a special flag to identify them as email attachments
const formattedAttachmentFiles = attachmentFiles.map((file: any) => ({
const attachmentFolder = `${username}-attachments`;
// List files from the user's attachment folder
const attachmentFiles = await listFilesFromBucket('client-emails', attachmentFolder + '/', true);
// Add these files with a special flag to identify them as email attachments
const formattedAttachmentFiles = attachmentFiles.map((file: any) => ({
...file,
isEmailAttachment: true,
displayPath: `Email Attachments/${file.name.replace(attachmentFolder + '/', '')}`,
bucket: 'client-emails'
}));
// Create a virtual folder for email attachments
if (formattedAttachmentFiles.length > 0 && !prefix) {
allFiles.push({
name: 'Email Attachments/',
size: 0,
lastModified: new Date(),
etag: '',
isFolder: true,
isVirtualFolder: true,
icon: 'mdi-email-multiple'
});
}
// If we're inside the Email Attachments folder, show the files
if (prefix === 'Email Attachments/') {
allFiles = formattedAttachmentFiles.map((file: any) => ({
...file,
isEmailAttachment: true,
displayPath: `Email Attachments/${file.name.replace(attachmentFolder + '/', '')}`,
bucket: 'client-emails'
name: file.name.replace(attachmentFolder + '/', '')
}));
// Create a virtual folder for email attachments
if (formattedAttachmentFiles.length > 0 && !prefix) {
allFiles.push({
name: 'Email Attachments/',
size: 0,
lastModified: new Date(),
etag: '',
isFolder: true,
isVirtualFolder: true,
icon: 'mdi-email-multiple'
});
}
// If we're inside the Email Attachments folder, show the files
if (prefix === 'Email Attachments/') {
allFiles = formattedAttachmentFiles.map((file: any) => ({
...file,
name: file.name.replace(attachmentFolder + '/', '')
}));
}
}
} catch (error) {
console.error('Error fetching email attachments:', error);

View File

@@ -1,13 +1,14 @@
import { uploadFile } from '~/server/utils/minio';
import { uploadFile, getMinioClient } from '~/server/utils/minio';
import formidable from 'formidable';
import { promises as fs } from 'fs';
import mime from 'mime-types';
export default defineEventHandler(async (event) => {
try {
// Get the current path from query params
// Get the current path and bucket from query params
const query = getQuery(event);
const currentPath = (query.path as string) || '';
const bucket = (query.bucket as string) || 'client-portal'; // Default bucket
// Parse multipart form data
const form = formidable({
@@ -38,23 +39,44 @@ export default defineEventHandler(async (event) => {
// Get content type
const contentType = mime.lookup(uploadedFile.originalFilename || '') || 'application/octet-stream';
// Upload to MinIO
await uploadFile(fullPath, fileBuffer, contentType);
// Upload to MinIO - handle different buckets
if (bucket === 'client-portal') {
await uploadFile(fullPath, fileBuffer, contentType);
} else {
// For other buckets, use the MinIO client directly
const client = getMinioClient();
await client.putObject(bucket, fullPath, fileBuffer, fileBuffer.length, {
'Content-Type': contentType,
});
}
// Clean up temp file
await fs.unlink(uploadedFile.filepath);
results.push({
fileName: fullPath,
path: fullPath,
originalName: uploadedFile.originalFilename,
size: uploadedFile.size,
contentType,
bucket: bucket
});
// Log audit event
await logAuditEvent(event, 'upload', fullPath, uploadedFile.size);
}
// Return the first file's info for single file uploads (backward compatibility)
if (results.length === 1) {
return {
success: true,
path: results[0].path,
fileName: results[0].fileName,
files: results,
message: `File uploaded successfully`,
};
}
return {
success: true,
files: results,