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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open

Unify Logic in Image APIs #647

wants to merge 4 commits into from

Conversation

patriciaahuang
Copy link
Contributor

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.

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

[diff-counting] Significant lines: 164.

@@ -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

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