Commit 0b260c50 authored by mark burdett's avatar mark burdett
Browse files

Issue #3309518 by JordanDukart, mfb: Endless loop possible when file URI does not exist

parent 170e9e53
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -9,7 +9,7 @@
        "source": "https://git.drupalcode.org/project/filehash"
    },
    "require": {
        "drupal/core": "^8.5 || ^9.0 || ^10.0"
        "drupal/core": "^8.6 || ^9.0 || ^10.0"
    },
    "authors": [
        {
+1 −1
Original line number Diff line number Diff line
@@ -4,5 +4,5 @@ description: Generate hashes for each uploaded file.
name: File Hash
type: module
dependencies:
  - drupal:file (>=8.5.0)
  - drupal:file (>=8.6.0)
core_version_requirement: ^8 || ^9 || ^10
+12 −0
Original line number Diff line number Diff line
<?php

/**
 * @file
 * Post update functions for File Hash module.
 */

/**
 * Add filehash.memory_cache as service argument.
 */
function filehash_post_update_add_memory_cache(): void {
}
+4 −1
Original line number Diff line number Diff line
@@ -8,4 +8,7 @@ services:

  filehash:
    class: Drupal\filehash\FileHash
    arguments: ['@config.factory', '@current_user', '@entity.definition_update_manager', '@entity_type.manager']
    arguments: ['@config.factory', '@current_user', '@entity.definition_update_manager', '@entity_type.manager', '@filehash.memory_cache']

  filehash.memory_cache:
    class: Drupal\Core\Cache\MemoryCache\MemoryCache
+15 −4
Original line number Diff line number Diff line
@@ -2,6 +2,7 @@

namespace Drupal\filehash;

use Drupal\Core\Cache\MemoryCache\MemoryCacheInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Database\DatabaseExceptionWrapper;
use Drupal\Core\Entity\EntityTypeManagerInterface;
@@ -80,6 +81,13 @@ class FileHash implements FileHashInterface {
   */
  protected $entityTypeManager;

  /**
   * The memory cache.
   *
   * @var \Drupal\Core\Cache\MemoryCache\MemoryCacheInterface
   */
  protected $memoryCache;

  /**
   * Constructs the File Hash service.
   *
@@ -91,12 +99,15 @@ class FileHash implements FileHashInterface {
   *   The entity type manager.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   * @param \Drupal\Core\Cache\MemoryCache\MemoryCacheInterface $memory_cache
   *   The memory cache.
   */
  public function __construct(ConfigFactoryInterface $config_factory, AccountInterface $current_user, EntityDefinitionUpdateManagerInterface $entity_definition_update_manager, EntityTypeManagerInterface $entity_type_manager) {
  public function __construct(ConfigFactoryInterface $config_factory, AccountInterface $current_user, EntityDefinitionUpdateManagerInterface $entity_definition_update_manager, EntityTypeManagerInterface $entity_type_manager, MemoryCacheInterface $memory_cache) {
    $this->configFactory = $config_factory;
    $this->currentUser = $current_user;
    $this->entityDefinitionUpdateManager = $entity_definition_update_manager;
    $this->entityTypeManager = $entity_type_manager;
    $this->memoryCache = $memory_cache;
  }

  /**
@@ -200,9 +211,9 @@ class FileHash implements FileHashInterface {
    // @todo Add a setting to toggle the auto-hash behavior?
    foreach ($files as $file) {
      foreach ($this->columns() as $column) {
        if (!$file->{$column}->value && $this->shouldHash($file)) {
          $file->original = clone($file);
          // Entity post-save will clean up the dangling "original" property.
        if (!$file->{$column}->value && $this->shouldHash($file) && !$this->memoryCache->get($file->id())) {
          // To avoid endless loops, auto-hash each file once per execution.
          $this->memoryCache->set($file->id(), TRUE);
          $file->save();
          break;
        }
Loading