Skip to content

Commit 7b90ddb

Browse files
committed
S3 Bucket implementation for profile image
1 parent 3398b59 commit 7b90ddb

12 files changed

Lines changed: 1533 additions & 95 deletions

File tree

api-gateway/src/middlewares/patient.guard.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,17 @@ export function requirePatientSelf(
1010
return res.status(401).json({ error: 'Unauthorized' });
1111
}
1212

13-
const patientIdFromPath = req.path.split('/')[1];
14-
15-
if (!patientIdFromPath) {
16-
return res.status(400).json({ error: 'Invalid patient path' });
17-
}
13+
const pathParts = req.path.split('/');
14+
const patientIdFromPath = pathParts[1];
1815

1916
/**
20-
* Allow /patients/me directly
17+
* Allow special routes
2118
*/
22-
if (patientIdFromPath === 'me') {
19+
if (
20+
patientIdFromPath === 'me' ||
21+
patientIdFromPath === 'upload' ||
22+
patientIdFromPath === 'profile-image'
23+
) {
2324
return next();
2425
}
2526

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { api } from '@/src/lib/api';
2+
3+
export async function uploadProfileImage(file: File) {
4+
// 1 get signed upload url
5+
const { data } = await api.get('/patients/upload/upload-url');
6+
7+
const { uploadUrl, fileUrl } = data;
8+
9+
// 2 upload to s3
10+
await fetch(uploadUrl, {
11+
method: 'PUT',
12+
body: file,
13+
headers: {
14+
'Content-Type': file.type,
15+
},
16+
});
17+
18+
// 3 save url in database
19+
await api.put('/patients/profile-image', {
20+
profile_image: fileUrl,
21+
});
22+
23+
return fileUrl;
24+
}

0 commit comments

Comments
 (0)