public function updateSourceField(
    MediaInterface $media,
    $resource,
    $mimetype
  ) {
    $source_field = $this->getSourceFieldName($media->bundle());
    $file = $this->getSourceFile($media);

    // Update it.
    $this->updateFile($file, $resource, $mimetype);
    $file->save();

    // Set fields provided by type plugin and mapped in bundle configuration
    // for the media.
    foreach ($media->bundle->entity->getFieldMap() as $source => $destination) {
      if ($media->hasField($destination) && $value = $media->getSource()->getMetadata($media, $source)) {
        $media->set($destination, $value);
      }
      // Ensure width and height are updated on File reference when it's an
      // image. Otherwise you run into scaling problems when updating images
      // with different sizes.
      if ($source == 'width' || $source == 'height') {
        $media->get($source_field)->first()->set($source, $value);
      }
    }

    $media->save();
  }