Skip to content

refactor: single source of truth for file supported size/type#60

Merged
igor47 merged 1 commit intomainfrom
igor/file-upload-validation
Nov 21, 2025
Merged

refactor: single source of truth for file supported size/type#60
igor47 merged 1 commit intomainfrom
igor/file-upload-validation

Conversation

@igor47
Copy link
Owner

@igor47 igor47 commented Nov 21, 2025

vs separate checks on FE and BE

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR refactors file upload validation by establishing a single source of truth for supported file types and size limits in src/db/uploads.ts, removing duplicate validation logic from the frontend and improving consistency across the codebase.

Key changes:

  • Removed client-side file type and size validation from static/character.js
  • Increased maximum upload size from 5MB to 10MB and added HEIF image format support in src/db/uploads.ts
  • Updated UploadAvatarForm.tsx to dynamically calculate max file size from backend constants
  • Improved error messages in src/services/uploads.ts to be more user-friendly

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.

File Description
static/character.js Removed duplicate frontend validation for file type and size
src/services/uploads.ts Updated error messages to be more user-friendly and consistent
src/db/uploads.ts Increased max upload size to 10MB and added HEIF format support
src/components/UploadAvatarForm.tsx Refactored to use backend constants for max size and converted from arrow function to named function

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +24 to +26
accept="image/jpeg,image/png,image/webp,image/gif"
/>
<div class="form-text">Supported formats: JPEG, PNG, WebP, GIF</div>
Copy link

Copilot AI Nov 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The accept attribute on the file input doesn't include image/heif, which was added to ALLOWED_IMAGE_TYPES in src/db/uploads.ts. This means users won't be able to select HEIF files from the file picker even though the backend now accepts them. Add image/heif to the accept attribute.

Suggested change
accept="image/jpeg,image/png,image/webp,image/gif"
/>
<div class="form-text">Supported formats: JPEG, PNG, WebP, GIF</div>
accept="image/jpeg,image/png,image/webp,image/gif,image/heif"
/>
<div class="form-text">Supported formats: JPEG, PNG, WebP, GIF, HEIF</div>

Copilot uses AI. Check for mistakes.
id="avatarFileInput"
accept="image/jpeg,image/png,image/webp,image/gif"
/>
<div class="form-text">Supported formats: JPEG, PNG, WebP, GIF</div>
Copy link

Copilot AI Nov 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The supported formats documentation doesn't mention HEIF, which was added to ALLOWED_IMAGE_TYPES in src/db/uploads.ts. Update this to include HEIF so users know it's supported.

Suggested change
<div class="form-text">Supported formats: JPEG, PNG, WebP, GIF</div>
<div class="form-text">Supported formats: JPEG, PNG, WebP, GIF, HEIF</div>

Copilot uses AI. Check for mistakes.
if (!isValidSize(sizeBytes)) {
throw new Error(
`Invalid file size: ${sizeBytes} bytes. Maximum allowed: ${uploads.MAX_UPLOAD_SIZE} bytes`
`File size (${sizeBytes / (1024 * 1024)}MB) too big; max allowed: ${uploads.MAX_UPLOAD_SIZE / (1024 * 1024)} MB`
Copy link

Copilot AI Nov 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Inconsistent spacing before "MB" in the error message. The first occurrence has no space (${sizeBytes / (1024 * 1024)}MB) while the second has a space (${uploads.MAX_UPLOAD_SIZE / (1024 * 1024)} MB). For consistency, either use a space in both places or omit it in both.

Suggested change
`File size (${sizeBytes / (1024 * 1024)}MB) too big; max allowed: ${uploads.MAX_UPLOAD_SIZE / (1024 * 1024)} MB`
`File size (${sizeBytes / (1024 * 1024)} MB) too big; max allowed: ${uploads.MAX_UPLOAD_SIZE / (1024 * 1024)} MB`

Copilot uses AI. Check for mistakes.
@igor47 igor47 force-pushed the igor/file-upload-validation branch from 8bbf6a7 to 284b16f Compare November 21, 2025 20:40
@igor47 igor47 merged commit b338513 into main Nov 21, 2025
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants