diff --git a/app/utils/files.test.ts b/app/utils/files.test.ts index f3b753e317..b01461ce27 100644 --- a/app/utils/files.test.ts +++ b/app/utils/files.test.ts @@ -20,4 +20,17 @@ describe("#toDropzoneAccept", () => { "image/heif": [], }); }); + + it("should keep extensions when there are no mime types", () => { + const accept = toDropzoneAccept([".png", ".jpg"]); + const entries = Object.entries(accept); + + expect(entries).toHaveLength(1); + expect(entries[0][0]).toMatch(/^application\/x-/); + expect(entries[0][1]).toEqual([".png", ".jpg"]); + }); + + it("should return an empty map when there are no types", () => { + expect(toDropzoneAccept([])).toEqual({}); + }); }); diff --git a/app/utils/files.ts b/app/utils/files.ts index 55eeb08ca3..2909e4629b 100644 --- a/app/utils/files.ts +++ b/app/utils/files.ts @@ -146,7 +146,8 @@ export { dataUrlToBlob }; /** * Converts a list of mime types and file extensions to the accept map used by * react-dropzone. The map is flattened into a single accept attribute, so the - * extensions are grouped under the first mime type. + * extensions are grouped under the first mime type, or a placeholder if there + * is none. * * @param types the mime types and file extensions, such as "image/png" or ".png". * @returns the accept map. @@ -155,6 +156,10 @@ export const toDropzoneAccept = (types: readonly string[]): Accept => { const mimeTypes = types.filter((type) => !type.startsWith(".")); const extensions = types.filter((type) => type.startsWith(".")); + if (mimeTypes.length === 0 && extensions.length > 0) { + return { [extensionOnlyMimeType]: extensions }; + } + return Object.fromEntries( mimeTypes.map((mimeType, index) => [ mimeType, @@ -163,6 +168,9 @@ export const toDropzoneAccept = (types: readonly string[]): Accept => { ); }; +/** A key for extensions with no mime type, no file reports this type. */ +const extensionOnlyMimeType = "application/x-outline-extension-only"; + /** * Header names the browser controls itself - it silently ignores an attempt to * set one and logs an error to the console.