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
| Comment | File | Size | Author |
|---|---|---|---|
| #10 | issue-3345450.png | 105.02 KB | yovanny.gomez.oyola |
| #8 | 3345450-8-restrict-zero-byte-file-upload.patch | 1.62 KB | asmasiddiquiCG |
Issue fork drupal-3345450
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
Comment #2
asmasiddiquiCG commentedThis patch adds a validator for checking file size and returns error to user if the file is blank
Comment #3
cilefen commentedComment #4
asmasiddiquiCG commentedFixed the spell checks and wide spacing
Comment #5
asmasiddiquiCG commentedComment #7
asmasiddiquiCG commentedComment #8
asmasiddiquiCG commentedResolved test errors and resubmitting the patch
Comment #9
asmasiddiquiCG commentedComment #10
yovanny.gomez.oyola commentedHi @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?
Comment #11
smustgrave commentedDid not test.
But this will require a test case showing the bug.
Comment #12
cilefen commentedThis is reported as having been fixed in #2377747: Incorrect node create validation error when an invalid image is attached to a field. Or, is this different?
Comment #13
yovanny.gomez.oyola commentedHi @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.
Comment #14
asmasiddiquiCG commentedI 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 (...)
)
Comment #16
kim.pepperSince 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.phpas an example of the validating the maximum filesize.Comment #17
kim.pepperComment #20
ameymudras commented@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
Comment #21
smustgrave commentedWas previously tagged for tests which are still needed too.
Comment #23
phthlaap commentedIt seems to conflict with a test in the JSONAPI module. They expected the zero-byte file to upload successfully.
\Drupal\Tests\jsonapi\Functional\FileUploadTest::testFileUploadZeroByteFileNeed someone review and decide postponed or not.
Comment #24
smustgrave commentedSeems to have test failures.
Have not reviewed.
Comment #25
phthlaap commentedAs 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