-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuploadHandler.js
More file actions
109 lines (99 loc) · 3.54 KB
/
uploadHandler.js
File metadata and controls
109 lines (99 loc) · 3.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
const AWS = require('aws-sdk');
const crypto = require('crypto');
const S3 = new AWS.S3();
const BUCKET_NAME = process.env.BUCKET_NAME;
exports.uploadProfilePicture = async (event) => {
console.log('Received event:', JSON.stringify(event, null, 2));
let file;
try {
const body = JSON.parse(event.body);
file = body.file;
} catch (error) {
console.error('Error parsing JSON:', error);
return {
statusCode: 400,
headers: {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Headers': 'Content-Type',
},
body: JSON.stringify({ error: 'Invalid JSON' }),
};
}
if (!file) {
console.warn('No file provided');
return {
statusCode: 400,
headers: {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Headers': 'Content-Type',
},
body: JSON.stringify({ error: 'No file provided' }),
};
}
const base64Data = file.split(',')[1];
const contentType = file.split(';')[0].split(':')[1];
// Hash the image
const hash = crypto.createHash('md5').update(Buffer.from(base64Data, 'base64')).digest('hex');
console.log('Image hash:', hash);
// Create a unique filename based on content type and hash
const filename = `profile-pictures/${hash}_profile_picture.${contentType.split('/')[1]}`;
// Check if the file already exists
const params = {
Bucket: BUCKET_NAME,
Key: filename,
};
try {
await S3.headObject(params).promise(); // Check if the object exists
console.log('File already exists:', filename);
const imageUrl = `https://${BUCKET_NAME}.s3.amazonaws.com/${filename}`;
return {
statusCode: 200,
headers: {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Headers': 'Content-Type',
},
body: JSON.stringify({ message: 'File already exists', imageUrl }),
};
} catch (error) {
if (error.code !== 'NotFound') {
console.error('Error checking file existence:', error);
return {
statusCode: 500,
headers: {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Headers': 'Content-Type',
},
body: JSON.stringify({ error: 'Could not check file existence', details: error.message }),
};
}
}
// If the file doesn't exist, proceed to upload
const uploadParams = {
Bucket: BUCKET_NAME,
Key: filename,
Body: Buffer.from(base64Data, 'base64'),
ContentType: contentType,
};
try {
await S3.upload(uploadParams).promise();
const imageUrl = `https://${BUCKET_NAME}.s3.amazonaws.com/${filename}`;
return {
statusCode: 200,
headers: {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Headers': 'Content-Type',
},
body: JSON.stringify({ message: 'Upload Successful', imageUrl }),
};
} catch (error) {
console.error('Upload failed:', error);
return {
statusCode: 500,
headers: {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Headers': 'Content-Type',
},
body: JSON.stringify({ error: 'Could not upload image', details: error.message }),
};
}
};