Extractors for mimetype & filesize would be useful for all local file media.
(Any else?)

Before (image source)

After (image and file sources, respectively)

Comments

axel.rutz created an issue. See original summary.

geek-merlin’s picture

Status: Active » Needs review
StatusFileSize
new1.27 KB

Trivial patch flying in, untested yet.

geek-merlin’s picture

Works like a charm here.

geek-merlin’s picture

jeetendrakumar’s picture

Hi Axel,

Thanks for the initial patch!

I think we have a function to get the file size getSize(), So we can use it

@@ -40,6 +43,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 filesize($file->getFileUri());

What do you think?

geek-merlin’s picture

StatusFileSize
new1.26 KB

Yes, i simply missed \Drupal\file\Entity\File::getSize...
So we won't need to stat().
Here we go!

chr.fritsch’s picture

Status: Needs review » Needs work
Issue tags: +Needs tests, +Media Initiative

Looks good.

One small thing:

+++ b/core/modules/media/src/Plugin/media/Source/File.php
@@ -26,7 +26,10 @@ class File extends MediaSourceBase {
+      'mimetype' => $this->t('MIME-Type'),
+      'filesize' => $this->t('File size'),

To be consistent with the image source, we should add this keys as class constants and use them instead.

Also, we should add a small test for that in Drupal\Tests\media\FunctionalJavascript\MediaSourceFileTest

balsama’s picture

Assigned: Unassigned » balsama
balsama’s picture

Assigned: balsama » Unassigned
Status: Needs work » Needs review
StatusFileSize
new3.89 KB
new3.37 KB

Addressed #8.

phenaproxima’s picture

Status: Needs review » Reviewed & tested by the community

Seems legit.

xjm’s picture

+++ b/core/modules/media/src/Plugin/media/Source/File.php
@@ -22,11 +22,29 @@
+      static::METADATA_ATTRIBUTE_MIME => $this->t('MIME-Type'),

I don't think this should be hyphenated nor have "Type" capitalized. Is there a place that we got this from?

phenaproxima’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: -Needs tests

Fair enough.

balsama’s picture

Status: Needs work » Needs review
StatusFileSize
new3.89 KB

That came from thin air as far as I know. Changed there and in the const comment (which was also inconsistent). Thanks!

balsama’s picture

StatusFileSize
new712 bytes
phenaproxima’s picture

Status: Needs review » Reviewed & tested by the community

Cool beans. RTBC once Drupal CI is OK with it.

xjm’s picture

Issue summary: View changes
StatusFileSize
new54.69 KB
new46.86 KB
new55.24 KB
+++ b/core/modules/media/src/Plugin/media/Source/File.php
@@ -40,6 +58,12 @@ public function getMetadata(MediaInterface $media, $attribute_name) {
+      case 'mimetype':
+        return $file->getMimeType();
+
+      case 'filesize':
+        return $file->getSize();

So the deal is that the File entity already provides the API for this, and all we're doing in this patch is exposing them for Media.

I was going to suggest a followup exposing the metadata to views, but one can already get this just by adding a relationship to the file entity in the view.

Screenshots!

Before (image source)

After (image and file sources, respectively)

  • xjm committed 3d0e79f on 8.5.x
    Issue #2928798 by balsama, axel.rutz, xjm, phenaproxima, chr.fritsch,...
xjm’s picture

Status: Reviewed & tested by the community » Fixed

Committed and pushed to 8.5.x. Thanks!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.