Follow-up of #2658268: Cache persistence after derivative creation

Problem/Motivation

The other issue was got committed with this check:

if (strpos($uri, PublicStream::basePath() . '/styles/') !== FALSE) {

It was discussed quite a bit, so I'm not sure why it was kept in the end.

The problem is that it only works for normal images becaus we call file_create_url() actually twice, the second time in template_preprocess_image(), only then our addition works.

If you use responsive images for example, that second call doesn't happen and responsive images aren't properly updated with the query arguments

Proposed resolution

Not 100% sure yet, possibly move the regex directly into the if.

Remaining tasks

User interface changes

API changes

Data model changes

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Berdir created an issue. See original summary.

Berdir’s picture

Status: Active » Needs review
Issue tags: +Needs tests
FileSize
2.65 KB

So, things obviously get a lot more complicated when you try to do it properly.

Looks like our test coverage is still far from complete here.

Notes:

* Moved the regex into the if, with a check about having styles, to avoid running it on anything that is clearly not an image style URL
* I don't understand the code about multiple styles. If there are multiple styles, then the only reason for that would be that they are in a folder called styles, which means the *first* styles is the one to look at, not the last.
* If you do this, then the h argument is added before itok, before it only worked hardcoded by assuming that it was appended to ?itok=...
* Another problem then was that the argument got encoded together with the URL as it was treated as a *path*, so we have to duplicate logic from file_create_url(). This might interfere with other modules that also alter the URL but there's not much we can do about that.
* Also added a check to not do something if there already is a h argument, for example because it gets called twice.

Status: Needs review » Needs work

The last submitted patch, 2: crop-file-alter-make-it-really-work-2868339-2.patch, failed testing.

Berdir’s picture

Yeah, that test was strange and was doing things 2 or even 3 times so that the public URL worked, but that also resulted in weird things like the repeated styles path. Cleaned that up.

The test only patch is also the interdiff.

The last submitted patch, 4: crop-file-alter-make-it-really-work-2868339-4-test-only.patch, failed testing.

Berdir’s picture

Not even sure we need both the buildUri() and buildUrl() tests as they are very similar, but doesn't hurt to have them both I think.

d.sibaud’s picture

The last patch solves my issue, not for responsive image, when I did upload/crop, on save the error shown in the log was:

Source image at public://2017-05/header-servizi-elopement.jpg&h=aefcb2e1 not found while trying to generate derivative image at public://styles/he/public/2017-05/header-servizi-elopement.jpg&h=aefcb2e1.

the source image public://2017-05/header-servizi-elopement.jpg was still there, but the url parameter &h=aefcb2e1 was causing the issue.

thanks for the patch.

undertext’s picture

These regular expressions and URI parsing make me sad.
Is extending Drupal API an option here?
We can just add a small hook into ImageStyle::buildUrl()
\Drupal::moduleHandler()->alter('image_style_url', $url, $this); to allow generated url to be changed based on image style.
What do you think about it?

jeroendegloire’s picture

woprrr’s picture

Status: Needs review » Reviewed & tested by the community

Thanks @jeroendegloire and Big up to @berdir for that patch for obscure reason I haven't seen this issue and any notifications :( Sorry for this bad following...

I have tested all of case and all works as a charm.

@undertext

I prefer the approach of regular expressions in this case, because this hook is called from file_create_url(), and is called fairly frequently (10+ times per page), depending on how many files there are in a given page. We really need to have a performant and fast trick to evaluate if we need to change url created or not.

  • woprrr committed 302265f on 8.x-1.x authored by Berdir
    Issue #2868339 by Berdir, jeroendegloire, tourtools, woprrr: Public...

  • woprrr committed e8a732e on 8.x-2.x authored by Berdir
    Issue #2868339 by Berdir, jeroendegloire, tourtools, woprrr: Public...
woprrr’s picture

Status: Reviewed & tested by the community » Fixed
Issue tags: +Media Initiative
FileSize
1011.54 KB

I can confirm everything is ok for both branches and responsive image work well now.

Small screenshot during my test.

@berdir big up :) and thank you all.

Status: Fixed » Closed (fixed)

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