Problem/Motivation

Its a requirement in our application that accepts user files (txt,docx,pdf) , to restrict users from uploading files that are empty or blank which makes the file size zero.
Such file upload must not be accepted and user must be displayed an error for uploading empty files.
I have searched for the issues in file module, but was unable to find anything that matches our requirement.

Steps to reproduce

Proposed resolution

If an additional validator could be added in the file module that checks if the file size is zero and returns error to user.

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

Issue fork drupal-3345450

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

asmasiddiquiCG created an issue. See original summary.

asmasiddiquiCG’s picture

This patch adds a validator for checking file size and returns error to user if the file is blank

cilefen’s picture

Version: 9.4.x-dev » 10.1.x-dev
asmasiddiquiCG’s picture

StatusFileSize
new1.65 KB

Fixed the spell checks and wide spacing

asmasiddiquiCG’s picture

Status: Active » Needs review

Status: Needs review » Needs work

The last submitted patch, 4: 3345450-4-restrict-zero-byte-file-upload.patch, failed testing. View results

asmasiddiquiCG’s picture

Issue summary: View changes
asmasiddiquiCG’s picture

StatusFileSize
new1.62 KB

Resolved test errors and resubmitting the patch

asmasiddiquiCG’s picture

Status: Needs work » Needs review
yovanny.gomez.oyola’s picture

StatusFileSize
new105.02 KB

Hi @asmasiddiquiCG thanks for the patch.

I have validated it and it works fine on:
Drupal core version: 10.0.4
PHP version: 8.1.14
Mysql version: 8.0.31

The result is as expected. But I see that the test keeps failing. Can you validate it?

smustgrave’s picture

Status: Needs review » Needs work
Issue tags: +Needs Review Queue Initiative, +Needs tests, +Needs subsystem maintainer review

Did not test.

But this will require a test case showing the bug.

yovanny.gomez.oyola’s picture

Hi @cilefen,

In the ticket you mention, only the error message is being changed when the !$image->isValid() function fails. But in this case what we want to validate is that the user cannot upload empty files to the CMS. I think they are 2 different things.

asmasiddiquiCG’s picture

I am unable to solve the error thrown in patch test, can someone guide?

Error below
1) Drupal\Tests\jsonapi\Functional\MediaTest::testPostIndividual
The 'errors' member was not as expected.
Failed asserting that two arrays are equal.
--- Expected
+++ Actual
@@ @@
0 => Array (
0 => '422'
1 => 'Unprocessable Content'
- 2 => 'name: Name: this field cannot hold more than 1 values.'
+ 2 => 'field_media_file.0: The file you uploaded is empty! Please check whether you want to upload the selected file'
3 => Array (
- 0 => '/data/attributes/name'
+ 0 => '/data/attributes/field_media_file/0'
)
)
+ 1 => Array (...)
)

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

kim.pepper’s picture

Since UploadedFile validator was added in #3375447: Create an UploadedFile validator and deprecate error checking methods on UploadedFileInterface you need to create a plugin and add the additional validator to your upload form settings.

See core/modules/file/src/Plugin/Validation/Constraint/FileSizeLimitConstraint.php as an example of the validating the maximum filesize.

kim.pepper’s picture

ameymudras made their first commit to this issue’s fork.

ameymudras’s picture

Status: Needs work » Needs review

@kim.pepper thanks for the suggestion. Ive added a validation constraint to check for an empty file. Appreciate if you could review and suggest changes / issues if any.

It still Needs tests though

smustgrave’s picture

Status: Needs review » Needs work

Was previously tagged for tests which are still needed too.

phthlaap made their first commit to this issue’s fork.

phthlaap’s picture

Status: Needs work » Needs review

It seems to conflict with a test in the JSONAPI module. They expected the zero-byte file to upload successfully.

\Drupal\Tests\jsonapi\Functional\FileUploadTest::testFileUploadZeroByteFile

Need someone review and decide postponed or not.

smustgrave’s picture

Status: Needs review » Needs work
Issue tags: -Needs tests

Seems to have test failures.

Have not reviewed.

phthlaap’s picture

As I mentioned in comment #23, the Json API module has a test to ensure that a file with zero bytes can be uploaded successfully. That test conflicts with this feature request, and I am unsure about what steps to take next.

\Drupal\Tests\jsonapi\Functional\FileUploadTest::testFileUploadZeroByteFile

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.