Skip to content

Commit

Permalink
EZEE-1451: Simplify mime type configuration (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
webhdx authored and lserwatka committed Apr 11, 2017
1 parent 7ab1b52 commit 96f00b3
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 92 deletions.
20 changes: 10 additions & 10 deletions bundle/ApplicationConfig/Providers/ContentTypeMappings.php
Expand Up @@ -61,30 +61,30 @@ public function getConfig()
'mappings' => [],
];

foreach ($locationConfiguration['mappings'] as $mapping) {
$structure['locationMappings'][$locationIdentifier]['mappings'][] = $this->buildMappingStructure($mapping);
foreach ($locationConfiguration['mappings'] as $mappingGroup) {
$structure['locationMappings'][$locationIdentifier]['mappings'][] = $this->buildMappingGroupStructure($mappingGroup);
}
}

foreach ($this->defaultMappings as $mapping) {
$structure['defaultMappings'][] = $this->buildMappingStructure($mapping);
foreach ($this->defaultMappings as $mappingGroup) {
$structure['defaultMappings'][] = $this->buildMappingGroupStructure($mappingGroup);
}

return $structure;
}

/**
* @param array $mapping
* @param array $mappingGroup
*
* @return array
*/
private function buildMappingStructure(array $mapping)
private function buildMappingGroupStructure(array $mappingGroup)
{
return [
'mimeType' => $mapping['mime_type'],
'contentTypeIdentifier' => $mapping['content_type_identifier'],
'contentFieldIdentifier' => $mapping['content_field_identifier'],
'nameFieldIdentifier' => $mapping['name_field_identifier'],
'mimeTypes' => $mappingGroup['mime_types'],
'contentTypeIdentifier' => $mappingGroup['content_type_identifier'],
'contentFieldIdentifier' => $mappingGroup['content_field_identifier'],
'nameFieldIdentifier' => $mappingGroup['name_field_identifier'],
];
}

Expand Down
45 changes: 25 additions & 20 deletions bundle/DependencyInjection/Configuration.php
Expand Up @@ -34,7 +34,26 @@ public function getConfigTreeBuilder()
->end()
->children()
->arrayNode('location_mappings')
->info('Let\'s you assign mappings bound to location')
->info('Let\'s you assign mappings bound to a location')
->example([
[
'content_type_identifier' => 'gallery',
'mime_type_filter' => [
'image/*',
],
'mappings' => [
[
'mime_types' => [
'image/jpeg',
'image/png',
],
'content_type_identifier' => 'image',
'content_field_identifier' => 'image',
'name_field_identifier' => 'name',
],
],
],
])
->prototype('array')
->children()
->scalarNode('content_type_identifier')
Expand All @@ -51,9 +70,10 @@ public function getConfigTreeBuilder()
->cannotBeEmpty()
->prototype('array')
->children()
->scalarNode('mime_type')
->arrayNode('mime_types')
->isRequired()
->cannotBeEmpty()
->prototype('scalar')->end()
->end()
->scalarNode('content_type_identifier')
->isRequired()
Expand All @@ -77,9 +97,10 @@ public function getConfigTreeBuilder()
->info('These mappings are used as a fallback in case there are no entries under `locations` key')
->prototype('array')
->children()
->scalarNode('mime_type')
->arrayNode('mime_types')
->isRequired()
->cannotBeEmpty()
->prototype('scalar')->end()
->end()
->scalarNode('content_type_identifier')
->isRequired()
Expand All @@ -98,22 +119,6 @@ public function getConfigTreeBuilder()
->end()
->arrayNode('fallback_content_type')
->info('This content type will be used for files with no mime type mapping')
->validate()
->always(function ($v) {
if (
empty($v['content_type_identifier'])
|| empty($v['content_field_identifier'])
) {
$v = [
'content_type_identifier' => null,
'content_field_identifier' => null,
'name_field_identifier' => null,
];
}

return $v;
})
->end()
->children()
->scalarNode('content_type_identifier')
->defaultNull()
Expand All @@ -122,7 +127,7 @@ public function getConfigTreeBuilder()
->defaultNull()
->end()
->scalarNode('name_field_identifier')
->defaultValue('name') // should work for most content types
->defaultNull()
->end()
->end()
->end()
Expand Down
50 changes: 29 additions & 21 deletions bundle/Resources/config/default_settings.yml
@@ -1,29 +1,37 @@
parameters:
ez_systems.multifile_upload.rest.path_prefix: /api/multifileupload/v1

ez_systems.multifile_upload.location_mappings: []

ez_systems.multifile_upload.default_mappings:
- { mime_type: image/jpeg, content_type_identifier: image, content_field_identifier: image }
- { mime_type: image/jpg, content_type_identifier: image, content_field_identifier: image }
- { mime_type: image/pjpeg, content_type_identifier: image, content_field_identifier: image }
- { mime_type: image/pjpg, content_type_identifier: image, content_field_identifier: image }
- { mime_type: image/pjpg, content_type_identifier: image, content_field_identifier: image }
- { mime_type: image/png, content_type_identifier: image, content_field_identifier: image }
- { mime_type: image/bmp, content_type_identifier: image, content_field_identifier: image }
- { mime_type: image/gif, content_type_identifier: image, content_field_identifier: image }
- { mime_type: image/tiff, content_type_identifier: image, content_field_identifier: image }
- { mime_type: image/x-icon, content_type_identifier: image, content_field_identifier: image }
- { mime_type: image/svg+xml, content_type_identifier: image, content_field_identifier: image }
- { mime_type: image/webp, content_type_identifier: image, content_field_identifier: image }
- { mime_type: application/msword, content_type_identifier: file, content_field_identifier: file }
- { mime_type: application/vnd.openxmlformats-officedocument.wordprocessingml.document, content_type_identifier: file, content_field_identifier: file }
- { mime_type: application/vnd.ms-excel, content_type_identifier: file, content_field_identifier: file }
- { mime_type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, content_type_identifier: file, content_field_identifier: file }
- { mime_type: application/vnd.ms-powerpoint, content_type_identifier: file, content_field_identifier: file }
- { mime_type: application/vnd.openxmlformats-officedocument.presentationml.presentation, content_type_identifier: file, content_field_identifier: file }
- { mime_type: application/pdf, content_type_identifier: file, content_field_identifier: file }
- # image
mime_types:
- image/jpeg
- image/jpg
- image/pjpeg
- image/pjpg
- image/png
- image/bmp
- image/gif
- image/tiff
- image/x-icon
- image/webp
content_type_identifier: image
content_field_identifier: image
name_field_identifier: name
- # file
mime_types:
- image/svg+xml
- application/msword
- application/vnd.openxmlformats-officedocument.wordprocessingml.document
- application/vnd.ms-excel
- application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
- application/vnd.ms-powerpoint
- application/vnd.openxmlformats-officedocument.presentationml.presentation
- application/pdf
content_type_identifier: file
content_field_identifier: file
name_field_identifier: name

ez_systems.multifile_upload.fallback_content_type:
content_type_identifier: file
content_field_identifier: file
name_field_identifier: name
3 changes: 2 additions & 1 deletion bundle/Resources/public/css/layouts/views/upload-popup.css
Expand Up @@ -55,12 +55,13 @@
}

.mfu-popup-opened .mfu-popup-overlay {
position: absolute;
position: fixed;
top: 0;
bottom: 0;
left: 0;
right: 0;
z-index: 10000;
transform: translate3d(0,0,0);
}

.ez-view-mfuuploadpopupview.mfu-popup--hidden {
Expand Down
14 changes: 7 additions & 7 deletions bundle/Resources/public/js/plugins/mfu-fileupload-plugin.js
Expand Up @@ -277,13 +277,13 @@ YUI.add('mfu-fileupload-plugin', function (Y) {
const locationIdentifier = this.get('host').get('contentType').get('identifier');
const locationMappings = this.get('contentTypeByLocationMappings');
const mappedLocation = locationMappings.find(item => item.contentTypeIdentifier === locationIdentifier);
const uniqueIdentifiers = mappedLocation ?
[...new Set(mappedLocation.mappings.map(item => item.contentTypeIdentifier))] :
[];
let promises = [this._loadContentTypeByIdentifier(defaultContentTypeIdentifier)];
const uniqueIdentifiers = mappedLocation ? [...new Set(mappedLocation.mappings.map(item => item.contentTypeIdentifier))] : [];
let promises = [];

if (uniqueIdentifiers.length) {
promises = uniqueIdentifiers.map(identifier => this._loadContentTypeByIdentifier(identifier));
} else {
promises = [this._loadContentTypeByIdentifier(defaultContentTypeIdentifier)];
}

Promise.all(promises)
Expand Down Expand Up @@ -498,7 +498,7 @@ YUI.add('mfu-fileupload-plugin', function (Y) {
return true;
}

return !!locationMapping.mappings.find(item => item.mimeType === file.type);
return !!this._findMimeTypeMapping(locationMapping.mappings, file);
},

/**
Expand Down Expand Up @@ -538,9 +538,9 @@ YUI.add('mfu-fileupload-plugin', function (Y) {
* @protected
* @param mappings {Array} list of mappings
* @param file {File} File object
* @return {Boolean}
* @return {Object}
*/
_findMimeTypeMapping: (mappings, file) => mappings.find(item => item.mimeType === file.type),
_findMimeTypeMapping: (mappings, file) => mappings.find(item => item.mimeTypes.find(type => type === file.type)),

/**
* Resolves/rejects a promise
Expand Down
Expand Up @@ -20,46 +20,48 @@ public function testGetConfig()
],
'mappings' => [
[
'mime_type' => 'application/msword',
'content_type_identifier' => 'file',
'content_field_identifier' => 'file',
],
[
'mime_type' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
'content_type_identifier' => 'file',
'content_field_identifier' => 'file',
'mime_types' => [
'image/jpeg',
'image/png',
],
'content_type_identifier' => 'image',
'content_field_identifier' => 'image',
'name_field_identifier' => 'name',
],
],
],
[
'content_type_identifier' => 3,
'mime_type_filter' => [
'video/*',
'application/msword',
],
'mappings' => [
[
'mime_type' => 'application/msword',
'mime_types' => [
'application/msword'
],
'content_type_identifier' => 'file',
'content_field_identifier' => 'file',
'name_field_identifier' => 'name',
],
],
],
];
$defaultMappings = [
[
'mime_type' => 'application/msword',
'content_type_identifier' => 'file',
'content_field_identifier' => 'file',
],
[
'mime_type' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
'mime_types' => [
'application/msword',
'application/vnd.openxmlformats-officedocument.wordprocessingml.document'
],
'content_type_identifier' => 'file',
'content_field_identifier' => 'file',
'name_field_identifier' => 'name',
],
];
$fallbackContentType = [
'content_type_identifier' => 'file',
'content_field_identifier' => 'file',
'name_field_identifier' => 'name',
];
$maxFileSize = 64000000;

Expand All @@ -80,51 +82,53 @@ public function testGetConfig()
],
'mappings' => [
[
'mimeType' => 'application/msword',
'contentTypeIdentifier' => 'file',
'contentFieldIdentifier' => 'file',
],
[
'mimeType' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
'contentTypeIdentifier' => 'file',
'contentFieldIdentifier' => 'file',
'mimeTypes' => [
'image/jpeg',
'image/png',
],
'contentTypeIdentifier' => 'image',
'contentFieldIdentifier' => 'image',
'nameFieldIdentifier' => 'name',
],
],
],
[
'contentTypeIdentifier' => 3,
'mimeTypeFilter' => [
'video/*',
'application/msword',
],
'mappings' => [
[
'mimeType' => 'application/msword',
'mimeTypes' => [
'application/msword',
],
'contentTypeIdentifier' => 'file',
'contentFieldIdentifier' => 'file',
'nameFieldIdentifier' => 'name',
],
],
],
],
'defaultMappings' => [
[
'mimeType' => 'application/msword',
'contentTypeIdentifier' => 'file',
'contentFieldIdentifier' => 'file',
],
[
'mimeType' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
'mimeTypes' => [
'application/msword',
'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
],
'contentTypeIdentifier' => 'file',
'contentFieldIdentifier' => 'file',
'nameFieldIdentifier' => 'name',
],
],
'fallbackContentType' => [
'contentTypeIdentifier' => 'file',
'contentFieldIdentifier' => 'file',
'nameFieldIdentifier' => 'name',
],
'maxFileSize' => 64000000,
];

$this->assertArraySubset($contentTypeMappingsConfig->getConfig(), $expectedArray);
$this->assertArraySubset($expectedArray, $contentTypeMappingsConfig->getConfig());
}

public function testGetConfigWithEmptyValues()
Expand All @@ -134,6 +138,7 @@ public function testGetConfigWithEmptyValues()
$fallbackContentType = [
'content_type_identifier' => null,
'content_field_identifier' => null,
'name_field_identifier' => null,
];
$maxFileSize = null;

Expand All @@ -145,6 +150,7 @@ public function testGetConfigWithEmptyValues()
'fallbackContentType' => [
'contentTypeIdentifier' => null,
'contentFieldIdentifier' => null,
'nameFieldIdentifier' => null,
],
'maxFileSize' => null,
];
Expand Down

0 comments on commit 96f00b3

Please sign in to comment.