Allow PNG custom background uploads

This commit is contained in:
Sunwood-ai-labs
2026-05-03 14:55:21 +09:00
parent c8d4e867b2
commit 613ed008fc
11 changed files with 51 additions and 12 deletions
@@ -43,6 +43,7 @@ import { getTestId } from "@/utils/getTestId";
import ColorPicker from "../ui/color-picker";
import { AnnotationSettingsPanel } from "./AnnotationSettingsPanel";
import { BlurSettingsPanel } from "./BlurSettingsPanel";
import { BACKGROUND_IMAGE_ACCEPT, isSupportedBackgroundImageType } from "./backgroundImageUpload";
import { CropControl } from "./CropControl";
import { KeyboardShortcutsHelp } from "./KeyboardShortcutsHelp";
import type {
@@ -459,9 +460,7 @@ export function SettingsPanel({
const file = files[0];
// Validate file type - only allow JPG/JPEG
const validTypes = ["image/jpeg", "image/jpg"];
if (!validTypes.includes(file.type)) {
if (!isSupportedBackgroundImageType(file.type, file.name)) {
toast.error(t("imageUpload.invalidFileType"), {
description: t("imageUpload.jpgOnly"),
});
@@ -1041,7 +1040,7 @@ export function SettingsPanel({
type="file"
ref={fileInputRef}
onChange={handleImageUpload}
accept=".jpg,.jpeg,image/jpeg"
accept={BACKGROUND_IMAGE_ACCEPT}
className="hidden"
/>
<Button
@@ -0,0 +1,20 @@
import { describe, expect, it } from "vitest";
import { isSupportedBackgroundImageType } from "./backgroundImageUpload";
describe("background image upload validation", () => {
it("accepts PNG images for custom backgrounds", () => {
expect(isSupportedBackgroundImageType("image/png", "生成画像1.png")).toBe(true);
});
it("accepts PNG images by extension when the browser does not provide a MIME type", () => {
expect(isSupportedBackgroundImageType("", "生成画像1.png")).toBe(true);
});
it("keeps rejecting non-image uploads", () => {
expect(isSupportedBackgroundImageType("text/plain", "notes.txt")).toBe(false);
});
it("does not allow extension fallback for explicit unsupported MIME types", () => {
expect(isSupportedBackgroundImageType("text/plain", "notes.png")).toBe(false);
});
});
@@ -0,0 +1,20 @@
const SUPPORTED_BACKGROUND_IMAGE_TYPES = new Set(["image/jpeg", "image/jpg", "image/png"]);
const SUPPORTED_BACKGROUND_IMAGE_EXTENSIONS = new Set([".jpg", ".jpeg", ".png"]);
export const BACKGROUND_IMAGE_ACCEPT = ".jpg,.jpeg,.png,image/jpeg,image/png";
export function isSupportedBackgroundImageType(type: string, fileName: string): boolean {
const normalizedType = type.trim().toLowerCase();
if (SUPPORTED_BACKGROUND_IMAGE_TYPES.has(normalizedType)) {
return true;
}
if (normalizedType) {
return false;
}
const lowerName = fileName.trim().toLowerCase();
return [...SUPPORTED_BACKGROUND_IMAGE_EXTENSIONS].some((extension) =>
lowerName.endsWith(extension),
);
}