Commit 3d0e79f2 authored by xjm's avatar xjm

Issue #2928798 by balsama, axel.rutz, xjm, phenaproxima, chr.fritsch,...

Issue #2928798 by balsama, axel.rutz, xjm, phenaproxima, chr.fritsch, jeetendrakumar: Add extractors: mimetype & filesize
parent db680529
......@@ -22,11 +22,29 @@
*/
class File extends MediaSourceBase {
/**
* Key for "MIME type" metadata attribute.
*
* @var string
*/
const METADATA_ATTRIBUTE_MIME = 'mimetype';
/**
* Key for "File size" metadata attribute.
*
* @var string
*/
const METADATA_ATTRIBUTE_SIZE = 'filesize';
/**
* {@inheritdoc}
*/
public function getMetadataAttributes() {
return [];
return [
static::METADATA_ATTRIBUTE_MIME => $this->t('MIME type'),
static::METADATA_ATTRIBUTE_SIZE => $this->t('File size'),
];
}
/**
......@@ -40,6 +58,12 @@ public function getMetadata(MediaInterface $media, $attribute_name) {
return parent::getMetadata($media, $attribute_name);
}
switch ($attribute_name) {
case 'mimetype':
return $file->getMimeType();
case 'filesize':
return $file->getSize();
case 'default_name':
return $file->getFilename();
......
......@@ -3,6 +3,7 @@
namespace Drupal\Tests\media\FunctionalJavascript;
use Drupal\media\Entity\Media;
use Drupal\media\Plugin\media\Source\File;
/**
* Tests the file media source.
......@@ -17,16 +18,32 @@ class MediaSourceFileTest extends MediaSourceTestBase {
public function testMediaFileSource() {
$media_type_id = 'test_media_file_type';
$source_field_id = 'field_media_file';
$provided_fields = [
File::METADATA_ATTRIBUTE_SIZE,
File::METADATA_ATTRIBUTE_MIME,
];
$session = $this->getSession();
$page = $session->getPage();
$assert_session = $this->assertSession();
$this->doTestCreateMediaType($media_type_id, 'file');
$this->doTestCreateMediaType($media_type_id, 'file', $provided_fields);
// Create custom fields for the media type to store metadata attributes.
$fields = [
'field_string_file_size' => 'string',
'field_string_mime_type' => 'string',
];
$this->createMediaTypeFields($fields, $media_type_id);
// Hide the name field widget to test default name generation.
$this->hideMediaTypeFieldWidget('name', $media_type_id);
$this->drupalGet("admin/structure/media/manage/{$media_type_id}");
$page->selectFieldOption("field_map[" . File::METADATA_ATTRIBUTE_SIZE . "]", 'field_string_file_size');
$page->selectFieldOption("field_map[" . File::METADATA_ATTRIBUTE_MIME . "]", 'field_string_mime_type');
$page->pressButton('Save');
$test_filename = $this->randomMachineName() . '.txt';
$test_filepath = 'public://' . $test_filename;
file_put_contents($test_filepath, $this->randomMachineName());
......@@ -50,9 +67,11 @@ public function testMediaFileSource() {
$page->checkField('revision');
$assert_session->elementAttributeNotContains('css', '.field--name-revision-log-message', 'style', 'display');
// Load the media and check if the label was properly populated.
// Load the media and check that all the fields are properly populated.
$media = Media::load(1);
$this->assertEquals($test_filename, $media->getName());
$this->assertEquals('8', $media->get('field_string_file_size')->value);
$this->assertEquals('text/plain', $media->get('field_string_mime_type')->value);
// Test the MIME type icon.
$icon_base = \Drupal::config('media.settings')->get('icon_base_uri');
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment