Merge pull request #116 from struffl/main

Tiny usability and security changes
This commit is contained in:
Jared Schoeny
2026-11-03 11:13:59 -07:00
committed by GitHub
2 changed files with 50 additions and 15 deletions

View File

@@ -31,6 +31,33 @@ function getPlaceholderBgClass(seed: string | null): string {
return PLACEHOLDER_BG_CLASSES[index]
}
/** Centre-crop to a square and downscale before upload, so a phone photo doesn't ship at full size. */
async function prepareAvatar(file: File, maxSize: number): Promise<File> {
try {
if (typeof createImageBitmap !== 'function') return file
const bitmap = await createImageBitmap(file)
const side = Math.min(bitmap.width, bitmap.height)
const out = Math.min(maxSize * 2, side)
const canvas = document.createElement('canvas')
canvas.width = out
canvas.height = out
const ctx = canvas.getContext('2d')
if (!ctx) {
bitmap.close()
return file
}
ctx.imageSmoothingEnabled = true
ctx.imageSmoothingQuality = 'high'
ctx.drawImage(bitmap, (bitmap.width - side) / 2, (bitmap.height - side) / 2, side, side, 0, 0, out, out)
bitmap.close()
const blob = await new Promise<Blob | null>((resolve) => canvas.toBlob(resolve, 'image/png'))
if (!blob) return file
return new File([blob], `${file.name.replace(/\.[^.]+$/, '')}.png`, { type: 'image/png' })
} catch {
return file
}
}
export default function Avatar({
uid,
url,
@@ -75,7 +102,7 @@ export default function Avatar({
throw new Error('You must select an image to upload.')
}
const file = event.target.files[0]
const file = await prepareAvatar(event.target.files[0], size)
const fileExt = file.name.split('.').pop()
const filePath = `${uid}-${Math.random()}.${fileExt}`

View File

@@ -169,20 +169,27 @@ function Shots({ images, platform, fill }: { images: string[]; platform?: Platfo
>
{fill && (
<span className="absolute inset-0 md:hidden" aria-hidden>
{images.map((src, i) => (
<img
key={`${src}-${i}`}
ref={(el) => {
backdropEls.current[i] = el;
}}
src={src}
alt=""
loading="lazy"
draggable={false}
style={{ "--fade": i === 0 ? 1 : 0 } as CSSProperties}
className="absolute -inset-6 h-[calc(100%+48px)] w-[calc(100%+48px)] max-w-none object-cover blur-[18px] saturate-[1.15] [opacity:calc(var(--fade)*.45)]"
/>
))}
{images.map((src, i) => {
// Only the active shot and its neighbours can be seen through the backdrop. Mounting the
// rest would fetch every screenshot in the catalog to fade in something already at zero.
const distance = Math.abs(i - index);
if (Math.min(distance, images.length - distance) > 2) return null;
return (
<img
key={`${src}-${i}`}
ref={(el) => {
backdropEls.current[i] = el;
}}
src={src}
alt=""
loading="lazy"
decoding="async"
draggable={false}
style={{ "--fade": i === index ? 1 : 0 } as CSSProperties}
className="absolute -inset-6 h-[calc(100%+48px)] w-[calc(100%+48px)] max-w-none object-cover blur-[18px] saturate-[1.15] [opacity:calc(var(--fade)*.45)]"
/>
);
})}
</span>
)}
<span ref={viewportRef} className={`relative block overflow-hidden ${many ? "cursor-grab active:cursor-grabbing" : ""} ${viewportWidth}`}>
@@ -200,6 +207,7 @@ function Shots({ images, platform, fill }: { images: string[]; platform?: Platfo
src={src}
alt=""
loading="lazy"
decoding="async"
draggable={false}
onLoad={(e) => snapRendering(e.currentTarget)}
className={`block h-auto w-full ${ratio} rounded-frame object-cover object-top ${fill ? "max-md:shadow-[0_4px_14px_rgba(0,0,0,.35)]" : ""}`}