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

Unify Logic in Image APIs #647

Merged
merged 11 commits into from
Oct 5, 2024
Merged

Unify Logic in Image APIs #647

merged 11 commits into from
Oct 5, 2024

Conversation

patriciaahuang
Copy link
Contributor

@patriciaahuang patriciaahuang commented Oct 1, 2024

Summary

Unified the logic in the backend image APIs and the frontend image API, since there are starting to be more use cases for images but the logic is all the same.

Notion/Figma Link

https://www.notion.so/Unify-Logic-in-Image-APIs-1180ad723ce18078b289c240371aea60?pvs=4

Test Plan

I checked that team event proof images and member profile images still works properly, using the same method as in PR #642.

@patriciaahuang patriciaahuang requested a review from a team as a code owner October 1, 2024 22:12
@dti-github-bot
Copy link
Member

dti-github-bot commented Oct 1, 2024

[diff-counting] Significant lines: 144.

@@ -315,6 +321,20 @@ loginCheckedPut('/coffee-chat', async (req, user) => ({
coffeeChats: await updateCoffeeChat(req.body, user)
}));

// Coffee Chat Proof Image
Copy link
Collaborator

Choose a reason for hiding this comment

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

What if we unified this on the backend as well? So just have a generic /image/:name(*) (get), /image-signed-url/:name(*) (get), /image/:name(*) (delete) endpoints? I think the logic is the same across all the different use-cases anyways.

Instead of the utils, we'd have just three endpoints total (as opposed to 9)

Copy link
Collaborator

Choose a reason for hiding this comment

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

You should keep the image names the same though, so I would still make sure the images, when they're created, are prefixed correctly (i.e. eventProofs/, coffeeChatProofs/, member-image/)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oops, I missed unifying this file as well. Will do that!

Copy link
Collaborator

Choose a reason for hiding this comment

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

Even better if it's just prefixed with /files to be super generic :D

Comment on lines 10 to 37
export const setImage = async (name: string): Promise<string> => {
const file = bucket.file(`${name}.jpg`);
const signedURL = await file.getSignedUrl({
action: 'write',
version: 'v4',
expires: Date.now() + 15 * 60000 // 15 min
});
return signedURL[0];
};

/**
* Gets image for member
* @param name - the name of the image
* @throws NotFoundError if the requested image does not exist
* @returns a Promise to the signed URL to the image file
*/
export const getImage = async (name: string): Promise<string> => {
const file = bucket.file(`${name}.jpg`);
const fileExists = await file.exists().then((result) => result[0]);
if (!fileExists) {
throw new NotFoundError(`The requested image (${name}) does not exist`);
}
const signedUrl = await file.getSignedUrl({
action: 'read',
expires: Date.now() + 15 * 60000
});
return signedUrl[0];
};
Copy link
Collaborator

@henry-li-06 henry-li-06 Oct 2, 2024

Choose a reason for hiding this comment

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

ok so let's rename these while you're at it (ik i'm the one that gave them shitty names in the first place)

  • getWriteSignedURL
  • getReadSignedURL

or something a long those lines

* @returns a Promise to the signed URL to the image file
*/
export const setImage = async (name: string): Promise<string> => {
const file = bucket.file(`${name}.jpg`);
Copy link
Collaborator

Choose a reason for hiding this comment

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

on a side note - probably better off including the file extension in the actual file path itself instead of assuming they're all .jpg

})
);

images
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think this value looks like it's floating and nothing is assigned to it?

@andrew032011
Copy link
Collaborator

andrew032011 commented Oct 5, 2024

I'm unable to fetch existing images anymore here. Did we change how we construct the image paths? The image paths should still be the same (to maintain backwards compatibility with the existing images we're storing), even if we're changing the endpoints.

@patriciaahuang
Copy link
Contributor Author

I'm unable to fetch existing images anymore here. Did we change how we construct the image paths? The image paths should still be the same (to maintain backwards compatibility with the existing images we're storing), even if we're changing the endpoints.

I included the file extension in the file path to avoid assuming all images are .jpg. Before, .jpg was manually added to the file path name when getting an image in imageAPI.ts, so removing that will cause existing images to be fetched incorrectly (instead of fetching pathname.jpg it'll try fetching just pathname). Should I revert this change to maintain backwards compatibility?

@andrew032011
Copy link
Collaborator

I'm unable to fetch existing images anymore here. Did we change how we construct the image paths? The image paths should still be the same (to maintain backwards compatibility with the existing images we're storing), even if we're changing the endpoints.

I included the file extension in the file path to avoid assuming all images are .jpg. Before, .jpg was manually added to the file path name when getting an image in imageAPI.ts, so removing that will cause existing images to be fetched incorrectly (instead of fetching pathname.jpg it'll try fetching just pathname). Should I revert this change to maintain backwards compatibility?

Wow, good find! In an ideal world we would go in and change all images to not have the .jpg suffix and patch this, so that your fix can stay and we no longer suffix our images with .jpg. Of course, there is always a risk with such a change and trying to change all these images might be more work than necessary.

Let's keep the .jpg for now! (rationale: This gets us unblocked the quickest. What might be easier is at the end of this semester, we can remove all images in firestore and start afresh next semester. And if it really bothers us this semester, we can always remove it later by writing a script to change all the images)

@patriciaahuang
Copy link
Contributor Author

I'm unable to fetch existing images anymore here. Did we change how we construct the image paths? The image paths should still be the same (to maintain backwards compatibility with the existing images we're storing), even if we're changing the endpoints.

I included the file extension in the file path to avoid assuming all images are .jpg. Before, .jpg was manually added to the file path name when getting an image in imageAPI.ts, so removing that will cause existing images to be fetched incorrectly (instead of fetching pathname.jpg it'll try fetching just pathname). Should I revert this change to maintain backwards compatibility?

Wow, good find! In an ideal world we would go in and change all images to not have the .jpg suffix and patch this, so that your fix can stay and we no longer suffix our images with .jpg. Of course, there is always a risk with such a change and trying to change all these images might be more work than necessary.

Let's keep the .jpg for now! (rationale: This gets us unblocked the quickest. What might be easier is at the end of this semester, we can remove all images in firestore and start afresh next semester. And if it really bothers us this semester, we can always remove it later by writing a script to change all the images)

Sounds good, thanks for catching this!

@@ -31,7 +31,7 @@ const UserProfileImage: React.FC = () => {
.then((res) => res.blob())
.then((blob) => {
imageURL = window.URL.createObjectURL(blob);
ImagesAPI.uploadMemberImage(blob);
ImagesAPI.uploadImage(blob, `image-signed-url/${userInfo ? userInfo.email : ''}`);
Copy link
Collaborator

Choose a reason for hiding this comment

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

we can move this prefix image-signed-url/ into the ImagesAPI.uploadImage function

@@ -63,7 +63,7 @@ const TeamEventCreditDashboard = (props: {
headerMsg: 'Team Event Attendance Deleted!',
contentMsg: 'Your team event attendance was successfully deleted!'
});
ImagesAPI.deleteEventProofImage(attendance.image);
ImagesAPI.deleteImage(`image/${attendance.image}`);
Copy link
Collaborator

Choose a reason for hiding this comment

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

we can move this prefix image/ into the ImagesAPI.deleteImage function

return this.getSignedURL().then((url) => {
const headers = { 'content-type': 'image/jpeg' };
public static uploadImage(body: Blob, name: string): Promise<void> {
return this.getSignedURL(`${name}`).then((url) => {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
return this.getSignedURL(`${name}`).then((url) => {
return this.getSignedURL(name).then((url) => {

const headers = { 'content-type': 'image/jpeg' };
public static uploadImage(body: Blob, name: string): Promise<void> {
return this.getSignedURL(`${name}`).then((url) => {
const headers = { 'content-type': `${body.type}` };
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
const headers = { 'content-type': `${body.type}` };
const headers = { 'content-type': body.type };

(res) => res.data
);
private static getSignedURL(name: string): Promise<string> {
const responseProm = APIWrapper.get(`${backendURL}/${name}`).then((res) => res.data);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
const responseProm = APIWrapper.get(`${backendURL}/${name}`).then((res) => res.data);
const responseProm = APIWrapper.get(`${backendURL}/image-signed-url/${name}`).then((res) => res.data);

return this.getSignedURL().then((url) => {
const headers = { 'content-type': 'image/jpeg' };
public static uploadImage(body: Blob, name: string): Promise<void> {
return this.getSignedURL(`image-signed-url/${name}`).then((url) => {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
return this.getSignedURL(`image-signed-url/${name}`).then((url) => {
return this.getSignedURL(name).then((url) => {

@andrew032011
Copy link
Collaborator

andrew032011 commented Oct 5, 2024

Just tested manually and if you upload and then try to get a TEC image, it does not fetch the image properly. I think it has something to do with how the file is named when it is uploaded.
Ignore me!

@patriciaahuang patriciaahuang merged commit f18e6d4 into main Oct 5, 2024
17 checks passed
@patriciaahuang patriciaahuang deleted the image-api branch October 5, 2024 05:11
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.

4 participants