Fix image formats and size, add feedback
This commit is contained in:
1 parent
feaab7e4e2
commit
fef1abade8
11 files changed
+192
-12
No files matched your search
@@ -15,6 +15,11 @@ server {
|
||||
root /srv/gochat/dist;
|
||||
index index.html;
|
||||
|
||||
# Avatar uploads are capped at 4 MiB by the backend; allow a little extra
|
||||
# for multipart form overhead. Without this nginx would reject them with
|
||||
# 413 Request Entity Too Large at its default 1m limit.
|
||||
client_max_body_size 5m;
|
||||
|
||||
# Hashed/static bundles can be cached aggressively; index.html must not be.
|
||||
location / {
|
||||
try_files $uri $uri/ /index.html;
|
||||
|
||||
@@ -86,6 +86,9 @@ export async function request(method, path, body, options = {}) {
|
||||
}
|
||||
|
||||
export const api = {
|
||||
// public configuration (avatar limits, etc.)
|
||||
getConfig: () => request('GET', '/api/config'),
|
||||
|
||||
// auth
|
||||
signup: (payload) => request('POST', '/api/auth/signup', payload),
|
||||
signin: (payload) => request('POST', '/api/auth/signin', payload),
|
||||
|
||||
@@ -1,6 +1,52 @@
|
||||
import React, { useRef, useState } from 'react'
|
||||
import React, { useEffect, useRef, useState } from 'react'
|
||||
import { api } from '../api.js'
|
||||
|
||||
// Fallback used until GET /api/config answers. Kept in sync with the backend
|
||||
// defaults (user.MaxImageSize and the accepted image formats).
|
||||
const DEFAULT_AVATAR_LIMITS = {
|
||||
maxSizeBytes: 4 << 20,
|
||||
maxWidth: 2048,
|
||||
maxHeight: 2048,
|
||||
allowedMimeTypes: ['image/jpeg', 'image/jpg', 'image/png'],
|
||||
}
|
||||
|
||||
// Builds the file picker `accept` value. MIME types cover the common case,
|
||||
// the explicit extensions make sure both ".jpg" and ".jpeg" are offered even
|
||||
// on pickers that do not recognise "image/jpg".
|
||||
function buildAccept(mimeTypes) {
|
||||
const types = mimeTypes?.length ? mimeTypes : DEFAULT_AVATAR_LIMITS.allowedMimeTypes
|
||||
const extensions = ['.jpg', '.jpeg', '.png']
|
||||
return [...types, ...extensions].join(',')
|
||||
}
|
||||
|
||||
function formatBytes(bytes) {
|
||||
if (!bytes || bytes <= 0) return '0 B'
|
||||
const units = ['B', 'KB', 'MB', 'GB']
|
||||
let value = bytes
|
||||
let unit = 0
|
||||
while (value >= 1024 && unit < units.length - 1) {
|
||||
value /= 1024
|
||||
unit += 1
|
||||
}
|
||||
const rounded = Number.isInteger(value) ? value : value.toFixed(1)
|
||||
return `${rounded} ${units[unit]}`
|
||||
}
|
||||
|
||||
// Maps accepted MIME types to a short human readable label (e.g. "JPEG, PNG").
|
||||
function formatTypes(mimeTypes) {
|
||||
const known = {
|
||||
'image/jpeg': 'JPEG',
|
||||
'image/jpg': 'JPEG',
|
||||
'image/png': 'PNG',
|
||||
}
|
||||
const labels = []
|
||||
for (const type of mimeTypes || []) {
|
||||
const label = known[type] || type
|
||||
if (!labels.includes(label)) labels.push(label)
|
||||
}
|
||||
return labels.join(', ')
|
||||
}
|
||||
|
||||
export default function ProfilePage({ user, onUserChange }) {
|
||||
const [fullName, setFullName] = useState(user.fullName || '')
|
||||
const [email, setEmail] = useState(user.email || '')
|
||||
@@ -12,8 +58,37 @@ export default function ProfilePage({ user, onUserChange }) {
|
||||
const [avatarMsg, setAvatarMsg] = useState(null)
|
||||
const [avatarVersion, setAvatarVersion] = useState(Date.now())
|
||||
const [avatarError, setAvatarError] = useState(false)
|
||||
const [avatarLimits, setAvatarLimits] = useState(DEFAULT_AVATAR_LIMITS)
|
||||
const fileRef = useRef(null)
|
||||
|
||||
// Query the backend for the avatar limits instead of hardcoding them, so the
|
||||
// file picker, the size hint and the client-side check always match what the
|
||||
// server enforces.
|
||||
useEffect(() => {
|
||||
let active = true
|
||||
api
|
||||
.getConfig()
|
||||
.then((config) => {
|
||||
if (active && config?.avatar) {
|
||||
setAvatarLimits({
|
||||
maxSizeBytes: config.avatar.maxSizeBytes ?? DEFAULT_AVATAR_LIMITS.maxSizeBytes,
|
||||
maxWidth: config.avatar.maxWidth ?? DEFAULT_AVATAR_LIMITS.maxWidth,
|
||||
maxHeight: config.avatar.maxHeight ?? DEFAULT_AVATAR_LIMITS.maxHeight,
|
||||
allowedMimeTypes:
|
||||
config.avatar.allowedMimeTypes?.length
|
||||
? config.avatar.allowedMimeTypes
|
||||
: DEFAULT_AVATAR_LIMITS.allowedMimeTypes,
|
||||
})
|
||||
}
|
||||
})
|
||||
.catch(() => {
|
||||
// Keep the hardcoded defaults when the config endpoint is unavailable.
|
||||
})
|
||||
return () => {
|
||||
active = false
|
||||
}
|
||||
}, [])
|
||||
|
||||
const initials = (user.fullName || user.login || '?').trim().charAt(0).toUpperCase()
|
||||
const hasAvatar = Boolean(user.hasAvatar) && !avatarError
|
||||
|
||||
@@ -45,6 +120,18 @@ export default function ProfilePage({ user, onUserChange }) {
|
||||
const file = event.target.files?.[0]
|
||||
if (!file) return
|
||||
setAvatarMsg(null)
|
||||
|
||||
if (file.size > avatarLimits.maxSizeBytes) {
|
||||
setAvatarMsg({
|
||||
type: 'error',
|
||||
text: `File is too large (${formatBytes(file.size)}). Maximum size is ${formatBytes(
|
||||
avatarLimits.maxSizeBytes,
|
||||
)}.`,
|
||||
})
|
||||
if (fileRef.current) fileRef.current.value = ''
|
||||
return
|
||||
}
|
||||
|
||||
try {
|
||||
await api.uploadAvatar(file)
|
||||
const profile = await api.getProfile()
|
||||
@@ -94,10 +181,15 @@ export default function ProfilePage({ user, onUserChange }) {
|
||||
<input
|
||||
ref={fileRef}
|
||||
type="file"
|
||||
accept="image/png,image/jpeg"
|
||||
accept={buildAccept(avatarLimits.allowedMimeTypes)}
|
||||
onChange={uploadAvatar}
|
||||
/>
|
||||
<p className="hint">PNG or JPEG.</p>
|
||||
<p className="hint">
|
||||
{formatTypes(avatarLimits.allowedMimeTypes)}. Max {formatBytes(avatarLimits.maxSizeBytes)}
|
||||
{avatarLimits.maxWidth && avatarLimits.maxHeight
|
||||
? `, up to ${avatarLimits.maxWidth}\u00d7${avatarLimits.maxHeight} px.`
|
||||
: '.'}
|
||||
</p>
|
||||
{user.hasAvatar && (
|
||||
<button className="btn-danger-sm" type="button" onClick={removeAvatar}>
|
||||
Remove avatar
|
||||
|
||||
Reference in new issue
Block a user