With media entity, file does not move back to public

Created on 18 October 2021, about 3 years ago
Updated 5 September 2023, about 1 year ago

Problem/Motivation

When editing media entities, the corresponding file will move from public to private but never the other way around.

Steps to reproduce

  1. Create image media entity, published
  2. Observe the attached file is in the public directory
  3. Unpublish the media entity
  4. Observe the attached file is in the private directory
  5. Publish the media entity
  6. Observe the attached file is incorrectly still in the private directory

Proposed resolution

Add some custom handling for Media entities

Remaining tasks

Review patch
Tests?
Commit

🐛 Bug report
Status

Postponed: needs info

Version

1.0

Component

Code

Created by

🇳🇿New Zealand danielveza Brisbane, AU

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

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • 🇳🇿New Zealand ericgsmith

    I can reliable reproduce this issue - and it is caused by static caching in file_get_file_references.

    When editing via the UI here is what is happening:

    1. FormBuilder gets the values from all inputs
    2. The managed file widget checks $file->access('download') in \Drupal\file\Element\ManagedFile::valueCallback
    3. If the file is private then the file access control calls file_get_file_usage to determine access. At this point, the save is not complete on the from so the current references are cached in file_get_file_usage.
    4. Then the entity is saved, this module calls file_get_file_usage and gets bad / outdated data

    That check was introduced in SA-CORE-2020-011 so its probably need broken since then.

    Hence - if the file is public then it is correctly moved to private as nothing is cached but it if is private then file_get_file_usage has cached (incorrect) data in it when this module runs.

    The issue is core - file_get_file_usage is statically caching stuff.

    https://www.drupal.org/project/drupal/issues/3035352 📌 [PP-1] Deprecate file_get_file_references(). Move the logic to file.usage service Postponed is where this would be solved by using a memory cache bin in the new service that is cache tag aware... But... its postponed.

    I have half baked patch for core that demonstrates the issue - I'll add it to 3035352.

  • 🇩🇪Germany geek-merlin Freiburg, Germany

    Thx eric for researching and closslinking this!

    Can you put your work in a dedicated issue (instead of hijacking the other)? Feel free to ping me, so i can review and RTBC it.

Production build 0.71.5 2024