Skip to content

Commit ceda35c

Browse files
authored
Merge pull request #1198 from trycompai/main
[comp] Production Deploy
2 parents 9a3d2dd + 3dc0475 commit ceda35c

2 files changed

Lines changed: 48 additions & 12 deletions

File tree

apps/app/src/actions/files/upload-file.ts

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,35 @@ const uploadAttachmentSchema = z.object({
4242
export const uploadFile = async (input: z.infer<typeof uploadAttachmentSchema>) => {
4343
logger.info(`[uploadFile] Starting upload for ${input.fileName}`);
4444
try {
45+
// Check if S3 client is available
46+
if (!s3Client) {
47+
logger.error('[uploadFile] S3 client not initialized - check environment variables');
48+
return {
49+
success: false,
50+
error: 'File upload service is currently unavailable. Please contact support.',
51+
} as const;
52+
}
53+
54+
if (!BUCKET_NAME) {
55+
logger.error('[uploadFile] S3 bucket name not configured');
56+
return {
57+
success: false,
58+
error: 'File upload service is not properly configured.',
59+
} as const;
60+
}
61+
4562
const { fileName, fileType, fileData, entityId, entityType, pathToRevalidate } =
4663
uploadAttachmentSchema.parse(input);
4764

4865
const session = await auth.api.getSession({ headers: await headers() });
4966
const organizationId = session?.session.activeOrganizationId;
5067

5168
if (!organizationId) {
52-
throw new Error('Not authorized - no organization found');
69+
logger.error('[uploadFile] Not authorized - no organization found');
70+
return {
71+
success: false,
72+
error: 'Not authorized - no organization found',
73+
} as const;
5374
}
5475

5576
logger.info(`[uploadFile] Starting upload for ${fileName} in org ${organizationId}`);
@@ -59,7 +80,13 @@ export const uploadFile = async (input: z.infer<typeof uploadAttachmentSchema>)
5980
const MAX_FILE_SIZE_MB = 10;
6081
const MAX_FILE_SIZE_BYTES = MAX_FILE_SIZE_MB * 1024 * 1024;
6182
if (fileBuffer.length > MAX_FILE_SIZE_BYTES) {
62-
throw new Error(`File exceeds the ${MAX_FILE_SIZE_MB}MB limit.`);
83+
logger.warn(
84+
`[uploadFile] File size ${fileBuffer.length} exceeds the ${MAX_FILE_SIZE_MB}MB limit.`,
85+
);
86+
return {
87+
success: false,
88+
error: `File exceeds the ${MAX_FILE_SIZE_MB}MB limit.`,
89+
} as const;
6390
}
6491

6592
const timestamp = Date.now();

apps/app/src/app/s3.ts

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,33 +14,42 @@ const redact = (value?: string) => {
1414
};
1515

1616
try {
17+
console.log('[S3] Initializing S3 Client');
18+
console.error('--- Provided Configuration ---');
19+
console.error(`APP_AWS_REGION: ${APP_AWS_REGION}`);
20+
console.error(`APP_AWS_ACCESS_KEY_ID: ${redact(APP_AWS_ACCESS_KEY_ID)}`);
21+
console.error(`APP_AWS_SECRET_ACCESS_KEY: ${redact(APP_AWS_SECRET_ACCESS_KEY)}`);
22+
console.error(`APP_AWS_BUCKET_NAME: ${BUCKET_NAME}`);
23+
console.error('-----------------------------');
24+
1725
if (!APP_AWS_ACCESS_KEY_ID || !APP_AWS_SECRET_ACCESS_KEY || !BUCKET_NAME || !APP_AWS_REGION) {
26+
console.error('[S3] AWS S3 credentials or configuration missing. Check environment variables.');
1827
throw new Error('AWS S3 credentials or configuration missing. Check environment variables.');
1928
}
2029

30+
console.log('[S3] AWS S3 credentials or configuration found. Initializing S3 Client');
31+
2132
s3ClientInstance = new S3Client({
2233
region: APP_AWS_REGION,
2334
credentials: {
2435
accessKeyId: APP_AWS_ACCESS_KEY_ID,
2536
secretAccessKey: APP_AWS_SECRET_ACCESS_KEY,
2637
},
2738
});
39+
40+
console.log('[S3] S3 Client initialized successfully');
2841
} catch (error) {
2942
console.error('!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!');
3043
console.error('!!! FAILED TO INITIALIZE S3 CLIENT !!!');
3144
console.error('!!! This is likely due to missing or invalid environment variables. !!!');
32-
console.error('--- Provided Configuration ---');
33-
console.error(`APP_AWS_REGION: ${APP_AWS_REGION}`);
34-
console.error(`APP_AWS_ACCESS_KEY_ID: ${redact(APP_AWS_ACCESS_KEY_ID)}`);
35-
console.error(`APP_AWS_SECRET_ACCESS_KEY: ${redact(APP_AWS_SECRET_ACCESS_KEY)}`);
36-
console.error(`APP_AWS_BUCKET_NAME: ${BUCKET_NAME}`);
37-
console.error('-----------------------------');
3845
console.error('Error:', error);
3946
console.error('!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!');
40-
// Prevent the app from continuing without a valid S3 client
41-
// In a real-world scenario, you might have a fallback or a dummy client.
42-
// For now, we re-throw to make the crash explicit.
43-
throw error;
47+
48+
// Create a dummy client that will fail gracefully at runtime instead of crashing during initialization
49+
s3ClientInstance = null as any;
50+
console.error(
51+
'[S3] Creating dummy S3 client - file uploads will fail until credentials are fixed',
52+
);
4453
}
4554

4655
export const s3Client = s3ClientInstance;

0 commit comments

Comments
 (0)