Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add check for local files before processing post images in Photon #13103

Merged
merged 5 commits into from
Sep 4, 2019

Conversation

hanifn
Copy link
Contributor

@hanifn hanifn commented Jul 22, 2019

Summary

In Jetpack_Photon::filter_the_content, the call to strip_image_dimensions_maybe is outside the if clause that checks for local uploads. This is problematic because strip_image_dimensions_maybe only checks if the filename has the image dimensions string, e.g.: 300x300, and then calls file_exists on this file after removing the image dimensions and prepending the basedir. If a file is hosted externally, like on a CDN, the call to file_exists will only return false. Additionally, if the site is using a custom Stream wrapper implementation to upload files to the CDN, these file_exists calls results in a lot of requests to the CDN that returns 404 status. This means those requests are uncached and would slow down the site load times.

Changes proposed in this Pull Request:

  • Create a dedicated function to check if a file is locally uploaded or not.
  • Add the local file check into strip_image_dimensions_maybe.

Testing instructions:

Note: Tested on a site hosted on VIP Go.

  • Enable Jetpack Photon for site.
  • Create a new post with an image that's hosted externally and with a filename that has ends in something like -300x300.jpg.
  • Visit post and make sure there's no external calls to CDN that results in a 404 response.

Proposed changelog entry for your changes:

  • Add check for local file upload before processing post images with Photon

@hanifn hanifn requested a review from a team July 22, 2019 12:31
@jetpackbot
Copy link

jetpackbot commented Jul 22, 2019

Thank you for the great PR description!

When this PR is ready for review, please apply the [Status] Needs Review label. If you are an a11n, please have someone from your team review the code if possible. The Jetpack team will also review this PR and merge it to be included in the next Jetpack release.

Scheduled Jetpack release: September 3, 2019.
Scheduled code freeze: August 27, 2019

Generated by 🚫 dangerJS against a84daa0

@hanifn hanifn added the [Status] Needs Review To request a review from fellow Jetpack developers. Label will be renamed soon. label Jul 22, 2019
@jeherve jeherve added [Type] Enhancement Changes to an existing feature — removing, adding, or changing parts of it [Feature] Photon aka "Image CDN". Feature developed in the Image CDN package and shipped in multiple plugins [Pri] Normal labels Jul 25, 2019
@jeherve jeherve added this to the 7.6 milestone Jul 25, 2019
Copy link
Member

@jeherve jeherve left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems to work in my tests. Thank you for adding tests as well. I would have 2 minor remarks.

class.photon.php Outdated Show resolved Hide resolved
class.photon.php Outdated Show resolved Hide resolved
@jeherve jeherve added [Status] Needs Author Reply We would need you to make some changes or provide some more details about your PR. Thank you! [Status] Needs Review To request a review from fellow Jetpack developers. Label will be renamed soon. and removed [Status] Needs Review To request a review from fellow Jetpack developers. Label will be renamed soon. [Status] Needs Author Reply We would need you to make some changes or provide some more details about your PR. Thank you! labels Jul 25, 2019
@jeherve jeherve dismissed their stale review July 25, 2019 12:32

changes addressed

Copy link
Member

@zinigor zinigor left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know I'm probably overthinking it, but can we go back to only getting upload dir details once per image? Currently with this code we're doing it three times. And while the code only runs the "lightweight" version that doesn't refresh the upload folder every time, it still runs through filters that may be set on the upload dir location. We will be triggering these filters three times more now with this change.
Can we get rid of the is_local_upload method, store the upload folder data as before and just run strpos inside if conditions?

class.photon.php Outdated Show resolved Hide resolved
@jeherve jeherve added [Status] Needs Author Reply We would need you to make some changes or provide some more details about your PR. Thank you! and removed [Status] Needs Review To request a review from fellow Jetpack developers. Label will be renamed soon. labels Jul 29, 2019
@jeherve jeherve removed this from the 7.6 milestone Jul 29, 2019
@hanifn
Copy link
Contributor Author

hanifn commented Jul 31, 2019

@zinigor That makes sense. Considering that we're only using this in only 2 places in the class right now, the new is_local_upload might be unneeded. Still, we would need to do the same check in strip_image_dimensions_maybe so the number of calls to wp_get_upload_dir() would only be reduced by one. What do you think of moving the call to strip_image_dimensions_maybe() inside the first if clause that checks for local file? That should reduce the need to get the upload directories to just once.

@zinigor
Copy link
Member

zinigor commented Aug 26, 2019

Whoa, sorry for such a delay with the response, @hanifn . Yeah, sure, if we can reduce the number of calls even more, I'm all for that. Can you please make it, as well as rebase the code so that it wouldn't conflict? I'm happy to take a look and test again.

@hanifn hanifn force-pushed the fix/strip-image-dimensions-maybe branch from 04048de to a84daa0 Compare August 27, 2019 09:33
@hanifn
Copy link
Contributor Author

hanifn commented Aug 27, 2019

@zinigor Changes made and rebased on master in a84daa0

@hanifn hanifn added [Status] Needs Review To request a review from fellow Jetpack developers. Label will be renamed soon. and removed [Status] Needs Author Reply We would need you to make some changes or provide some more details about your PR. Thank you! labels Aug 27, 2019
@hanifn hanifn requested a review from zinigor August 28, 2019 02:22
Copy link
Member

@zinigor zinigor left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, sorry it took me so long to review this one!

@zinigor zinigor added [Status] Ready to Merge Go ahead, you can push that green button! and removed [Status] Needs Review To request a review from fellow Jetpack developers. Label will be renamed soon. labels Sep 4, 2019
@jeherve jeherve added this to the 7.8 milestone Sep 4, 2019
@jeherve jeherve merged commit 21952b5 into master Sep 4, 2019
@jeherve jeherve deleted the fix/strip-image-dimensions-maybe branch September 4, 2019 15:19
@matticbot matticbot added [Status] Needs Changelog and removed [Status] Ready to Merge Go ahead, you can push that green button! labels Sep 4, 2019
jeherve added a commit that referenced this pull request Sep 20, 2019
jeherve added a commit that referenced this pull request Sep 24, 2019
jeherve added a commit that referenced this pull request Sep 24, 2019
* Changelog: initial set of changes for 7.8

* Changelog: add #13310

* Changelog: add #13103

* Changelog: add #13426

* Changelog: add #13389

* Changelog: add #13449

* Changelog: add #13461

* Changelog: add #13460

* Changelog: add #13441

* Changelog: add #13454

* Changelog: add #13457

* Changelog: add #13425

* Changelog: add #13473

* Changelog: add #13355

* Changelog: add #13451

* Changelog: add #13358

* Changelog: add #13464

* Changelog: add #13416

* Changelog: add #13494

* Changelog: add #13465

* Changelog: add #13424

* Changelog: add #13432

* Changelog: add #13471

* Changelog: add 7.7.2 entry

* Changelog: add #13446

* Add more testing elements
jeherve added a commit that referenced this pull request Sep 24, 2019
* Changelog: initial set of changes for 7.8

* Changelog: add #13310

* Changelog: add #13103

* Changelog: add #13426

* Changelog: add #13389

* Changelog: add #13449

* Changelog: add #13461

* Changelog: add #13460

* Changelog: add #13441

* Changelog: add #13454

* Changelog: add #13457

* Changelog: add #13425

* Changelog: add #13473

* Changelog: add #13355

* Changelog: add #13451

* Changelog: add #13358

* Changelog: add #13464

* Changelog: add #13416

* Changelog: add #13494

* Changelog: add #13465

* Changelog: add #13424

* Changelog: add #13432

* Changelog: add #13471

* Changelog: add 7.7.2 entry

* Changelog: add #13446

* Add more testing elements
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] Photon aka "Image CDN". Feature developed in the Image CDN package and shipped in multiple plugins [Pri] Normal [Type] Enhancement Changes to an existing feature — removing, adding, or changing parts of it
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants