How to get File entity in media_presave hook? I want to get the file entity out of an media image bundle to the get the filename to check if the image is cmyk or not.

/**
 * Implements hook_ENTITY_TYPE_presave().
 */
function hook_media_presave(MediaInterface $entity) {
  if ($entity-&gt;bundle() == 'image') {
    $field_definitions = $entity-&gt;getFieldDefinitions();
    foreach ($field_definitions as $field_definition) {
      if ($field_definition-&gt;getType() 'image') {
        // get File entity here.
      }
    }
  }
}


How do I do this?
I've found a solution.

/**
 * Implements hook_ENTITY_TYPE_presave().
 */
function hook_media_presave(MediaInterface $entity) {
  if ($entity-&gt;bundle() == 'image') {
    $field_definitions = $entity-&gt;getFieldDefinitions();
    foreach ($field_definitions as $field_definition) {
      if ($field_definition-&gt;getType() == 'image') {
        /** @var FieldItemListInterface $field_value */
        $field_values = $entity-&gt;get($field_definition-&gt;getName())-&gt;getValue();
        if (is_array($field_values)) {
          foreach ($field_values as $field_value) {
            if ($field_value['target_id']) {
              /** @var File $file */
              $file = File::load($field_value['target_id']);
              $image = getimagesize($file-&gt;getFileUri());
              // If the image has channels '4' it is cmyk.
              if ($image['channels'] == '4') {
                $imagick = new Imagick($file-&gt;getFileUri());
                $imagick-&gt;setImageColorspace(Imagick::COLORSPACE_RGB);
                $imagick-&gt;writeImage($image);
                $imagick-&gt;destroy();
              }
            }
          }
        }
      }
    }
  }
}