Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions packages/backend/src/routes/avatar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ const s3Client = new S3Client(
)

export const handleAvatarUpload = async (req: Request, res: Response) => {
const key = req.headers['x-user-id'] as string

let key: string
let base64String: string
try {
const parsed = JSON.parse(req.body.toString())
base64String = parsed.data
key = parsed.id
} catch {
base64String = req.body.data
return res.status(400).send('Could not JSON.parse() the request body')
}

if (!base64String) return res.status(400).send('No file provided in request')
Expand All @@ -50,7 +50,13 @@ export const handleAvatarUpload = async (req: Request, res: Response) => {
}

export const handleAvatarDeletion = async (req: Request, res: Response) => {
const key = req.headers['x-user-id'] as string
let key: string
try {
const parsed = JSON.parse(req.body.toString())
key = parsed.id
} catch {
return res.status(400).send('Could not JSON.parse() the request body')
}

s3Client.send(
new DeleteObjectCommand({
Expand Down
23 changes: 19 additions & 4 deletions packages/frontend/src/components/avatar-settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,8 @@ const AvatarSettings: React.FunctionComponent = () => {
headers: {
authorization: 'allow',
'Content-Type': file.type,
'X-User-Id': currentUser.id,
},
body: JSON.stringify({ data: base64String }),
body: JSON.stringify({ id: currentUser.id, data: base64String }),
credentials: 'include',
})

Expand All @@ -140,6 +139,13 @@ const AvatarSettings: React.FunctionComponent = () => {
text: strings.settings.avatar.toasts.errorFileSize.description,
type: 'error',
})
} else {
addToast({
icon: 'Error',
title: `Error ${response.status}:`,
text: `${await response.text()}`,
type: 'error',
})
}
}
}
Expand All @@ -155,15 +161,24 @@ const AvatarSettings: React.FunctionComponent = () => {
async function removeAvatar() {
if (!currentUser) return

await fetch(`${BackendUrl}/avatar`, {
const response = await fetch(`${BackendUrl}/avatar`, {
method: 'DELETE',
headers: {
authorization: 'allow',
'X-User-Id': currentUser.id,
},
body: JSON.stringify({ id: currentUser.id }),
credentials: 'include',
})

if (!response.ok) {
addToast({
icon: 'Error',
title: `Error ${response.status}:`,
text: `${await response.text()}`,
type: 'error',
})
}

eventBus.emit('avatarUpdated', undefined)
}

Expand Down