Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: reject profile photos bigger than 2MB (backport v39) #1379

Merged
merged 1 commit into from
Dec 15, 2023
Merged
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
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
Loading