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

Fix for plug-in: cloudfront_a #828

Merged
merged 1 commit into from
Dec 5, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions plugins/cloudfront_a.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
var hoverZoomPlugins = hoverZoomPlugins || [];
hoverZoomPlugins.push({
name:'cloudfront_a',
version:'0.1',
version:'0.2',
prepareImgLinks:function (callback) {
var res = [];

// sample: https://d38hokjm2drjyk.cloudfront.net/?url=nypost.com%2Fwp-content%2Fuploads%2Fsites%2F2%2F2021%2F03%2Fjen-psaki.jpg%3Fquality%3D90%26strip%3Dall%26w%3D1200&w=300&h=190&secure=yes&token=cdb675f5d8e584c6d6233188352417df6750c099
// -> nypost.com/wp-content/uploads/sites/2/2021/03/jen-psaki.jpg
var reThumb1 = /.*cloudfront.net\/\?url=(.*)\.(jpe?g|gif|png|webp)(.*)/;
var reThumb1 = /.*cloudfront.net.*?\?url=(.*)\.(jpe?g|gif|png|webp)(.*)/;
Copy link
Owner

Choose a reason for hiding this comment

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

@GrosPoulet I don't understand .*? pattern, can you please clarify? The * means "0 or more instances" and the ? means "0 or 1 instances". They seem redundant when used together.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@extesy:

.* : greedy
.*? : non-greedy

For instance, with url = https://d38hokjm2drjyk.cloudfront.net/blah/blah/blah/?url=nypost.com/wp-content/uploads/sites/2/2021/03/jen-psaki.jpg?quality=90

url.replace(/.*cloudfront.net.*\?(.*)/, '$1')
returns: quality=90
because regex grabs all characters until LAST "?"

url.replace(/.*cloudfront.net.*?\?(.*)/, '$1')
returns: url=nypost.com/wp-content/uploads/sites/2/2021/03/jen-psaki.jpg?quality=90
because regex grabs all characters until FIRST "?"

In the case of Cloudfront, it's a little overkill because i use "?url=" to split.
Since there is only one "?url=" in urls, greedy & non-greedy regex will return the same result.
It would be necessary if there was a second occurence of "?url=" since we want to stop at first one.
You can change it for clarity.

Copy link
Owner

Choose a reason for hiding this comment

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

Very interesting, I didn't know about non-greedy modifier. Thank you for a great explanation!

var reReplace1 = '$1.$2';

// sample: https://www.leparisien.fr/resizer/xRuYCaOxI88qdE8cFXvzjqhmje4=/932x582/cloudfront-eu-central-1.images.arcpublishing.com/leparisien/5WVYUA3CRHLVQ2B5HZZCL43LTM.jpg
Expand Down