-
Notifications
You must be signed in to change notification settings - Fork 2
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
base: main
Are you sure you want to change the base?
Conversation
[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 |
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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/
)
There was a problem hiding this comment.
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!
There was a problem hiding this comment.
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
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]; | ||
}; |
There was a problem hiding this comment.
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`); |
There was a problem hiding this comment.
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
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.