hngprojects / hng_boilerplate_nestjs

Description
Apache License 2.0
182 stars 105 forks source link

[FEAT] Implement Endpoints for Efficient Image Serving (Upload, Retrieve, and Compression) #247

Closed thenoblet closed 2 weeks ago

thenoblet commented 1 month ago

Description

Implement endpoints to serve images in an efficient manner, ensuring fast delivery and optimized performance. This includes handling image compression, resizing, and caching.

Purpose

To provide a seamless and fast experience for users accessing images on the platform by optimizing image delivery.


Acceptance Criteria

Image Serving

Image Upload

Image Retrieval


Requirements

Image Compression and Resizing

Security


Expected Outcome


Checklist

Image Upload and Serving/Retrieval:

  1. Upload Image

    • [ ] Implement POST /api/v1/images/upload endpoint
    • [ ] Validate image type
    • [ ] Handle image compression and resizing.
    • [ ] Document endpoint, including request and response formats.
  2. Retrieve Image

    • [ ] Implement GET /api/v1/images/{id} endpoint
    • [ ] Support size and format query parameters

Testing:

Documentation:


Endpoints

Upload Image

Description: Upload an image to the server.
Endpoint: POST /api/v1/images/upload
Request (Body): multipart/form-data

{
  "image": "File"
}

Response (Success):

{
  "message": "Image uploaded successfully.",
  "imageId": "String",
  "statusCode": 201
}

Response (Error):

{
  "message": "Failed to upload image.",
  "statusCode": 500
}

Retrieve Image

Description: Retrieve an image by ID, with optional query parameters for size and format. Endpoint: GET /api/v1/images/{id} Query Parameters:

Response (Success):

HTTP/1.1 200 OK
Content-Type: image/jpeg

<binary data of the image>

Response (Error):

{
  "message": "Failed to retrieve image.",
  "statusCode": 500
}
markessien commented 1 month ago

This feature should be discussed with me before implementation. It is not well spec'd out, and this should be revised

highb33kay commented 1 month ago

Need more details about how this interacts at application level?

Are images not going to be fetched with the specific endpoints like User profile?

@thenoblet

thenoblet commented 1 month ago

Need more details about how this interacts at application level?

Are images not going to be fetched with the specific endpoints like User profile?

@thenoblet

@highb33kay, Mark suggested this endpoint be created. So I thought images would be stored on the server or a CDN, then served using efficient techniques like caching (which a friend raised a separate issue for #252 after discussing with him) and compression.

For caching, if images are stored on the server, appropriate cache headers can be set to allow browsers and CDNs to cache them. If images are stored in an S3 bucket, with CloudFront, they can be cached based on their unique slugs reducing the load on the origin server(s).