File system can get in a state where files with certain names cannot be uploaded

Created on 17 April 2023, about 1 year ago
Updated 15 May 2023, about 1 year ago

Problem/Motivation

If a file is in the file_managed table but the file is not present in the filesystem, it is impossible to upload a temporary file with the same name.

Steps to reproduce

  • Install standard.
  • Create a basic page.
  • Upload an image into the Body field and save the page.
  • Delete the file from sites/default/files/inline-images.
  • Create a new basic page.
  • Insert an image with the same name of the filename just deleted into the body field.

Drupal will display the image in the body field but will then display a message "Couldn't upload file: ." (note clicking the save button is not necessary to trigger the error).

Whilst deleting the file after upload is somewhat contrived, we have hit the scenario on a production system where a file is in file_managed but not in the filesystem possibly due to S3 lag. Users were unable to upload anything with the filename 2.jpg, and it wasn't obvious they could work around the issue by renaming the file.

Proposed resolution

Drupal only attempts to rename files on upload if file_exists() on their proposed new path is true. If a file is missing but still in file_managed, Drupal will not try to rename it, but it will later fail the FileUriUnique constraint, return 422.

If Drupal checked the constraint before deciding to rename the file, it could avoid this predictable and frustrating error.

Remaining tasks

tbd

User interface changes

None

API changes

None

Data model changes

None

🐛 Bug report
Status

Active

Version

10.1

Component
File system 

Last updated about 10 hours ago

Created by

🇬🇧United Kingdom Eli-T Manchester

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

Comments & Activities

  • Issue created by @Eli-T
  • 🇬🇧United Kingdom Eli-T Manchester
  • 🇬🇷Greece hungry_mind

    So, what is the solution to this problem? I've got Drupal 9 and I got this message while trying to upload a jpg file.

  • 🇮🇳India Mahima_Mathur23

    I was able to replicate the issue too.
    Although it will not be feasible to check the filesystem. And in case of using S3, if there is a lag, checking is even not going to be possible.

    As a temporary solution, can we give an error message that is more descriptive like: "Unable to upload the file, please try again with a different file name" or something like this.

Production build 0.69.0 2024