Problem/Motivation

Whilst working on #1927648: Allow creation of file entities from binary data via REST requests, I realised that the code in \Drupal\file\Entity\File::preSave does not allow a file with zero bytes/length to be set. This does against how numeric fields check their emptiness. The offending code:

if ($size = @filesize($uri)) {
  $this->setSize($size);      
}                             

So if filesize is legitimately 0, it will set no value at all.

Proposed resolution

Check numeric emptiness properly and set for all cases, unless the filesize call fails.

Remaining tasks

Reviews

User interface changes

N/A

API changes

N/A

Data model changes

Some additional field values of 0 being saved if an empty file is saved.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

damiankloip created an issue. See original summary.

damiankloip’s picture

damiankloip’s picture

Could do with a comment above the !== FALSE check I think.

The last submitted patch, 2: 2901998-tests-only-FAIL.patch, failed testing. View results

kim.pepper’s picture

Added a comment.

andypost’s picture

Status: Needs review » Reviewed & tested by the community

Looks good to go

Anonymous’s picture

+1. File with zero size is legal and can be useful. Examples:

  • empty key.file to confirm the affiliation of the site when using a third-party service
  • file with data in property
  • file with provide template name
  • stub file (like empty index.html in folder for prevent view other files)

Maybe add CR for warn developers, who used this bug as a feature for filtering files? It is also possible to add the checkbox "Prevent upload empty files" in the file system settings (/admin/config/media/file-system)?

Wim Leers’s picture

Component: file system » file.module

+1, and +1 for the great comment by @vaplas.

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 5: 2901998-5.patch, failed testing. View results

Wim Leers’s picture

Status: Needs work » Reviewed & tested by the community
Issue tags: +API-First Initiative, +blocker, +Entity validation

Random fail in the JS test SettingsTrayBlockFormTest. Retesting.

Anonymous’s picture

Status: Reviewed & tested by the community » Needs review
FileSize
3.72 KB
4.14 KB

#8: thank you, @Wim Leers!

This patch contains second part of #7 about checkbox "Prevent upload empty files".
Also bit change test. Now it is separated, and it checks three configuration cases (default/allow/prevent save empty file).

Feel free to ignore, if it is not what we should do in this issue. Back to review due to this patch.

Wim Leers’s picture

Assigned: Unassigned » Berdir

Assigning to Berdir to get his thoughts on whether it's necessary to have the abc layer that #11 is adding.

Status: Needs review » Needs work

The last submitted patch, 11: 2901998-11.patch, failed testing. View results

Anonymous’s picture

Status: Needs work » Needs review
FileSize
5.08 KB
1.37 KB

Thanks!
A small correction to not keep the red issue.

damiankloip’s picture

I agree that is a good idea worth dicussing but I'm sorry, this seems to be opening up the scope of this fix a little too much IMO. We're now checking some global config in the preSave of the file entity. I'm not convinced there are really any real world examples of people relying on there being no field value for empty files still... Maybe berdir has some feedback on that.

Empty file is legal, but you can prevent save it.

This doesn't seem accurate, all that's happening here is we're saving the file size or not. This implies the file will not be saved at all. So for me, that means the 'prevent_save_empty_file' config value is also not named accurately.

  1. empty key.file to confirm the affiliation of the site when using a third-party service
  2. file with data in property
  3. file with provide template name
  4. stub file (like empty index.html in folder for prevent view other files)

Great examples BTW :)

Anonymous’s picture

Assigned: Berdir » Unassigned
Status: Needs review » Reviewed & tested by the community
FileSize
1.45 KB

Thank you, @damiankloip! I'm really wrong. Back to #10 state with #5 patch. Sorry for noise.

  • catch committed 7b4861f on 8.5.x
    Issue #2901998 by vaplas, damiankloip, kim.pepper: File size of 0 is not...

  • catch committed 2045f3a on 8.4.x
    Issue #2901998 by vaplas, damiankloip, kim.pepper: File size of 0 is not...

catch credited catch.

catch’s picture

Version: 8.5.x-dev » 8.4.x-dev
Status: Reviewed & tested by the community » Fixed

Committed/pushed to 8.5.x and cherry-picked to 8.4.x. Thanks!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.