Ver Fonte

fix(core): Allow asset uploads with same major mime type

Fixes #727
Michael Bromley há 4 anos atrás
pai
commit
070c5f22fe

+ 30 - 2
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({

+ 6 - 3
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;
     }