From 070c5f22fece8e547470b264c59d4fc0a8ccd046 Mon Sep 17 00:00:00 2001 From: Michael Bromley Date: Fri, 26 Feb 2021 15:57:07 +0100 Subject: [PATCH] fix(core): Allow asset uploads with same major mime type Fixes #727 --- packages/core/e2e/asset.e2e-spec.ts | 32 +++++++++++++++++-- .../src/service/services/asset.service.ts | 9 ++++-- 2 files changed, 36 insertions(+), 5 deletions(-) diff --git a/packages/core/e2e/asset.e2e-spec.ts b/packages/core/e2e/asset.e2e-spec.ts index cef4bdcd19..fa24c6c891 100644 --- a/packages/core/e2e/asset.e2e-spec.ts +++ b/packages/core/e2e/asset.e2e-spec.ts @@ -6,7 +6,7 @@ import gql from 'graphql-tag'; import path from 'path'; import { initialData } from '../../../e2e-common/e2e-initial-data'; -import { TEST_SETUP_TIMEOUT_MS, testConfig } from '../../../e2e-common/test-config'; +import { testConfig, TEST_SETUP_TIMEOUT_MS } from '../../../e2e-common/test-config'; import { ASSET_FRAGMENT } from './graphql/fragments'; import { @@ -32,7 +32,7 @@ describe('Asset resolver', () => { const { server, adminClient } = createTestEnvironment( mergeConfig(testConfig, { assetOptions: { - permittedFileTypes: ['image/*', '.pdf'], + permittedFileTypes: ['image/*', '.pdf', '.zip'], }, }), ); @@ -205,6 +205,34 @@ describe('Asset resolver', () => { ]); }); + // https://github.com/vendure-ecommerce/vendure/issues/727 + it('file extension with shared type', async () => { + const filesToUpload = [path.join(__dirname, 'fixtures/assets/dummy.zip')]; + const { createAssets }: CreateAssets.Mutation = await adminClient.fileUploadMutation({ + mutation: CREATE_ASSETS, + filePaths: filesToUpload, + mapVariables: filePaths => ({ + input: filePaths.map(p => ({ file: null })), + }), + }); + + expect(createAssets.length).toBe(1); + + expect(isAsset(createAssets[0])).toBe(true); + const results = createAssets.filter(isAsset); + expect(results.map(a => omit(a, ['id']))).toEqual([ + { + fileSize: 1680, + focalPoint: null, + mimeType: 'application/zip', + name: 'dummy.zip', + preview: 'test-url/test-assets/dummy__preview.zip.png', + source: 'test-url/test-assets/dummy.zip', + type: 'BINARY', + }, + ]); + }); + it('not permitted type', async () => { const filesToUpload = [path.join(__dirname, 'fixtures/assets/dummy.txt')]; const { createAssets }: CreateAssets.Mutation = await adminClient.fileUploadMutation({ diff --git a/packages/core/src/service/services/asset.service.ts b/packages/core/src/service/services/asset.service.ts index 25753a5a20..788265d508 100644 --- a/packages/core/src/service/services/asset.service.ts +++ b/packages/core/src/service/services/asset.service.ts @@ -374,9 +374,12 @@ export class AssetService { private validateMimeType(mimeType: string): boolean { const [type, subtype] = mimeType.split('/'); - const typeMatch = this.permittedMimeTypes.find(t => t.type === type); - if (typeMatch) { - return typeMatch.subtype === subtype || typeMatch.subtype === '*'; + const typeMatches = this.permittedMimeTypes.filter(t => t.type === type); + + for (const match of typeMatches) { + if (match.subtype === subtype || match.subtype === '*') { + return true; + } } return false; }