FileValidationEvent may not report the correct file size

Created on 2 May 2025, about 1 month ago

Problem/Motivation

When uploading an image that is larger than constraints and the image is resized, the file size reported in FileValidationEvent does not match the actual file size.

The issue was originally reported in https://www.drupal.org/project/drupal/issues/3292350 🐛 file_validate_image_resolution does not update file size after resizing Needs work , but was fixed for hook_file_validate, but I think this issue appeared again with this event.

Steps to reproduce

ClamAV contrib module switched from hook_file_validate to FileValidationEvent and it appears that in some cases, the file scanning logic now breaks.

If we log file size (uploading test PNG file that will be resized) as reported by the FileInterface in both FileValidationEvent and hook_file_validate, it seems they return different data.

$file_size = filesize($file->getFileUri());
\Drupal::logger('File validate')->error('Size of file is: ' . $file->getSize() . ', URI is: ' . $file->getFileUri() . ' file size is: ' . $file_size);

Using hook_file_validate, this logs

Size of file is: 387365, URI is: /tmp/phpzipzoE file size is: 387365

but using FileValidationEvent logs

Size of file is: 387365, URI is: /tmp/phpuX8xcs file size is: 506707

🐛 Bug report
Status

Active

Version

11.1 🔥

Component

image system

Created by

🇸🇮Slovenia KlemenDEV

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

Comments & Activities

Production build 0.71.5 2024