-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpicture.patch.ts
More file actions
37 lines (29 loc) · 955 Bytes
/
picture.patch.ts
File metadata and controls
37 lines (29 loc) · 955 Bytes
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
import { cloudinary, getMessage, mongo } from "~/server";
export default defineEventHandler(async (event) => {
const {userId, newPicture} = await readBody(event);
let imageURL = null;
try {
imageURL = await cloudinary.uploader.upload(newPicture, {
public_id: `${userId}`,
overwrite: true,
unique_filename: false,
});
} catch(error) {
console.error("Error uploading picture to Cloudinary", error);
const message = getMessage(error);
setResponseStatus(event, 500, message);
}
if (imageURL) {
try {
await mongo.updateUser(userId, { picture: imageURL.secure_url })
} catch(error) {
console.error("Error updating user's picture", error);
const message = getMessage(error);
setResponseStatus(event, 500, message);
}
setResponseStatus(event, 200);
return imageURL.secure_url;
} else {
setResponseStatus(event, 500, "Could not update picture");
}
})