Unit mismatch in file insert check

Created on 11 April 2024, 7 months ago

Problem/Motivation

In the hook_ENTITY_TYPE_insert() in the image_resize.module file:

/**
 * Implements hook_ENTITY_TYPE_insert().
 */
function image_resize_file_insert(FileInterface $file) {
  $config = \Drupal::config('image_resize.settings');
  if (in_array($file->getMimeType(), $config->get('mimetypes'), TRUE)) {
    if ($config->get('min_filesize') && $file->getSize() > $config->get('min_filesize')) {
      $queue = \Drupal::service('queue')->get('image_resize');
      $queue->createItem($file->id());
    }
  }
}

$file->getSize() returns the size in bytes however, the configuration setting retrieved by $config->get('min_filesize') specifies file sizes in user-friendly units (KB, MB etc), as indicated in the configuration description:
'#description' => $this->t('Only attempt to resize images above the given file size. Enter a value like "512" (bytes), "80 KB" (kilobytes) or "50 MB" (megabytes) in order to restrict the allowed file size.'),
This mismatch causes improper handling of the file size comparison, leading to incorrect skipped resizing operations.

Steps to reproduce

  1. Go to the config page /admin/config/media/image-resizer.
  2. In the input Minimum filesize write "80 KB".
  3. Go to your content and add an image that is bigger than 80KB.
  4. Check that your image is not in the queue even if it meets the requirements.

Proposed resolution

Convert the value from the config to bytes using Bytes::toNumber for the comparison as done elsewhere in the module.

Remaining tasks

Provide a MR

User interface changes

none

API changes

none

Data model changes

none

🐛 Bug report
Status

Needs review

Version

1.0

Component

Code

Created by

🇪🇸Spain isaacrc

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

Production build 0.71.5 2024