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

Dka36/EmployeePhotoS3 #558

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Open

Dka36/EmployeePhotoS3 #558

wants to merge 14 commits into from

Conversation

Dwain-Anderson
Copy link
Collaborator

@Dwain-Anderson Dwain-Anderson commented Dec 2, 2024

Implemented the ability to add employee photos using a newly created AWS-S3 bucket.
Revamped existing upload functions and added checks for existing API.

API Documentation for Image Upload Endpoint

POST /

Description:

This endpoint allows users to upload images associated with Drivers or Admins and stores the images in an Amazon S3 bucket. It then updates the respective database record with the uploaded image's URL.

Request Headers:
Header Type Description
Authorization String Required. User authentication token for validating access.
Request Body:

The request body should be sent in JSON format with the following fields:

Field Type Description
id String Required. Unique identifier for the Admin or Driver.
tableName String Required. Specifies the target table: either Drivers or Admins.
fileBuffer String Required. Base64-encoded string representing the image file to be uploaded.
Response:
Success (200):

If the image is successfully uploaded and the database is updated:

{
  "message": "Image uploaded and database updated successfully.",
  "photoLink": "https://carriage-images.s3.us-east-2.amazonaws.com/<Drivers|Admins>/<id>"
}
Error Responses:
Status Code Description Example Response
400 Invalid or missing id { "err": "Invalid ID: empty or missing" }
400 Invalid or missing fileBuffer { "err": "Invalid file buffer: empty or missing" }
400 Invalid tableName { "err": "Invalid table name: <tableName>" }
400 S3 upload error { "err": "Error message from S3" }
500 General server error { "err": "Unexpected server error occurred" }
Validation Rules:
  1. ID Validation:
    • Must be a non-empty string.
  2. File Buffer Validation:
    • Must be a non-empty Base64-encoded string.
  3. Table Name Validation:
    • Must be either Drivers or Admins.

Implementation Details:
  • AWS S3 Bucket: Images are uploaded to the S3 bucket named carriage-images in the us-east-2 region.
  • Object Key: Images are stored under <tableName>/<id>.
  • Access Control: Uploaded images are publicly readable (public-read).
  • File Size Limit: Images can be up to 50,000 MB in size.
  • Content Encoding: The fileBuffer is decoded from Base64 before being sent to S3.

Usage Notes

  • Database Update: After the image is successfully uploaded, the corresponding photoLink is saved in the database for the given Admin or Driver.
  • Permissions: Only authenticated users with the User role can access this endpoint. The uploaded image is only allowed for Drivers and Admins.

WIP/next iteration
Weird availability parse error (unrelated to this api I think)
Better cache (unsure if needed)

@Dwain-Anderson Dwain-Anderson requested a review from a team as a code owner December 2, 2024 19:25
@dti-github-bot
Copy link
Member

dti-github-bot commented Dec 2, 2024

[diff-counting] Significant lines: 421.

if (phoneNumber !== undefined) {
const fmtPhone = formatPhone(phoneNumber);
} else {
const fmtPhone = '';
Copy link
Contributor

Choose a reason for hiding this comment

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

I dont think this else statement is needed since you set fmtPhone to '' already on line 36

also, nit: try to keep all the logic for formatPhone within the helper function, ie. do the check for if the phone number is undefined within formatPhone itself, so that if the helper is used in the future, you don't have to repeat this logic

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Thanks, I'll fix it here. I left it in when debugging some error regarding workingContext sending duplicated request.

} else {
console.log('invalid file');
setErrorMessage(`Images must be under ${IMAGE_SIZE_LIMIT / 1000} KB`);
Copy link
Contributor

Choose a reason for hiding this comment

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

Hey Dwain, I checked out this branch and was having some issues uploading an image at first, so I checked the logs and got "Images must be under 50 KB," which seems to have come from here. After choosing a smaller image (20 KB) I was able to upload an image but the first one I tried was 116 KB, which is a size I think users should also be able to use to upload, so either here or in a future PR, maybe try either increasing the IMAGE_SIZE_LIMIT or investigating why the if statement would be returning false here.
Otherwise, good work on figuring out the backend and getting the uploading images to work!!

Copy link
Collaborator Author

@Dwain-Anderson Dwain-Anderson Dec 18, 2024

Choose a reason for hiding this comment

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

Hello, the img size limit was set prior, in server/app.ts its set to 5000kb and anything above it throws a payload size error. I can ask Desmond about changing that directly. Otherwise I'll look into other ways to get past it when i add the optimizations. Also thank you!

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.

3 participants