Commit c0b0264f authored by Ivan Doroshenko's avatar Ivan Doroshenko Committed by Joshua Walker
Browse files

Issue #3253384 by Matroskeen, yaasir.ketwaroo: 422 error when file extension...

Issue #3253384 by Matroskeen, yaasir.ketwaroo: 422 error when file extension does not match mime type
parent dad6bae8
Loading
Loading
Loading
Loading
+5 −6
Original line number Diff line number Diff line
@@ -148,12 +148,11 @@ class TusServerController extends ControllerBase {
    $field_definition = $bundle_fields[$meta_values['fieldName']];

    // Check the uploaded file type is permitted by field.
    $allowed_extensions = explode(' ', $field_definition->getSettings()['file_extensions']);
    $file_type = explode('/', $meta_values['filetype']);
    $file_type = end($file_type);

    if (!in_array($file_type, $allowed_extensions, TRUE)) {
      throw new UnprocessableEntityHttpException(sprintf('File type "%s" is not supported for this field.', $file_type));
    // See file_validate_extensions().
    $allowed_extensions = $field_definition->getSettings()['file_extensions'];
    $regex = '/\.(' . preg_replace('/ +/', '|', preg_quote($allowed_extensions)) . ')$/i';
    if (!preg_match($regex, $meta_values['filename'])) {
      throw new UnprocessableEntityHttpException(sprintf('Only files with the following extensions are allowed: %s.', $allowed_extensions));
    }
  }

+5 −6
Original line number Diff line number Diff line
@@ -242,12 +242,11 @@ class TusServer implements TusServerInterface, ContainerInjectionInterface {
    $field_definition = $bundle_fields[$metadata['fieldName']];

    // Check the uploaded file type is permitted by field.
    $allowed_extensions = explode(' ', $field_definition->getSettings()['file_extensions']);
    $file_type = explode('/', $metadata['filetype']);
    $file_type = end($file_type);

    if (!in_array($file_type, $allowed_extensions, TRUE)) {
      throw new UnprocessableEntityHttpException(sprintf('File type "%s" is not supported for this field.', $file_type));
    // See file_validate_extensions().
    $allowed_extensions = $field_definition->getSettings()['file_extensions'];
    $regex = '/\.(' . preg_replace('/ +/', '|', preg_quote($allowed_extensions)) . ')$/i';
    if (!preg_match($regex, $metadata['filename'])) {
      throw new UnprocessableEntityHttpException(sprintf('Only files with the following extensions are allowed: %s.', $allowed_extensions));
    }

    // Check if the file already exists.