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 script to convert images from images to out folder #30

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

Genne23v
Copy link

@Genne23v Genne23v commented Dec 3, 2022

This PR is addressing #3.
I'm adding image-optimizer.js with this PR. But I think it will need much more discussion from here as I have some questions about the next step.

  • This script will get all files in root images folder and create resized images in out folder.
  • Resized mages will be 525 x 295, 746 x 420, 934 x 525. Please let me know if this is suitable. Probably I have to create more square-like images for image grid layout.
  • By resizing images with sharp library, it will clear most of metadata, but let me know if this is not the intention.
  • This script can be run with node image-optimizer.js.
  • To add an option to choose image type, I would like to further understand how it should be done. I'm assuming this app is deployed whenever the user sends a pull request. Please let me know if this assumption is right.

@SerpentBytes
Copy link
Collaborator

SerpentBytes commented Dec 3, 2022

  • ... I'm assuming this app is deployed whenever the user sends a pull request. Please let me know if this assumption is right.

As I understood the flow of the events, it's as follows:

  • User enters a valid GitHub PAT token and/or repo name
  • A repo is generated using the user-provided name (If no name is provided, we generate a name)
  • User selects photos they want to upload to the repo
  • The images are optimized using a script
  • Images are committed to the repo generated on behalf of the user previously.

@Genne23v
Copy link
Author

Genne23v commented Dec 3, 2022

Thanks for the comment @SerpentBytes. I'm still not sure how it is deployed to GitHub Pages, but let me start writing scripts.

@Genne23v
Copy link
Author

Genne23v commented Dec 4, 2022

@SerpentBytes Just to make sure I'm on the right track. My understanding is that the user go to my-photohub webpage and add credentials and photos. When upload button is pressed, the user's own repo is created and deployed to GitHub Page. Am I in the right direction?

@SerpentBytes
Copy link
Collaborator

SerpentBytes commented Dec 4, 2022

@SerpentBytes Just to make sure I'm on the right track. My understanding is that the user go to my-photohub webpage and add credentials and photos. When upload button is pressed, the user's own repo is created and deployed to GitHub Page. Am I in the right direction?

To access the service, the user needs to go through the authentication which we are doing using a form which accepts PAT token and repo name. PAT token is required, but the repo name is not always. The repo name is required in a use case where a repo name is taken on the user's GitHub account. If that's the case, then the repo name is mandatory (with the current implementation.)

@SerpentBytes
Copy link
Collaborator

@Genne23v At the moment, only authentication and repo generation functionality is implemented. We have a UI for uploading photos, but it's not connected to the service.

@SerpentBytes SerpentBytes added the ✨ enhancement New feature or request label Dec 4, 2022
@humphd
Copy link
Collaborator

humphd commented Dec 5, 2022

We have a few options we can take here:

  1. We could do this optimization in the browser before we upload them to the repo. This could be done with something like https://www.npmjs.com/package/browser-image-compression or https://github.com/fengyuanchen/compressorjs or using some WASM-based library (this is what https://squoosh.app/ does via its API).
  2. We could continue with your approach, and do this server-side. The down side of this is that the images have already been sent, along with their metadata.

Regardless of which way we go, I think you should consider the file types and sizes to generate. You're picking a few sizes, but they are quite small, and they include both a width and height. This will skew images that don't have the same aspect ratio as your final dimensions. We should probably use the <picture> element, and provide <source> elements to include various sizes the browser can pick from at runtime.

I also think we need more than WebP. Probably we should do: JPEG, WebP, AVIF, PNG.

RE: your code, you are mixing async, sync fs methods. I would use async everywhere or promises.

@Genne23v
Copy link
Author

Genne23v commented Dec 5, 2022

Thank you for the feedback. I'm combining UI and GitHub repo generation script along with image optimizer. So I will replace server-side image generation work with your suggestion. Basically my change will do

  1. Hit Upload button
  2. Validate PAT and repo name and show appropriate error message on UI
  3. If credential and repo name are valid, create optimized images
  4. Add and commit to the repo

Please let me have any advise if you have. Thank you!

@humphd
Copy link
Collaborator

humphd commented Dec 5, 2022

I think this sounds right, yes.

Our UI will have a bunch of work to do, so we'll need "progress" indication to be part of it, so people don't close the window.

@Genne23v
Copy link
Author

Genne23v commented Dec 10, 2022

@humphd @SerpentBytes
I have added some updates as below.

  • By hitting Upload button, it will start image optimization, creating a repo and making a commit
  • I utilized some of the assets in worker folder. I copied the worker folder inside the src
  • This will create png, webp, jpg, avif with the sizes of 375, 744, 950, 1120
  • Added userMessage so that user can be aware what's going

Please try the feature and let me know if there is any improvement that I need to make. Thanks

@Genne23v
Copy link
Author

Also I noticed there is a merge conflict. It seems like I don't have an authority to fix this. Please let me know if I need to do something.

@SerpentBytes SerpentBytes requested a review from humphd December 11, 2022 02:11
@Genne23v
Copy link
Author

@humphd I have updated DataURL to BlobURL. Please let me know if there is any further improvement needed. Thank you

@Genne23v
Copy link
Author

@SerpentBytes As I mentioned above, I copied worker folder with a little modification inside src. Will it be an issue if I remove worker folder in the root?

@SerpentBytes
Copy link
Collaborator

@Genne23v Make sure to update the package.json scripts associated with the worker, i.e.: auth-worker and auth-worker:login. Also, since eslint configuration is different for the contents of the worker folder, could you please ensure that we don't run into linting errors? Thank you.

@Genne23v
Copy link
Author

I added your incremental lint rule to the .eslintrc.js in the root along with npm run auth-worker script. And I checked auth-worker scripts work fine. Thank you

Copy link
Collaborator

@humphd humphd left a comment

Choose a reason for hiding this comment

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

This is awesome, nice work! A few comments.

return (
<div className="App">
<header className="preview">
{!userMessage ? (
<></>
Copy link
Collaborator

Choose a reason for hiding this comment

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

null is more common when you want to show nothing.

Copy link
Author

Choose a reason for hiding this comment

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

Will be addressed

const reader = new FileReader();
reader.onload = (e) => {
imgPreviewEl.current.src = e.target.result;
};
reader.readAsDataURL(file);
imgPreviewEl.current.title = file.name;
reader.readAsDataURL(files[0]);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Question: on line 137 you allow selecting multiple files, but here assume it's only 1 (i.e., files[0]). Should you be looping through them?

Copy link
Author

Choose a reason for hiding this comment

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

If I understand this code correctly, it should read only one image and add it to imgPreviewEl.current.src to load the image on the element. If I change it to files, it doesn't load the preview on the element. I think it doesn't need to read every file unless if we want to show multiple previews.

reader.onload = async () => {
const imageBuffer = reader.result;
const byteArray = new Uint8Array(imageBuffer);
const blob = new Blob([byteArray]);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Since you know the file types we allow (i.e., image/*), why don't we pass the mimeType here too as second arg: https://developer.mozilla.org/en-US/docs/Web/API/Blob/Blob

Copy link
Author

Choose a reason for hiding this comment

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

Will be addressed accordingly

const imageBuffer = reader.result;
const byteArray = new Uint8Array(imageBuffer);
const blob = new Blob([byteArray]);
const BlobURL = URL.createObjectURL(blob);
Copy link
Collaborator

Choose a reason for hiding this comment

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

lowerCamelCase for naming in JS

Copy link
Author

Choose a reason for hiding this comment

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

Will be handled

const byteArray = new Uint8Array(imageBuffer);
const blob = new Blob([byteArray]);
const BlobURL = URL.createObjectURL(blob);
const blobData = octokit.request(`POST /repos/${username}/${repoName}/git/blobs`, {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Either you need to await this call, or better, you should return it as the Promise to wait on in line 48 below.

Copy link
Author

Choose a reason for hiding this comment

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

Will be addressed

@humphd
Copy link
Collaborator

humphd commented Dec 15, 2022

This needs a rebase too.

@Genne23v
Copy link
Author

This needs a rebase too.

I rebased my branch and it shows one commit in my view. Also I will make sure there is one commit in next one. Please let me know something is still not right. Thanks

@Genne23v
Copy link
Author

@humphd I have added camelCase dependency to transform the file name into lowerCamelCase. Image width is followed without underscore. Please let me know if this one is not the standard.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
✨ enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants