Change record status: 
Project: 
Introduced in branch: 
10.2.x
Introduced in version: 
10.2.0
Description: 

Filename sanitization settings

Site builders are now able to opt-in for various forms of filename sanitization on admin/config/media/file-system:

  • Transliterate the filename. Disabled by default, because it's not useful for some languages like Japanese.
  • Replace whitespace with dash (-) or underscore (_).
  • Replace any characters other than 0-9A-Za-z or - or _ or . with dash (-) or underscore (_).
  • Remove duplicate separator characters ie -- or .. or __ with be replaced with - or . or _.
  • Convert to lowercase (to prevent issues on case-insensitive file systems).

Example values for file.settings config settings::

filename_sanitization:
  transliterate: true
  replace_whitespace: '-'
  replace_non_alphanumeric: ' '
  dedupe_separators: true
  lowercase: true

FileUploadSanitizeNameEvent dispatched during file upload

Core now dispatches a \Drupal\file\Event\FileUploadSanitizeNameEvent whenever a file is uploaded (either via the UI or via REST). Event subscribers can modify and sanitize the filename (but not extension) before the file is saved.

Changes to the FileUploadResource plugin constructor

To properly support the new FileUploadSanitizeNameEvent during REST uploads \Drupal\file\Plugin\rest\FileUploadResource::__construct() takes 2 new arguments for additional service dependencies:

 /**
   ...
   * @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $event_dispatcher
   *   The event dispatcher service.
   * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
   *   The language manager service.
   */

These arguments are optional for now, but not defining them is deprecated and any classes that extend FileUploadResource or code that calls the constructor directly should be updated to use proper dependency injection before 11.0.0.

Impacts: 
Site builders, administrators, editors
Module developers
Site templates, recipes and distribution developers

Comments

anybody’s picture

If you're using the transliterate_filenames module, see this issue and please help to switch over: https://www.drupal.org/project/transliterate_filenames/issues/3385940

http://www.DROWL.de || Professionelle Drupal Lösungen aus Ostwestfalen-Lippe (OWL)
http://www.webks.de || webks: websolutions kept simple - Webbasierte Lösungen die einfach überzeugen!
http://www.drupal-theming.com || Individuelle Responsive Themes

jwilson3’s picture

If, on the other hand, you're using the filename_transliteration module, see this issue and please help to switch over https://www.drupal.org/project/filename_transliteration/issues/3238461