Problem/Motivation

How to reproduce:
* Enable duplicate-file-prevention
* Upload a file to a filefield of an entity
* Delete the entity
* Upload the file again to a filefield of an entity

Expected:
* No problem

Experisnced:
* Duplicate-file-prevention prevents re-upload

Background:
Since #1399846-280: Make unused file 'cleanup' configurable deleting fielfield files does not physically delete them, but marks them as managed temporary files, that will be deleted by cron some hours later.

Proposed resolution

Exclude temporary files from duplicate-file-prevention. Backport to 7.x also.
(Maybe a one-liner)

Comments

axel.rutz created an issue. See original summary.

o'briat’s picture

Something like altering the query in filehash_validate_dedupe() that way :

     $query = \Drupal::database()->select('filehash', 'fh');
     $query->addField('fh', 'fid');
     // Do not check temp files (removed every 6 hours @see file_cron()).
     $query->join('file_managed', 'fm', 'fh.fid=fm.fid');
     $query->condition('fm.status', 1, '=');
     // Do not check itself :).
     $query->condition('fh.fid', (int) $file->id(), '!=');
     // Check if hash match.
     $query->condition('fh.' . $algo, $file->filehash[$algo]);
     $query->range(0, 1);
mfb’s picture

Issue tags: +Needs tests

Sounds sensible to me. Adding needs test tag (it also needs a patch of course :)

mfb’s picture

The one kinda big problem with this is that it allows duplicate files to be uploaded at the same time (either by the same user, or different users), if they're both temporary files.

mfb’s picture

Ideally we could fix this upstream in Drupal core - temporary and deleted could be different file statuses?

timwood’s picture

We encounter this issue weekly, sometimes daily, with our users. Sometime they upload a file and then don't finish the process of saving the object the file is attached to (usually a media entity but in some cases filefield on node). Then they try again and get the duplicate file error.

We recently added the https://www.drupal.org/project/media_entity_file_replace module to allow replacing/updating a file while optionally maintaining the original filename, but when there is a validation error (some other required field not filled in or whatever) this module doesn't "carry along" the temporarily uploaded new file and thus the user is stuck with another duplicate file error.

The one kinda big problem with this is that it allows duplicate files to be uploaded at the same time (either by the same user, or different users), if they're both temporary files.

To this point, I think it would be a pretty rare occurrence for this to happen and if it was by the same user on the same edit form, at least only one file would be marked as permanent and the duplicate temp file would be removed eventually anyway.

mfb’s picture

Category: Bug report » Feature request

This feels more like a feature request / enhancement than bug report - it's working as designed but UX is quite poor :)

foodslover’s picture

Using https://www.drupal.org/project/filehash/issues/2920042#comment-12919090 to create a patch to exclude temporary files from duplicate-file-prevention

  • drupal 9.2.7
  • version 8.x-1.5
foodslover’s picture

foodslover’s picture

foodslover’s picture

avpaderno’s picture

Status: Active » Needs review
mfb’s picture

Status: Needs review » Needs work

Patch doesn't apply, and needs tests

timwood’s picture

StatusFileSize
new804 bytes

Here's a patch that should apply cleanly. No tests added so keeping status as Needs work.

avpaderno’s picture

Title: Exclude temporary (&deleted) files from duplicate-file-prevention » Exclude temporary (and deleted) files from duplicate-file prevention
Status: Needs work » Needs review
mfb’s picture

Status: Needs review » Needs work

Still needs tests (and there is a minor code style issue to fix up)

timwood’s picture

StatusFileSize
new805 bytes

Code style issue fixed, I believe.

mfb’s picture

Version: 8.x-1.x-dev » 2.x-dev
Status: Needs work » Needs review

I have work underway on this in the 2.x branch (still needs test coverage)

mfb’s picture

StatusFileSize
new1.88 KB

Functional test for this feature

  • mfb committed 7efc1ed on 2.x
    Issue #2920042 by mfb: Test for exclude temporary (and deleted) files...
mfb’s picture

Status: Needs review » Fixed
Issue tags: -Needs tests

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

WiseMike’s picture

Patch #17 worked in my case. Thanks