Good morning,
I try to use mimedetect-module (https://www.drupal.org/project/mimedetect) which offers a way to check if uploaded files mimetype matches the file extension.
To make the validation work, I added

/**
 * Implements hook_file_validate().
 */
function custommedia_file_validate($file) {
  if (module_exists('mimedetect')) {
    $errors = array();
    $detected_mime = mimedetect_mime($file);

    if ($file->filemime != $detected_mime) {
      $extension = pathinfo($file->filename, PATHINFO_EXTENSION);
      $errors[] = t('It was detected the file format was originally (@type) and this does not match the current file format (@extension). Please re-save the image file in the original format and attempt to re-upload.', array('@type' => $detected_mime, '@extension' => $extension));
    }

    return $errors;
  }
}

This works great in all file / media dialogs/widgets besides plupload (in combinaiton with filefield_sources_plupload). If there is a validation error, I get the error message as expected. But, the form UI changes and I have 2 plupload fields now. The first plupload widgets does NOT work afterwards when trying to upload a valid file. The second widget works.

I already checked out some of the issues decribed here in the issue queue - but no suggested solution seems to work.

Comments

vistree created an issue. See original summary.

vistree’s picture

Issue summary: View changes
vistree’s picture

As an update: the problem seems to be related to this issue https://www.drupal.org/project/filefield_sources_plupload/issues/2996699
So, the problem is only if I upload a single "faulty" file. If I upload multiple files and just one is faulty, everything works fine.

vistree’s picture

After investigating again: problem seems only to be if you try to upload just corrupted files
- 1 file with wrong mimetype: error is shown about wrong mimetype + upload window is duplicated = broken
- 1 good file + 1 file with wrong mimetype: works (error shown about the one file having wrong mimetype, but the is uploaded and I see single upload field afterwards)
- 2 files with wrong mimetype: error is shown about wrong mimetype + upload window is duplicated = broken

jordanmagnuson’s picture

Any update/solution in regards to this issue? I'm experiencing the same problem.

jordanmagnuson’s picture

Here's a screenshot of the duplicate plupload form I get on custom hook_file_validation() error: https://i.imgur.com/HBQZSdI.png

jordanmagnuson’s picture

I can confirm most of vistree's observations:

- If the filefield cardinality (number of values) is set to 1, everything works fine, whether or not file validation errors are triggered. If cardinality is set to more than 1, the duplicate plupload field becomes an issue any time a file validation error is triggered. (see this screenshot: https://i.imgur.com/HBQZSdI.png)

- If cardinality is > 1 and multiple files are uploaded at once, everything is fine as long as *one* of the files passes file validation. I suspect because of the way the form is rebuilt/redrawn on successful file upload. So even if some files fail validation, the form is not duplicated as long as one file successfully uploads.