Add base rom file type restrictions

This commit is contained in:
Jared Schoeny 2025-10-18 22:17:04 -10:00
parent 2a007c4be2
commit e47fe2a1d3
3 changed files with 46 additions and 2 deletions

View File

@ -5,6 +5,9 @@ import { FaTriangleExclamation } from "react-icons/fa6";
import { baseRoms } from "@/data/baseRoms";
import { useBaseRoms } from "@/contexts/BaseRomContext";
import BaseRomCard from "@/components/BaseRomCard";
import { platformAcceptAll } from "@/utils/idb";
const ALLOWED_TYPES = platformAcceptAll().split(",");
export default function RomsInteractive() {
const { supported, linked, statuses, cached, totalCachedBytes, importUploadedBlob, importToCache, removeFromCache, unlinkRom, ensurePermission, countReady } = useBaseRoms();
@ -59,6 +62,10 @@ export default function RomsInteractive() {
setDragActive(false);
const file = e.dataTransfer?.files?.[0];
if (!file) return;
if (!ALLOWED_TYPES.includes(`.${file.name.split(".").pop()?.toLowerCase() ?? ""}`)) {
setUploadMsg(`Unrecognized file. Not cached. Accepted types: ${ALLOWED_TYPES.join(", ")}`);
return;
}
const name = await importUploadedBlob(file);
if (name) setUploadMsg(`Recognized and cached: ${name}`);
else setUploadMsg("Unrecognized ROM. Not cached.");
@ -76,7 +83,7 @@ export default function RomsInteractive() {
</div>
</div>
<label className="inline-flex cursor-pointer items-center justify-center rounded-md bg-[var(--accent)] px-3 py-2 text-sm font-medium text-[var(--accent-foreground)] transition-colors hover:bg-[var(--accent-700)]">
<input type="file" onChange={onUpload} className="hidden" />
<input type="file" onChange={onUpload} className="hidden" accept={platformAcceptAll()} />
Choose file
</label>
</div>

View File

@ -1,7 +1,15 @@
export const PLATFORMS = [
"GB",
"GBC",
"GBA",
"NDS",
] as const;
export type Platform = typeof PLATFORMS[number];
export type BaseRom = {
id: string;
name: string;
platform: "GB" | "GBC" | "GBA" | "NDS";
platform: Platform;
region: string;
sha1?: string;
};

View File

@ -1,4 +1,6 @@
// Minimal IndexedDB helpers for storing FileSystemFileHandle references
import type { Platform } from "@/data/baseRoms";
import { PLATFORMS } from "@/data/baseRoms";
const DB_NAME = "romhaven";
const DB_VERSION = 2;
@ -113,4 +115,31 @@ export async function getAllBlobEntries(): Promise<Array<{ name: string; blob: B
});
}
export function platformAccept(p: Platform | Platform[]): string {
if (Array.isArray(p)) {
// Gather all individual extensions and dedupe
const extSet = new Set<string>();
for (const plat of p) {
const exts = platformAccept(plat).split(",");
for (const ext of exts) {
extSet.add(ext.trim());
}
}
return Array.from(extSet).join(",");
}
// Exhaustive check using a mapping object for platform strings
const mapping: Record<Platform, string> = {
GB: ".gb",
GBC: ".gbc,.gb",
GBA: ".gba",
NDS: ".nds",
};
// If `p` is not a valid Platform, TypeScript will error here.
return mapping[p];
}
export function platformAcceptAll(): string {
return platformAccept([...PLATFORMS]);
}