-
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
Conversation
[diff-counting] Significant lines: 144. |
backend/src/api.ts
Outdated
@@ -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
backend/src/utils/image-utils.ts
Outdated
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
backend/src/utils/image-utils.ts
Outdated
* @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
backend/src/API/imageAPI.ts
Outdated
}) | ||
); | ||
|
||
images |
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.
I think this value looks like it's floating and nothing is assigned to it?
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 |
Wow, good find! In an ideal world we would go in and change all images to not have the Let's keep the |
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 : ''}`); |
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.
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}`); |
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.
we can move this prefix image/
into the ImagesAPI.deleteImage function
frontend/src/API/ImagesAPI.ts
Outdated
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) => { |
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.
return this.getSignedURL(`${name}`).then((url) => { | |
return this.getSignedURL(name).then((url) => { |
frontend/src/API/ImagesAPI.ts
Outdated
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}` }; |
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.
const headers = { 'content-type': `${body.type}` }; | |
const headers = { 'content-type': body.type }; |
frontend/src/API/ImagesAPI.ts
Outdated
(res) => res.data | ||
); | ||
private static getSignedURL(name: string): Promise<string> { | ||
const responseProm = APIWrapper.get(`${backendURL}/${name}`).then((res) => res.data); |
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.
const responseProm = APIWrapper.get(`${backendURL}/${name}`).then((res) => res.data); | |
const responseProm = APIWrapper.get(`${backendURL}/image-signed-url/${name}`).then((res) => res.data); |
frontend/src/API/ImagesAPI.ts
Outdated
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) => { |
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.
return this.getSignedURL(`image-signed-url/${name}`).then((url) => { | |
return this.getSignedURL(name).then((url) => { |
|
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.