-
-
Notifications
You must be signed in to change notification settings - Fork 251
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
feat: upload and attach documents #461
Merged
Merged
Changes from 10 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
c8f31f3
feat: wip upload documents
abouolia 8f904fa
feat: link and unlink document to resource model
abouolia 15dbc41
Merge branch 'develop' into big-157-attach-documents-to-transactions
abouolia 2244cc6
feat: wip attach attachments to resource models
abouolia fcd61c6
feat: wip UI attachments
abouolia cfdbcea
feat: wip UI upload attachments
abouolia e7871e3
feat: wip upload attachmentsx
abouolia ceb133e
feat: getting presigned url of the uploaded attachment
abouolia 6e50de1
feat: style tweaks to upload attachments popover
abouolia 308a4f6
feat: catch exceptions link attachment service
abouolia 6a6dcad
fix: TS types
abouolia 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 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
217 changes: 217 additions & 0 deletions
217
packages/server/src/api/controllers/Attachments/AttachmentsController.ts
This file contains 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,217 @@ | ||
import mime from 'mime-types'; | ||
import { Service, Inject } from 'typedi'; | ||
import { Router, Response } from 'express'; | ||
import { body, param } from 'express-validator'; | ||
import BaseController from '@/api/controllers/BaseController'; | ||
import { Request } from 'express-validator/src/base'; | ||
import { AttachmentsApplication } from '@/services/Attachments/AttachmentsApplication'; | ||
|
||
@Service() | ||
export class AttachmentsController extends BaseController { | ||
@Inject() | ||
private attachmentsApplication: AttachmentsApplication; | ||
|
||
/** | ||
* Router constructor. | ||
*/ | ||
public router() { | ||
const router = Router(); | ||
|
||
router.post( | ||
'/', | ||
this.attachmentsApplication.uploadPipeline.single('file'), | ||
this.uploadAttachment.bind(this) | ||
); | ||
router.delete( | ||
'/:id', | ||
[param('id').exists()], | ||
this.validationResult, | ||
this.deleteAttachment.bind(this) | ||
); | ||
router.get( | ||
'/:id', | ||
[param('id').exists()], | ||
this.validationResult, | ||
this.getAttachment.bind(this) | ||
); | ||
router.post( | ||
'/:id/link', | ||
[body('modelRef').exists(), body('modelId').exists()], | ||
this.validationResult | ||
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. Duplicated code. |
||
); | ||
router.post( | ||
'/:id/link', | ||
[body('modelRef').exists(), body('modelId').exists()], | ||
this.validationResult, | ||
this.linkDocument.bind(this) | ||
); | ||
router.post( | ||
'/:id/unlink', | ||
[body('modelRef').exists(), body('modelId').exists()], | ||
this.validationResult, | ||
this.unlinkDocument.bind(this) | ||
); | ||
router.get( | ||
'/:id/presigned-url', | ||
[param('id').exists()], | ||
this.validationResult, | ||
this.getAttachmentPresignedUrl.bind(this) | ||
); | ||
|
||
return router; | ||
} | ||
|
||
/** | ||
* Uploads the attachments to S3 and store the file metadata to DB. | ||
* @param {Request} req | ||
* @param {Response} res | ||
* @param {Function} next | ||
* @returns | ||
*/ | ||
private async uploadAttachment(req: Request, res: Response, next: Function) { | ||
const { tenantId } = req; | ||
const file = req.file; | ||
|
||
try { | ||
const data = await this.attachmentsApplication.upload(tenantId, file); | ||
|
||
return res.status(200).send({ | ||
status: 200, | ||
message: 'The document has uploaded successfully.', | ||
data, | ||
}); | ||
} catch (error) { | ||
next(error); | ||
} | ||
} | ||
|
||
/** | ||
* | ||
abouolia marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* @param {Request} req | ||
* @param {Response} res | ||
* @param next | ||
*/ | ||
private async getAttachment(req: Request, res: Response, next: Function) { | ||
const { tenantId } = req; | ||
const { id } = req.params; | ||
|
||
try { | ||
const data = await this.attachmentsApplication.get(tenantId, id); | ||
|
||
const byte = await data.Body.transformToByteArray(); | ||
const extension = mime.extension(data.ContentType); | ||
const buffer = Buffer.from(byte); | ||
|
||
res.set( | ||
'Content-Disposition', | ||
`filename="${req.params.id}.${extension}"` | ||
); | ||
res.set('Content-Type', data.ContentType); | ||
res.send(buffer); | ||
} catch (error) { | ||
next(error); | ||
} | ||
} | ||
|
||
/** | ||
* Deletes the given document key. | ||
* @param {Request} req | ||
* @param {Response} res | ||
* @param {NextFunction} next | ||
* @returns | ||
*/ | ||
private async deleteAttachment(req: Request, res: Response, next: Function) { | ||
abouolia marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const { tenantId } = req; | ||
const { id: documentId } = req.params; | ||
|
||
try { | ||
await this.attachmentsApplication.delete(tenantId, documentId); | ||
|
||
return res.status(200).send({ | ||
status: 200, | ||
message: 'The document has been delete successfully.', | ||
}); | ||
} catch (error) { | ||
next(error); | ||
} | ||
} | ||
|
||
/** | ||
* Links the given document key. | ||
* @param {Request} req | ||
* @param {Response} res | ||
* @param {NextFunction} next | ||
* @returns | ||
*/ | ||
private async linkDocument(req: Request, res: Response, next: Function) { | ||
const { tenantId } = req; | ||
const { id: documentId } = req.params; | ||
const { modelRef, modelId } = this.matchedBodyData(req); | ||
|
||
try { | ||
await this.attachmentsApplication.link( | ||
tenantId, | ||
documentId, | ||
modelRef, | ||
modelId | ||
); | ||
return res.status(200).send({ | ||
status: 200, | ||
message: 'The document has been linked successfully.', | ||
}); | ||
} catch (error) { | ||
next(error); | ||
} | ||
} | ||
|
||
/** | ||
* Links the given document key. | ||
* @param {Request} req | ||
* @param {Response} res | ||
* @param {NextFunction} next | ||
* @returns | ||
*/ | ||
private async unlinkDocument(req: Request, res: Response, next: Function) { | ||
const { tenantId } = req; | ||
const { id: documentId } = req.params; | ||
const { modelRef, modelId } = this.matchedBodyData(req); | ||
|
||
try { | ||
await this.attachmentsApplication.link( | ||
tenantId, | ||
documentId, | ||
modelRef, | ||
modelId | ||
); | ||
return res.status(200).send({ | ||
status: 200, | ||
message: 'The document has been linked successfully.', | ||
}); | ||
} catch (error) { | ||
next(error); | ||
} | ||
} | ||
|
||
/** | ||
* Retreives the presigned url of the given attachment key. | ||
* @param {Request} req | ||
* @param {Response} res | ||
* @param next | ||
*/ | ||
private async getAttachmentPresignedUrl( | ||
req: Request, | ||
res: Response, | ||
next: any | ||
) { | ||
const { id: documentKey } = req.params; | ||
|
||
try { | ||
const presignedUrl = await this.attachmentsApplication.getPresignedUrl( | ||
documentKey | ||
); | ||
return res.status(200).send({ presignedUrl }); | ||
} catch (error) { | ||
next(error); | ||
} | ||
} | ||
} |
This file contains 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 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 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 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 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
Oops, 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.
Validate the file should exist.