This project is designed to handle user registration, project creation, and image management using Prisma as the ORM for database interactions, Express.js for the web server, and Sharp for image transformation. The application manages users, their associated projects, and image uploads, storing both original and transformed images in a structured folder system.
- User Creation: Create a user and set up a folder structure for storing images.
- Project Creation: Allow users to create projects, which also create associated folders for storing images related to each project.
- Image Upload & Transformation: Users can upload images, which are stored in specific project folders. The images can also be transformed (resized, formatted) before being served.
- Image Retrieval: Retrieve either original or transformed images with dynamic parameters (like width, height, format, and quality).
- Backend:
- Express.js
- Prisma ORM
- Multer for handling file uploads
- Sharp for image transformations
- Database: PostgreSQL (via Prisma Client)
- File Storage: Local disk storage
- File Format: JPEG, PNG, and other formats supported by Sharp
/
├── public/
│ └── images/
│ └── users/
│ └── <user_id>/
│ └── <project_id>/
│ └── original/
│ └── transformed/
├── src/
│ ├── routes/
│ │ ├── userRoutes.js
│ │ ├── projectRoutes.js
│ │ └── imageRoutes.js
│ ├── model/
│ │ ├── userHandler.js
│ │ └── imageHandler.js
│ ├── server.js
│ └── utils/
│ └── multer.js
├── .env
├── prisma/
│ └── schema.prisma
└── package.json- Endpoint:
POST /api/v1/user/create - Description: Creates a new user and sets up a folder for storing project-related images.
- Request Parameter:
{ "name": "John Doe", "email": "[email protected]" } - Response:
{ "message": "User created successfully", "userId": "<user_id>" }
- Endpoint:
POST /api/v1/project/create - Description: Creates a new project for a user and sets up the corresponding project folders.
- Request query:
{ "userId": "<user_id>", "projectName": "Project X" } - Response:
{ "message": "Project created successfully", "projectId": "<project_id>", "projectName": "Project X", "userId": "<user_id>" }
- Endpoint:
POST /api/v1/image/upload/:userId/:projectName - Description: Uploads an image for a specific project. The image is saved in the "original" folder of the project.
- Request: Form-data (multipart) with a file field named
file. - Response:
{ "message": "Image uploaded successfully", "filename": "<unique_file_name>" }
- Endpoint:
GET /api/v1/image/download/:userId/:projectName/:fileName - Description: Downloads a transformed image (if applicable) or the original image, applying any requested transformations (resize, format change, quality adjustments).
- Query Parameters:
w: Width of the imageh: Height of the imagef: Format (e.g., "jpeg", "png")q: Quality (1-100)
- Response: Image file (downloadable).
- A
POSTrequest is sent to/api/v1/user/createwith user details (name and email). - The server creates a user entry in the database via Prisma.
- A folder is created on the server under
public/images/users/<user_id>, where all the user's project-related images will be stored. - The user is returned with a success message and their unique user ID.
- A
POSTrequest is sent to/api/v1/project/createwith the user ID and project name. - The server creates a project entry in the database.
- Folders are created for the project under
public/images/users/<user_id>/<project_id>/originalandtransformed. - The project is returned with a success message and project details.
- A
POSTrequest is sent to/api/v1/image/upload/:userId/:projectName, with the image as a file (using form-data). - The server checks if the user exists in the database.
- The image is saved in the
originalfolder of the project. - The file is renamed to avoid conflicts and to ensure uniqueness.
- The image metadata (file name and path) is stored in the database.
- A
GETrequest is sent to/api/v1/image/download/:userId/:projectName/:fileName, with query parameters for transformations (width, height, format, quality). - The server checks if the original image exists.
- If the image needs to be transformed, the Sharp library processes the image based on the query parameters.
- The transformed image is saved in the
transformedfolder. - The image is served to the client for download.
model User {
id String @id @default(uuid())
name String
email String @unique
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
projects Project[]
}- id: Unique identifier for the user.
- name: User's name.
- email: Unique email address of the user.
- projects: Relation to the Project model.
model Project {
id String @id @default(uuid())
name String
userId String
user User @relation(fields: [userId], references: [id])
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
}- id: Unique identifier for the project.
- name: Project name.
- userId: Foreign key to the User model.
- user: Relation to the User model.
model Image {
id String @id @default(uuid())
fileName String
filePath String
userId String
createdAt DateTime @default(now())
user User @relation(fields: [userId], references: [id])
}- id: Unique identifier for the image.
- fileName: The name of the image file.
- filePath: The path where the image is stored.
- userId: Foreign key to the User model.
- user: Relation to the User model.
Ensure that the .env file contains the following environment variables:
DATABASE_URL=your_database_url
PORT=3000
This documentation provides a detailed overview of the user, project, and image management system built using Express.js, Prisma, and Sharp. The flow ensures that images are uploaded, stored, and transformed according to user specifications. By utilizing Prisma for database interactions and implementing a file system for image storage, the application offers an efficient solution for managing user data, projects, and images in a structured manner.
T