When trying to set a source imagefile with an upper-cased extension (ie 'JPG') using setSourceImageFile, the module throws an Exception:

Drupal\textimage\TextimageException: Textimage error: Attempted to set an unsupported file image extension (JPG) in Drupal\textimage\Textimage->setTargetExtension() (line 314 of /httpdocs/modules/textimage/src/Textimage.php).

Turns out the returned Array from $this->imageFactory->getSupportedExtensions() is all in lower-case.
So even when (in my case) the returned Array is:
array(5)
string(3) "gif"
string(3) "jpe"
string(4) "jpeg"
string(3) "jpg"
string(3) "png"

which has "jpg" in the a file like 'example.JPG' isn't accepted.

CommentFileSizeAuthor
#9 2810123-9.patch2.05 KBspokje
#6 2810123-5.patch2.08 KBspokje
#3 2810123-3.patch1.92 KBspokje

Comments

Spokje created an issue. See original summary.

mondrake’s picture

Issue tags: +Novice, +Needs tests

Thanks for the report @Spokje

This should be easy to fix: the setTargetExtension method is not converting to lower the extension passed in before checking against the valid ones.

  /**
   * {@inheritdoc}
   */
  public function setTargetExtension($extension) {
    if ($this->extension) {
      throw new TextimageException("Extension already set");
    }
    if (!in_array($extension, $this->imageFactory->getSupportedExtensions())) {
      $this->logger->error("Unsupported image file extension (%extension) requested.", ['%extension' => $extension]);
      throw new TextimageException("Attempted to set an unsupported file image extension ({$extension})");
    }
    return $this->set('extension', $extension);
  }

Also, would need a test in TextimageApiTest to prevent future regressions.

spokje’s picture

StatusFileSize
new1.92 KB

As mondrake suggested in #2: Patch includes new test-case and lower-casing file extension before do the check.

Since I'm new to the Patching and TestCasing business, there are endless possibilities I made errors, please feel free to point them out.

spokje’s picture

Status: Active » Needs review
mondrake’s picture

Status: Needs review » Needs work
Issue tags: -Novice, -Needs tests

Patch looks great! Just a few points:

1. You have some whitespace (or tabs?) at the end of the lines. Please remove any trailing space and/or convert tabs to spaces, according to coding standards.

2.

+++ b/src/Tests/TextimageApiTest.php
@@ -197,6 +197,22 @@ class TextimageApiTest extends TextimageTestBase {
+    // Ensure upper-casing in target image file extension is not a reason for exceptions.

This comment is exceeding 80 chars. Please trim exceeding chars and put in a new line.

3.

+++ b/src/Tests/TextimageApiTest.php
@@ -197,6 +197,22 @@ class TextimageApiTest extends TextimageTestBase {
+    $this->assertEqual('image/png', $image->getMimeType());      ¶

You may also want to assert that the file extension of $textimage->getUri() is actually a '.png' (lowercase).

4.

+++ b/src/Textimage.php
@@ -309,7 +309,7 @@ class Textimage implements TextimageInterface {
-    if (!in_array($extension, $this->imageFactory->getSupportedExtensions())) {
+    if (!in_array(strtolower($extension), $this->imageFactory->getSupportedExtensions())) {

Let's do a $extension = strtolower($extension); before the if block, and leave the if untouched. This way we will store the lowercased extension in any case, which is better as in all the image system the extensions are represented lowercase.

spokje’s picture

StatusFileSize
new2.08 KB

Thanks, all are valid remarks. Should all be fixed in the newly added patch.

spokje’s picture

Status: Needs work » Needs review

Grmbl, I've seem to manage to type one message without an English grammar error, and then miscounted the comment#, so patch 2810123-5.patch should be named 2810123-6.patch.

mondrake’s picture

Status: Needs review » Needs work

Thank you. Last two more things from me then :)

  1. +++ b/src/Tests/TextimageApiTest.php
    @@ -197,6 +197,27 @@ class TextimageApiTest extends TextimageTestBase {
    +    // Ensure upper-casing in target image file extension
    +    // is not a reason for exceptions, and upper-cased
    +    // extensions are lowered.
    

    Each line should be as close as possible to 80 chars. I can fix it on commit, do not worry, but just be aware that this is the coding standard and you will find similar remarks especially when you will be proposing patches for Drupal core :)

  2. +++ b/src/Tests/TextimageApiTest.php
    @@ -197,6 +197,27 @@ class TextimageApiTest extends TextimageTestBase {
    +    $exploded_textimage_uri = explode('.', $textimage->getUri());
    +    $image_file_extension = array_pop($exploded_textimage_uri);
    

    why not using $image_file_extension = pathinfo($textimage->getUri(), PATHINFO_EXTENSION); instead?

Also, it would be good to include interdiffs in new patches :)

spokje’s picture

StatusFileSize
new2.05 KB

Thanks @Mondrake for dragging me along here.
All remarks should be addressed in patch 2810123-9.patch, except for the interdiffs (which I solemnly promise to setup for the next patch ;).

spokje’s picture

Status: Needs work » Needs review
mondrake’s picture

Status: Needs review » Reviewed & tested by the community

Looks good now, thanks. Setting to RTBC if anyone will have more comments; not much reviews of patches in this module - if no more input in next couple days I will commit this.

mondrake’s picture

Title: TextimageException: Textimage error: Attempted to set an unsupported file image extension (JPG) in Drupal\textimage\Textimage->setTargetExtension() thrown for uppercase allowed file-extension » Attempt to set valid target extension to uppercase throws an exception
mondrake’s picture

  • mondrake committed afe26be on 8.x-3.x authored by Spokje
    Issue #2810123 by Spokje, mondrake: Attempt to set valid target...
mondrake’s picture

Committed. Thank you @Spokje!

mondrake’s picture

Status: Reviewed & tested by the community » Fixed

Status: Fixed » Closed (fixed)

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

spokje’s picture

Assigned: spokje » Unassigned