Allow to bulk overwrite or replace existing media

Created on 22 February 2024, about 1 year ago

Problem/Motivation

It would be ideal to handle existing files with the same name by replacing with the newly uploaded media ftp style.

Steps to reproduce

Upload a media file that already exists and drupal will append numerical values to the file instead of replacing it.

Proposed resolution

Add a setting to overwrite/replace files with newer versions. I've made a starting point to jump off from in this patch.

Remaining tasks

User interface changes

API changes

Data model changes

Feature request
Status

Active

Version

3.0

Component

Code

Created by

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

Merge Requests

Comments & Activities

  • Issue created by @ninobrownh20
  • Re-patching for the correct branch

  • Status changed to Needs work 4 months ago
  • 🇳🇱Netherlands Martijn de Wit 🇳🇱 The Netherlands

    There is a patch available. Changing status.

    Would be nice if patch goes into a merge request, so it is visible if patch still applies.

    Changing status to "Needs work".

  • 🇳🇱Netherlands Martijn de Wit 🇳🇱 The Netherlands

    also the use case could be a duplicate off: Prevent duplicates Active

  • First commit to issue fork.
  • 🇬🇧United Kingdom the_g_bomb

    I have created a PR based on the patch supplied in #6 with some coding standards improvements.

    This could be considered a duplicate although that doesn't have a solution as yet. The original request is slightly different however, that is asking for a skip if exists feature, this is a replace if exists feature request, both have merit and should exist in and of themselves, I think, so one solution may fix both and it may be that we end up with a selection on the upload form requesting how to handle duplicates.

    There still needs to be some investigation into whether https://www.drupal.org/project/media_duplicates works combined, this issue can be closed, I think.

  • 🇬🇧United Kingdom the_g_bomb

    One thing I did spot when doing the patch to MR conversion

        $duplicates = [];
    
        // Loop through each of the uploaded files
        foreach ($fileNames as $fileName) {
          $pattern = $realpath . '/*/' . $fileName;
          $matches = glob($pattern);
    
          if (count($matches) > 0) {
            // If there is already a file in the public directory with the same name
            $duplicates[] = $fileName;
          }
        }
    
        if (!empty($duplicateUris)) {
          // Load the file entities by their URIs.
          $existingFileIds = \Drupal::entityQuery('file')
          ->accessCheck(TRUE)
          ->condition('uri', $duplicateUris, 'IN')
          ->execute();
    

    The array of duplicates is never used once populated, and I wonder if the $duplicateUris variable is a typo as it is never populated that I can see.

    Perhaps the $duplicates is meant to be used in place of $duplicateUris

  • 🇬🇧United Kingdom the_g_bomb

    FYI: I have installed the media_duplicates module and
    a) It doesn't seem to work with media_bulk_upload as you are still able to upload media while duplicates exist
    b) When using the media upload only, the new module throws a form error which stops you creating a new media item while the same file exists, it doesn't then update the existing media item instead.

  • 🇬🇧United Kingdom the_g_bomb

    No point reviewing, the duplicates thing is a problem and stops this working

Production build 0.71.5 2024