-
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?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { | ||
setImage, | ||
getImage, | ||
getAllImagesForMember, | ||
deleteImage, | ||
deleteAllImagesForMember | ||
} from '../utils/image-utils'; | ||
|
||
export const setCoffeeChatProofImage = async (name: string): Promise<string> => setImage(name); | ||
|
||
export const getCoffeeChatProofImage = async (name: string): Promise<string> => getImage(name); | ||
|
||
export const allCoffeeChatProofImagesForMember = async ( | ||
user: IdolMember | ||
): Promise<readonly Image[]> => getAllImagesForMember(user, 'coffeeChatProofs'); | ||
|
||
export const deleteCoffeeChatProofImage = async (name: string): Promise<void> => { | ||
deleteImage(name); | ||
}; | ||
|
||
export const deleteCoffeeChatProofImagesForMember = async (user: IdolMember): Promise<void> => { | ||
deleteAllImagesForMember(user, 'coffeeChatProofs'); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,97 +1,22 @@ | ||
import { bucket } from '../firebase'; | ||
import { getNetIDFromEmail } from '../utils/memberUtil'; | ||
import { NotFoundError } from '../utils/errors'; | ||
import { | ||
setImage, | ||
getImage, | ||
getAllImagesForMember, | ||
deleteImage, | ||
deleteAllImagesForMember | ||
} from '../utils/image-utils'; | ||
|
||
/** | ||
* Sets TEC proof image for member | ||
* @param name - the name of the image | ||
* @param user - the member who made the request | ||
* @returns a Promise to the signed URL to the image file | ||
*/ | ||
export const setEventProofImage = async (name: string, user: IdolMember): 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 TEC proof image for member | ||
* @param name - the name of the image | ||
* @param user - the member who made the request | ||
* @throws NotFoundError if the requested image does not exist | ||
* @returns a Promise to the signed URL to the image file | ||
*/ | ||
export const getEventProofImage = async (name: string, user: IdolMember): 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]; | ||
}; | ||
export const setEventProofImage = async (name: string): Promise<string> => setImage(name); | ||
|
||
/** | ||
* Gets all TEC proof images associated with the IdolMember | ||
* @param user - the member who made the request | ||
* @returns a Promise which results in an array of EventProofImage with file name and signed URL | ||
*/ | ||
export const allEventProofImagesForMember = async ( | ||
user: IdolMember | ||
): Promise<readonly EventProofImage[]> => { | ||
const netId: string = getNetIDFromEmail(user.email); | ||
const files = await bucket.getFiles({ prefix: `eventProofs/${netId}` }); | ||
const images = await Promise.all( | ||
files[0].map(async (file) => { | ||
const signedURL = await file.getSignedUrl({ | ||
action: 'read', | ||
expires: Date.now() + 15 * 60000 // 15 min | ||
}); | ||
const fileName = await file.getMetadata().then((data) => data[1].body.name); | ||
return { | ||
fileName, | ||
url: signedURL[0] | ||
}; | ||
}) | ||
); | ||
export const getEventProofImage = async (name: string): Promise<string> => getImage(name); | ||
|
||
images | ||
.filter((image) => image.fileName.length > 'eventProofs/'.length) | ||
.map((image) => ({ | ||
...image, | ||
fileName: image.fileName.slice(image.fileName.indexOf('/') + 1) | ||
})); | ||
|
||
return images; | ||
}; | ||
export const allEventProofImagesForMember = async (user: IdolMember): Promise<readonly Image[]> => | ||
getAllImagesForMember(user, 'eventProofs'); | ||
|
||
/** | ||
* Deletes TEC proof image for member | ||
* @param name - the name of the image | ||
* @param user - the member who made the request | ||
*/ | ||
export const deleteEventProofImage = async (name: string, user: IdolMember): Promise<void> => { | ||
const imageFile = bucket.file(`${name}.jpg`); | ||
await imageFile.delete(); | ||
export const deleteEventProofImage = async (name: string): Promise<void> => { | ||
deleteImage(name); | ||
}; | ||
|
||
/** | ||
* Deletes all TEC proof images for given member | ||
* @param user - the member who made the request | ||
*/ | ||
export const deleteEventProofImagesForMember = async (user: IdolMember): Promise<void> => { | ||
const netId: string = getNetIDFromEmail(user.email); | ||
const files = await bucket.getFiles({ prefix: `eventProofs/${netId}` }); | ||
Promise.all( | ||
files[0].map(async (file) => { | ||
file.delete(); | ||
}) | ||
); | ||
deleteAllImagesForMember(user, 'eventProofs'); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import { bucket } from '../firebase'; | ||
import { getNetIDFromEmail } from './memberUtil'; | ||
import { NotFoundError } from './errors'; | ||
|
||
/** | ||
* Sets image for member | ||
* @param name - the name of the image | ||
* @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 commentThe 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 |
||
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]; | ||
}; | ||
Comment on lines
+10
to
+37
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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)
or something a long those lines |
||
|
||
/** | ||
* Gets all images associated with the IdolMember | ||
* @param user - the member who made the request | ||
* @param type - the type of image (ex: eventProof, coffeeChatProof) | ||
* @returns a Promise which results in an array of ProofImage with file name and signed URL | ||
*/ | ||
export const getAllImagesForMember = async ( | ||
user: IdolMember, | ||
type: string | ||
): Promise<readonly Image[]> => { | ||
const netId: string = getNetIDFromEmail(user.email); | ||
const files = await bucket.getFiles({ prefix: `${type}/${netId}` }); | ||
const images = await Promise.all( | ||
files[0].map(async (file) => { | ||
const signedURL = await file.getSignedUrl({ | ||
action: 'read', | ||
expires: Date.now() + 15 * 60000 // 15 min | ||
}); | ||
const fileName = await file.getMetadata().then((data) => data[1].body.name); | ||
return { | ||
fileName, | ||
url: signedURL[0] | ||
}; | ||
}) | ||
); | ||
|
||
images | ||
.filter((image) => image.fileName.length > `${type}/`.length) | ||
.map((image) => ({ | ||
...image, | ||
fileName: image.fileName.slice(image.fileName.indexOf('/') + 1) | ||
})); | ||
|
||
return images; | ||
}; | ||
|
||
/** | ||
* Deletes image for member | ||
* @param name - the name of the image | ||
*/ | ||
export const deleteImage = async (name: string): Promise<void> => { | ||
const imageFile = bucket.file(`${name}.jpg`); | ||
await imageFile.delete(); | ||
}; | ||
|
||
/** | ||
* Deletes all images for given member | ||
* @param user - the member who made the request | ||
* @param type - the type of image (ex: eventProof, coffeeChatProof) | ||
*/ | ||
export const deleteAllImagesForMember = async (user: IdolMember, type: string): Promise<void> => { | ||
const netId: string = getNetIDFromEmail(user.email); | ||
const files = await bucket.getFiles({ prefix: `${type}/${netId}` }); | ||
Promise.all( | ||
files[0].map(async (file) => { | ||
file.delete(); | ||
}) | ||
); | ||
}; |
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