Skip to content

Commit

Permalink
fix: reject profile photos bigger than 1MB
Browse files Browse the repository at this point in the history
  • Loading branch information
kabaros committed Dec 15, 2023
1 parent 6ed1dc3 commit 8f1d8e9
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/layout/AvatarEditor.component.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,19 @@ import React, { Component } from 'react'
import userSettingsActions from '../app.actions.js'
import i18n from '../locales/index.js'
import './avatareditor.component.css'
import optionValueStore from '../optionValue.store.js'

const MAX_PROFILE_PICTURE_SIZE_IN_MB = 2 // maximum allowed file size for an avatar (2MB)

class AvatarEditor extends Component {
constructor(props) {
super(props)

// This system setting does not exist currently, but leaving it here in case 1MB is not enough in some contexts
this.maxPhotoSize =
optionValueStore?.state.systemDefault?.keyMaxAvatarSizeInMB ??
MAX_PROFILE_PICTURE_SIZE_IN_MB

this.api = props.d2.Api.getApi()
this.userId = props.currentUser.id

Expand All @@ -36,6 +44,23 @@ class AvatarEditor extends Component {
// Setup form data for image file
const file = event.target.files[0]

// reject files bigger than the maximum allowed size
const maxSize = this.maxPhotoSize * 1000 * 1024 // maximum size in bytes
if (file?.size > maxSize) {
userSettingsActions.showSnackbarMessage({
message: i18n.t(
`Please choose a profile avatar less than {{- maxSize}}MB in size.`,
{
maxSize: this.maxPhotoSize,
}
),
status: 'error',
permanent: true,
})

return
}

// Cancel was pressed, no file provided
if (!file) {
return
Expand Down

0 comments on commit 8f1d8e9

Please sign in to comment.