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

ImgProxy CDN #66

Merged
merged 31 commits into from
Aug 11, 2024
Merged

ImgProxy CDN #66

merged 31 commits into from
Aug 11, 2024

Conversation

coreyja
Copy link
Owner

@coreyja coreyja commented Jun 10, 2024

I setup an instance of imgproxy and a BunnyCDN in front of it to help handle images for me

The idea is that the site can now request images from imgproxy and ask for them at specific sizes and dimensions, likely inside a picture HTML element so the browser can pick the size thats best

Comment on lines 291 to 299
let adjusted_url = if let Some(imgproxy_url) = config.imgproxy_url.as_ref() {
format!(
"{}/unsafe/rs:auto:1000:0:false:false/plain/{}",
imgproxy_url,
urlencoding::encode(&self.url)
)
} else {
self.url
};
Copy link
Owner Author

Choose a reason for hiding this comment

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

The issue I'm running into here that needs to be solved is that the self.url here is likely a relative path to the image, which isn't something I can send to imgproxy

So I need to get the URL of the page we are rendering in here to be able to move forward

@coreyja
Copy link
Owner Author

coreyja commented Jul 30, 2024

URL for imgproxy: https://imgproxy-cja.fly.dev/

];

if std::env::var("CRON_DISABLED").unwrap_or_else(|_| "false".to_string()) != "true" {
Copy link
Collaborator

Choose a reason for hiding this comment

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

This code checks an environment variable to determine whether to enable ordisable cronjobs.

_config: &AppConfig,
_context: &SyntaxHighlightingContext,
) -> Result<Markup> {
fn into_html(self, config: &AppConfig, context: &SyntaxHighlightingContext) -> Result<Markup> {
Copy link
Collaborator

@Seif-Mamdouh Seif-Mamdouh Aug 1, 2024

Choose a reason for hiding this comment

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

function into_html take the
Markdown image syntax into HTML, constructing the image URL by combining the base URL, article path, and image filename.

.ast
.into_html(&state.app, &state.markdown_to_html_context)
{
// creating some context for the article_path
Copy link
Collaborator

Choose a reason for hiding this comment

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

context vat attempts to clone the existing markdown-to-html context and updates it with the current article's path

.gitignore Outdated
@@ -17,3 +17,5 @@ models
tmp_base_model

server/src/local.rs

CRON_DISABLED=true
Copy link
Owner Author

Choose a reason for hiding this comment

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

Hehe wrong file for this 😉

This wants to be in your .envrc file (any likely is I feel like lol)

Copy link
Collaborator

Choose a reason for hiding this comment

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

I put it there by accident lol

tailwindcss Outdated
Copy link
Owner Author

Choose a reason for hiding this comment

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

Ahh we don't want this binary file in the repo! It should also be in your ~/bin dir so I think we can leave it off here!

Comment on lines 299 to 300
.split('/')
.next()
Copy link
Owner Author

Choose a reason for hiding this comment

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

Would be nice to not do any path parsing 'manually' 🤔

This is grabbing the stuff before the first / right? Whats after the slash lol?

Copy link
Collaborator

@Seif-Mamdouh Seif-Mamdouh Aug 2, 2024

Choose a reason for hiding this comment

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

In my local directory, I have a URL like this: http://localhost:3000/posts/BattlesnakeMinimax/Minimax%20in%20Battlesnake/. I only need the part of the URL before Minimax%20in%20Battlesnake for the image path. To achieve this, I split the URL and grab everything I need before Minimax%20in%20Battlesnake

"{}/posts/{}/{}",
config.base_url.trim_end_matches('/'),
article_path,
self.url.trim_start_matches("../")
Copy link
Owner Author

Choose a reason for hiding this comment

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

Would be nice to avoid this path parsing too!

Could we use some Rust Path structs/methods to like join the paths together? I feel like if we told it to join the paths and one has a ../ it will do the 'correct' thing and move up a directory there! 🤞

Copy link
Owner Author

@coreyja coreyja left a comment

Choose a reason for hiding this comment

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

Looking great!

Left some comments on the structs and then on the manual path parsing logic!

@@ -19,6 +21,7 @@ use crate::AppConfig;
pub struct SyntaxHighlightingContext {
pub(crate) theme: syntect::highlighting::Theme,
pub(crate) syntax_set: syntect::parsing::SyntaxSet,
pub(crate) current_article_path: Option<String>,
Copy link
Owner Author

Choose a reason for hiding this comment

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

We can remove this now that we have the new MarkdownRenderContext struct right?

Copy link
Owner Author

Choose a reason for hiding this comment

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

Did this come back in a merge maybe?

We have the MarkdownRenderContext so I think we can/want to remove this path from this struct!

.unwrap_or("unknown");

let base_url = PathBuf::from(config.base_url.trim_end_matches('/'));
let image_path = PathBuf::from(self.url.trim_start_matches("./"));
Copy link
Owner Author

Choose a reason for hiding this comment

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

./ means that we stay in the current path so this is actually fine to keep in!

The PathBuf::join method know hows to handle this and strips it our for us!

Copy link
Owner Author

Choose a reason for hiding this comment

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

You can also call join and pass it a &str so you don't have to convert it to a PathBuf first!

@coreyja
Copy link
Owner Author

coreyja commented Aug 4, 2024

Hey @Seif-Mamdouh!

I pulled down the code to check it running and it run great for me without any changes! So don't think you broke it at the end lol

I was curious about a refactor and then kinda just did it 😆

Here is a link to the commit where I did most of the refactoring. I wrote a longer commit message so hopefully reading that can kinda show my thought processes since I didn't record doing this refactor.
93dd2d5 (#66)

And then also just highlighting these two code level comments I left on your changes before I refactored them: #66 (comment)

I think this is looking really good for getting the absolute URL on image now!
But one thing I think that got lost is using imgproxy so how about on Monday you give a shot to adding that back and better than the test version I had before!

So what we want to do is when we make the Image struct into html instead of using an 'old school' img tag lets us the newer picture tag which lets us do some more fancy things!

This is a good article on how to use this to serve different sized images to different sized devices! https://developer.mozilla.org/en-US/docs/Learn/HTML/Multimedia_and_embedding/Responsive_images

The idea is that we want to do that! But we only have the original sized images in our repo. So instead of linking to hard coded resized images, we want to link to imgproxy images so that it can do the resizing on the fly! And we will actually run imgproxy behind a CDN so we only have to generate each image size once!

Here are the imgproxy docs on how to construct a URL that has different options: https://docs.imgproxy.net/usage/processing#resize

@Seif-Mamdouh Seif-Mamdouh marked this pull request as ready for review August 6, 2024 15:29
posts/.DS_Store Outdated Show resolved Hide resolved
Comment on lines 311 to 313
let small_url = generate_imgproxy_url(base_url, &img_src, 300, 300);
let medium_url = generate_imgproxy_url(base_url, &img_src, 600, 600);
let large_url = generate_imgproxy_url(base_url, &img_src, 1200, 1200);
Copy link
Owner Author

Choose a reason for hiding this comment

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

For these can we maybe do height 0 so that imgproxy calculates the right height based on the width?

Since we are using the width for picking the right image source (which I like!) I think we might want to just use that and leave off the heights and let imgproxy figure those out!

When set to 0, imgproxy will calculate resulting height using the defined width and source aspect ratio.
From: https://docs.imgproxy.net/usage/processing#resize

Copy link
Collaborator

Choose a reason for hiding this comment

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

Make sense

…guess to format of the fly url but I think thats ok
# Via GitHub
* main:
  Remove Neon Review App branches on PR close (#70)

# Conflicts:
#	.github/workflows/fly_review.yml
@coreyja coreyja mentioned this pull request Aug 11, 2024
@coreyja coreyja linked an issue Aug 11, 2024 that may be closed by this pull request
@coreyja coreyja merged commit ed4f4e2 into main Aug 11, 2024
6 checks passed
@coreyja coreyja deleted the ca/main/imgproxy branch August 11, 2024 19:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Create appropriate sized images
2 participants