mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-17 05:10:31 +00:00
fix: TS types
This commit is contained in:
@@ -1,9 +1,8 @@
|
||||
import mime from 'mime-types';
|
||||
import { Service, Inject } from 'typedi';
|
||||
import { Router, Response } from 'express';
|
||||
import { Router, Response, NextFunction, Request } 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()
|
||||
@@ -20,6 +19,7 @@ export class AttachmentsController extends BaseController {
|
||||
router.post(
|
||||
'/',
|
||||
this.attachmentsApplication.uploadPipeline.single('file'),
|
||||
this.validateUploadedFileExistance,
|
||||
this.uploadAttachment.bind(this)
|
||||
);
|
||||
router.delete(
|
||||
@@ -57,18 +57,42 @@ export class AttachmentsController extends BaseController {
|
||||
this.validationResult,
|
||||
this.getAttachmentPresignedUrl.bind(this)
|
||||
);
|
||||
|
||||
return router;
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates the upload file existance.
|
||||
* @param {Request} req
|
||||
* @param {Response} res
|
||||
* @param {NextFunction} next
|
||||
* @returns {Response|void}
|
||||
*/
|
||||
private validateUploadedFileExistance(
|
||||
req: Request,
|
||||
res: Response,
|
||||
next: NextFunction
|
||||
) {
|
||||
if (!req.file) {
|
||||
return res.boom.badRequest(null, {
|
||||
errorType: 'FILE_UPLOAD_FAILED',
|
||||
message: 'Now file uploaded.',
|
||||
});
|
||||
}
|
||||
next();
|
||||
}
|
||||
|
||||
/**
|
||||
* Uploads the attachments to S3 and store the file metadata to DB.
|
||||
* @param {Request} req
|
||||
* @param {Response} res
|
||||
* @param {Function} next
|
||||
* @returns
|
||||
* @param {NextFunction} next
|
||||
* @returns {Response|void}
|
||||
*/
|
||||
private async uploadAttachment(req: Request, res: Response, next: Function) {
|
||||
private async uploadAttachment(
|
||||
req: Request,
|
||||
res: Response,
|
||||
next: NextFunction
|
||||
): Promise<Response | void> {
|
||||
const { tenantId } = req;
|
||||
const file = req.file;
|
||||
|
||||
@@ -86,12 +110,17 @@ export class AttachmentsController extends BaseController {
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* Retrieves the given attachment key.
|
||||
* @param {Request} req
|
||||
* @param {Response} res
|
||||
* @param next
|
||||
* @param {NextFunction} next
|
||||
* @returns {Promise<Response|void>}
|
||||
*/
|
||||
private async getAttachment(req: Request, res: Response, next: Function) {
|
||||
private async getAttachment(
|
||||
req: Request,
|
||||
res: Response,
|
||||
next: NextFunction
|
||||
): Promise<Response | void> {
|
||||
const { tenantId } = req;
|
||||
const { id } = req.params;
|
||||
|
||||
@@ -118,9 +147,13 @@ export class AttachmentsController extends BaseController {
|
||||
* @param {Request} req
|
||||
* @param {Response} res
|
||||
* @param {NextFunction} next
|
||||
* @returns
|
||||
* @returns {Promise<Response|void>}
|
||||
*/
|
||||
private async deleteAttachment(req: Request, res: Response, next: Function) {
|
||||
private async deleteAttachment(
|
||||
req: Request,
|
||||
res: Response,
|
||||
next: NextFunction
|
||||
): Promise<Response | void> {
|
||||
const { tenantId } = req;
|
||||
const { id: documentId } = req.params;
|
||||
|
||||
@@ -141,9 +174,13 @@ export class AttachmentsController extends BaseController {
|
||||
* @param {Request} req
|
||||
* @param {Response} res
|
||||
* @param {NextFunction} next
|
||||
* @returns
|
||||
* @returns {Promise<Response|void>}
|
||||
*/
|
||||
private async linkDocument(req: Request, res: Response, next: Function) {
|
||||
private async linkDocument(
|
||||
req: Request,
|
||||
res: Response,
|
||||
next: Function
|
||||
): Promise<Response | void> {
|
||||
const { tenantId } = req;
|
||||
const { id: documentId } = req.params;
|
||||
const { modelRef, modelId } = this.matchedBodyData(req);
|
||||
@@ -169,9 +206,13 @@ export class AttachmentsController extends BaseController {
|
||||
* @param {Request} req
|
||||
* @param {Response} res
|
||||
* @param {NextFunction} next
|
||||
* @returns
|
||||
* @returns {Promise<Response|void>}
|
||||
*/
|
||||
private async unlinkDocument(req: Request, res: Response, next: Function) {
|
||||
private async unlinkDocument(
|
||||
req: Request,
|
||||
res: Response,
|
||||
next: NextFunction
|
||||
): Promise<Response | void> {
|
||||
const { tenantId } = req;
|
||||
const { id: documentId } = req.params;
|
||||
const { modelRef, modelId } = this.matchedBodyData(req);
|
||||
@@ -196,13 +237,14 @@ export class AttachmentsController extends BaseController {
|
||||
* Retreives the presigned url of the given attachment key.
|
||||
* @param {Request} req
|
||||
* @param {Response} res
|
||||
* @param next
|
||||
* @param {NextFunction} next
|
||||
* @returns {Promise<Response|void>}
|
||||
*/
|
||||
private async getAttachmentPresignedUrl(
|
||||
req: Request,
|
||||
res: Response,
|
||||
next: any
|
||||
) {
|
||||
next: NextFunction
|
||||
): Promise<Response | void> {
|
||||
const { id: documentKey } = req.params;
|
||||
|
||||
try {
|
||||
|
||||
@@ -1,10 +1,11 @@
|
||||
import { S3Client } from '@aws-sdk/client-s3';
|
||||
import config from '@/config';
|
||||
|
||||
export const s3 = new S3Client({
|
||||
region: process.env.AWS_REGION,
|
||||
region: config.s3.region,
|
||||
credentials: {
|
||||
accessKeyId: process.env.AWS_ACCESS_KEY_ID as string,
|
||||
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY as string,
|
||||
accessKeyId: config.s3.accessKeyId,
|
||||
secretAccessKey: config.s3.secretAccessKey,
|
||||
},
|
||||
endpoint: process.env.AWS_ENDPOINT,
|
||||
endpoint: config.s3.endpoint,
|
||||
});
|
||||
|
||||
@@ -6,6 +6,7 @@ import { AttachmentUploadPipeline } from './S3UploadPipeline';
|
||||
import { LinkAttachment } from './LinkAttachment';
|
||||
import { UnlinkAttachment } from './UnlinkAttachment';
|
||||
import { getAttachmentPresignedUrl } from './GetAttachmentPresignedUrl';
|
||||
import type { Multer } from 'multer';
|
||||
|
||||
@Service()
|
||||
export class AttachmentsApplication {
|
||||
@@ -31,18 +32,18 @@ export class AttachmentsApplication {
|
||||
private getPresignedUrlService: getAttachmentPresignedUrl;
|
||||
|
||||
/**
|
||||
*
|
||||
* @returns
|
||||
* Express middleware for uploading attachments to an S3 bucket.
|
||||
* @returns {Multer}
|
||||
*/
|
||||
get uploadPipeline() {
|
||||
get uploadPipeline(): Multer {
|
||||
return this.uploadPipelineService.uploadPipeline();
|
||||
}
|
||||
|
||||
/**
|
||||
* Uploads
|
||||
* Saves the metadata of uploaded document to S3 on database.
|
||||
* @param {number} tenantId
|
||||
* @param {} file
|
||||
* @returns
|
||||
* @returns {Promise<Document>}
|
||||
*/
|
||||
public upload(tenantId: number, file: any) {
|
||||
return this.uploadDocumentService.upload(tenantId, file);
|
||||
|
||||
@@ -1,15 +1,21 @@
|
||||
import multer from 'multer';
|
||||
import type { Multer } from 'multer'
|
||||
import multerS3 from 'multer-s3';
|
||||
import { s3 } from '@/lib/S3/S3';
|
||||
import { Service } from 'typedi';
|
||||
import config from '@/config';
|
||||
|
||||
@Service()
|
||||
export class AttachmentUploadPipeline {
|
||||
uploadPipeline() {
|
||||
/**
|
||||
* Express middleware for uploading attachments to an S3 bucket.
|
||||
* It utilizes the multer middleware for handling multipart/form-data, specifically for file uploads.
|
||||
*/
|
||||
public uploadPipeline(): Multer {
|
||||
return multer({
|
||||
storage: multerS3({
|
||||
s3,
|
||||
bucket: process.env.AWS_BUCKET,
|
||||
bucket: config.s3.bucket,
|
||||
contentType: multerS3.AUTO_CONTENT_TYPE,
|
||||
metadata: function (req, file, cb) {
|
||||
cb(null, { fieldName: file.fieldname });
|
||||
|
||||
Reference in New Issue
Block a user