Skip to content

Commit

Permalink
fix(core): Allow asset uploads with same major mime type
Browse files Browse the repository at this point in the history
Fixes #727
  • Loading branch information
michaelbromley committed Feb 26, 2021
1 parent 50aafb3 commit 070c5f2
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 5 deletions.
32 changes: 30 additions & 2 deletions packages/core/e2e/asset.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -32,7 +32,7 @@ describe('Asset resolver', () => {
const { server, adminClient } = createTestEnvironment(
mergeConfig(testConfig, {
assetOptions: {
permittedFileTypes: ['image/*', '.pdf'],
permittedFileTypes: ['image/*', '.pdf', '.zip'],
},
}),
);
Expand Down Expand Up @@ -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({
Expand Down
9 changes: 6 additions & 3 deletions packages/core/src/service/services/asset.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit 070c5f2

Please sign in to comment.