-
Notifications
You must be signed in to change notification settings - Fork 8
Feature/hanoak #40
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
Merged
Merged
Feature/hanoak #40
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
57924ff
chore: Updated the text.
hanoak20 45b3113
feat: Added multipart upload to S3 functionality.
hanoak20 aa91081
chore: added validators to /uploads routes
hanoak20 2f7c276
chore: moved _getProject to an utility function in /utils.
hanoak20 17137cb
chore: updated the example.env file.
hanoak20 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,5 @@ | ||
| PORT= | ||
| APP_TOKEN_KEY= | ||
| MONGODB_URI= | ||
| MONGODB_URI= | ||
| UPLOAD_BUCKET= | ||
| AWS_REGION= |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| import { Request, Response } from "express"; | ||
| import { uploadsService } from "../services/projects.uploads.service"; | ||
|
|
||
| const initializeUpload = async (req: Request, res: Response) => { | ||
| const resp = await uploadsService.initializeUpload(req); | ||
| res.status(resp.status).json(resp.data); | ||
| }; | ||
|
|
||
| const getPreSignedUrls = async (req: Request, res: Response) => { | ||
| const resp = await uploadsService.getPreSignedUrls(req); | ||
| res.status(resp.status).json(resp.data); | ||
| }; | ||
| const finalizeUpload = async (req: Request, res: Response) => { | ||
| const resp = await uploadsService.finalizeUpload(req); | ||
| res.status(resp.status).json(resp.data); | ||
| }; | ||
|
|
||
| export const uploadController = { | ||
| initializeUpload, | ||
| getPreSignedUrls, | ||
| finalizeUpload, | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| import express from "express"; | ||
| import { uploadController } from "../controllers/projects.uploads.controller"; | ||
| import { asyncRouter } from "../utils/async-router.utils"; | ||
| import validator from "../validators"; | ||
|
|
||
| const router = express.Router({ mergeParams: true }); | ||
|
|
||
| // Initialize multipart upload | ||
| router.post( | ||
| "/initialize", | ||
| validator("initialize_upload"), | ||
| asyncRouter(uploadController.initializeUpload) | ||
| ); | ||
|
|
||
| // GET pre-signed urls | ||
| router.post( | ||
| "/pre-signed-urls", | ||
| validator("file_upload"), | ||
| asyncRouter(uploadController.getPreSignedUrls) | ||
| ); | ||
|
|
||
| // Finalize multipart upload | ||
| router.post( | ||
| "/finalize", | ||
| validator("file_upload"), | ||
| asyncRouter(uploadController.finalizeUpload) | ||
| ); | ||
|
|
||
| export default router; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| import { Request } from "express"; | ||
| import { EXCLUDE_CONTENT_MAPPER, HTTP_TEXTS, HTTP_CODES } from "../constants"; | ||
| import { initialize, preSignedUrls, finalize } from "../utils/s3-uploads.utils"; | ||
| import getProjectUtil from "../utils/get-project.utils"; | ||
|
|
||
| const initializeUpload = async (req: Request) => { | ||
| const orgId = req?.params?.orgId; | ||
| const projectId = req?.params?.projectId; | ||
| const fileName = req.body.file_name; | ||
| const { user_id = "", region = "" } = req.body.token_payload; | ||
|
|
||
| // Find the project based on both orgId and projectId, region, owner | ||
| await getProjectUtil( | ||
| projectId, | ||
| { | ||
| _id: projectId, | ||
| org_id: orgId, | ||
| region: region, | ||
| owner: user_id, | ||
| }, | ||
| EXCLUDE_CONTENT_MAPPER | ||
| ); | ||
|
|
||
| const result = await initialize(region, orgId, user_id, projectId, fileName); | ||
|
|
||
| return { | ||
| status: HTTP_CODES.OK, | ||
| data: { | ||
| file_id: result.UploadId, | ||
| file_key: result.Key, | ||
| }, | ||
| }; | ||
| }; | ||
|
|
||
| const getPreSignedUrls = async (req: Request) => { | ||
| const orgId = req?.params?.orgId; | ||
| const projectId = req?.params?.projectId; | ||
| const { file_key, file_id, parts } = req.body; | ||
| const { user_id = "", region = "" } = req.body.token_payload; | ||
|
|
||
| // Find the project based on both orgId and projectId, region, owner | ||
| await getProjectUtil( | ||
| projectId, | ||
| { | ||
| _id: projectId, | ||
| org_id: orgId, | ||
| region: region, | ||
| owner: user_id, | ||
| }, | ||
| EXCLUDE_CONTENT_MAPPER | ||
| ); | ||
|
|
||
| const result = await preSignedUrls(file_key, file_id, parts); | ||
|
|
||
| return { | ||
| status: HTTP_CODES.OK, | ||
| data: { parts: result }, | ||
| }; | ||
| }; | ||
|
|
||
| const finalizeUpload = async (req: Request) => { | ||
| const orgId = req?.params?.orgId; | ||
| const projectId = req?.params?.projectId; | ||
| const { file_key, file_id, parts } = req.body; | ||
| const { user_id = "", region = "" } = req.body.token_payload; | ||
|
|
||
| // Find the project based on both orgId and projectId, region, owner | ||
| await getProjectUtil( | ||
| projectId, | ||
| { | ||
| _id: projectId, | ||
| org_id: orgId, | ||
| region: region, | ||
| owner: user_id, | ||
| }, | ||
| EXCLUDE_CONTENT_MAPPER | ||
| ); | ||
|
|
||
| await finalize(file_key, file_id, parts); | ||
|
|
||
| return { | ||
| status: HTTP_CODES.OK, | ||
| data: { | ||
| message: HTTP_TEXTS.UPLOAD_SUCCESS, | ||
| }, | ||
| }; | ||
| }; | ||
|
|
||
| export const uploadsService = { | ||
| initializeUpload, | ||
| getPreSignedUrls, | ||
| finalizeUpload, | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| import ProjectModel from "../models/project"; | ||
| import { | ||
| BadRequestError, | ||
| NotFoundError, | ||
| DatabaseError, | ||
| } from "../utils/custom-errors.utils"; | ||
| import { HTTP_TEXTS } from "../constants"; | ||
| import { MigrationQueryType } from "../models/types"; | ||
| import { isValidObjectId } from "../utils"; | ||
|
|
||
| export default async ( | ||
| projectId: string, | ||
| query: MigrationQueryType, | ||
| projections: string = "" | ||
| ) => { | ||
| try { | ||
| if (!isValidObjectId(projectId)) | ||
| throw new BadRequestError(HTTP_TEXTS.INVALID_ID.replace("$", "project")); | ||
|
|
||
| const project = await ProjectModel.findOne(query).select(projections); | ||
|
|
||
| if (!project) throw new NotFoundError(HTTP_TEXTS.NO_PROJECT); | ||
|
|
||
| return project; | ||
| } catch (err) { | ||
| throw new DatabaseError(HTTP_TEXTS.SOMETHING_WENT_WRONG); | ||
| } | ||
| }; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
In every service files try to use try catch with proper logging in place, as we discussed before.