Problem/Motivation

The module unconditionally appends FileIsSvgImage to #upload_validators so when you upload a JPEG or PNG via the Media Library widget, Drupal runs FileIsSvgImageConstraintValidator and the uploaded file is rejected because it isn't an SVG.

Steps to reproduce

Enable SVG Image Field, the SVG Image Field Media Bundle submodule, and core Media Library module.
Configure the Article content type to contain a Media field with Image and Vector Image types available.
Go to /node/add/article, click Add Media and upload a JPEG to the Media Image field where the extensions jpg, jpeg are allowed using the Media Library widget.

Expected result
User is able to upload JPEG image to the Image media bundle via the Media Library popup widget.

Actual result
User is prevented from uploading JPEG image to the Image media bundle with error message:

The specified file [filename].jpeg could not be uploaded.
Only files with mime type image/svg+xml,image/svg are allowed. Mime type detected image/jpeg

Media Library Widget with SVG validation error for JPG image upload

Proposed resolution

Only apply SVG validation when the media type is svg/Vector Image.

Remaining tasks

Merge request to come.

User interface changes

None

API changes

None

Data model changes

None

CommentFileSizeAuthor
#7 3590084-after.png183.64 KBjwilson3
#4 3590084-before.png249.08 KBjwilson3
Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

hitfactory created an issue. See original summary.

jwilson3 made their first commit to this issue’s fork.

jwilson3’s picture

Issue summary: View changes
jwilson3’s picture

Issue summary: View changes
StatusFileSize
new249.08 KB

jwilson3’s picture

Status: Active » Needs review

I encountered FileIsSvgImageConstraintValidator and prepared an MR-first fix for review.

What this change does:

  • Scopes the Media Library upload validator so FileIsSvgImage is only added when the selected media type uses the svg media source.
  • Adds kernel test coverage that builds the real Media Library upload form for both image and vector_image and verifies the SVG validator is only present on the SVG-backed form.

Scope of changes:

  • 2 files modified
  • +96/-20 lines
  • svg_image_field.module
  • tests/src/Kernel/MediaLibraryUploadValidationTest.php

Steps to test:

  1. Enable SVG Image Field, SVG Image Field Media Bundle, and Media Library.
  2. Configure Article with a Media Library field that allows both Image and Vector Image uploads.
  3. Select the Image tab in the Media Library modal and upload a JPEG. Before the fix it is rejected by FileIsSvgImageConstraintValidator; after the fix it is accepted.
  4. Select the Vector Image tab and upload an invalid SVG. SVG-specific validation should still reject it.
jwilson3’s picture

Status: Needs review » Reviewed & tested by the community
StatusFileSize
new183.64 KB

The normal CI pipeline passed all tests. I then ran the tests-only CI Job, which reverted svg_image_field.module, so it ran the new regression test against the pre-fix behavior. In that state, the failure is expected:

testSvgUploadValidatorsAreScopedToSvgMediaType fails because the non-SVG Media Library upload form still has FileIsSvgImage attached.

This result proves the test catches the bug when the fix is absent.

I also tested locally with and without the fix applied.

Without the fix: I see the same error message as OP (see screenshot in IS).
With the fix: I am able to successfully upload a JPEG image via Media Library widget popup.

Proper JPEG upload

  • jwilson3 committed 2a32c6ae on 2.3.x
    Issue #3590084 by jwilson3: Scope Media Library SVG validation to vector...
jwilson3’s picture

Status: Reviewed & tested by the community » Fixed

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

Status: Fixed » Closed (fixed)

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