mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-16 12:50:38 +00:00
feat: wip upload documents
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
import { Inject, Service } from 'typedi';
|
||||
import { UploadDocument } from './UploadDocument';
|
||||
import { DeleteAttachment } from './DeleteAttachment';
|
||||
import { GetAttachment } from './GetAttachment';
|
||||
import { AttachmentUploadPipeline } from './S3UploadPipeline';
|
||||
|
||||
@Service()
|
||||
export class AttachmentsApplication {
|
||||
@Inject()
|
||||
private uploadDocumentService: UploadDocument;
|
||||
|
||||
@Inject()
|
||||
private deleteDocumentService: DeleteAttachment;
|
||||
|
||||
@Inject()
|
||||
private getDocumentService: GetAttachment;
|
||||
|
||||
private uploadPipelineService: AttachmentUploadPipeline;
|
||||
|
||||
/**
|
||||
*
|
||||
* @returns
|
||||
*/
|
||||
get uploadPipeline() {
|
||||
return this.uploadPipelineService.uploadPipeline();
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {number} tenantId
|
||||
* @param {} file
|
||||
* @returns
|
||||
*/
|
||||
public upload(tenantId: number, file: any) {
|
||||
return this.uploadDocumentService.upload(tenantId, file);
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes the give file attachment file key.
|
||||
* @param {number} tenantId
|
||||
* @param {string} documentKey
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
public delete(tenantId: number, documentKey: string) {
|
||||
return this.deleteDocumentService.delete(tenantId, documentKey);
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the document data.
|
||||
* @param {number} tenantId
|
||||
* @param {string} documentKey
|
||||
*/
|
||||
public get(tenantId: number, documentKey: string) {
|
||||
return this.getDocumentService.getAttachment(tenantId, documentKey);
|
||||
}
|
||||
}
|
||||
19
packages/server/src/services/Attachments/DeleteAttachment.ts
Normal file
19
packages/server/src/services/Attachments/DeleteAttachment.ts
Normal file
@@ -0,0 +1,19 @@
|
||||
import { DeleteObjectCommand } from '@aws-sdk/client-s3';
|
||||
import { s3 } from '@/lib/S3/S3';
|
||||
import { Service } from 'typedi';
|
||||
|
||||
@Service()
|
||||
export class DeleteAttachment {
|
||||
/**
|
||||
* Deletes the give file attachment file key.
|
||||
* @param {number} tenantId
|
||||
* @param {string} filekey
|
||||
*/
|
||||
async delete(tenantId: number, filekey: string): Promise<void> {
|
||||
const params = {
|
||||
Bucket: process.env.AWS_BUCKET,
|
||||
Key: filekey,
|
||||
};
|
||||
await s3.send(new DeleteObjectCommand(params));
|
||||
}
|
||||
}
|
||||
21
packages/server/src/services/Attachments/GetAttachment.ts
Normal file
21
packages/server/src/services/Attachments/GetAttachment.ts
Normal file
@@ -0,0 +1,21 @@
|
||||
import { Service } from 'typedi';
|
||||
import { s3 } from '@/lib/S3/S3';
|
||||
import { GetObjectCommand } from '@aws-sdk/client-s3';
|
||||
|
||||
@Service()
|
||||
export class GetAttachment {
|
||||
/**
|
||||
* Retrieves data of the given document key.
|
||||
* @param {number} tenantId
|
||||
* @param {string} filekey
|
||||
*/
|
||||
async getAttachment(tenantId: number, filekey: string) {
|
||||
const params = {
|
||||
Bucket: process.env.AWS_BUCKET,
|
||||
Key: filekey,
|
||||
};
|
||||
const data = await s3.send(new GetObjectCommand(params));
|
||||
|
||||
return data;
|
||||
}
|
||||
}
|
||||
23
packages/server/src/services/Attachments/S3UploadPipeline.ts
Normal file
23
packages/server/src/services/Attachments/S3UploadPipeline.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
import multer from 'multer';
|
||||
import multerS3 from 'multer-s3';
|
||||
import { s3 } from '@/lib/S3/S3';
|
||||
import { Service } from 'typedi';
|
||||
|
||||
@Service()
|
||||
export class AttachmentUploadPipeline {
|
||||
uploadPipeline() {
|
||||
return multer({
|
||||
storage: multerS3({
|
||||
s3,
|
||||
bucket: process.env.AWS_BUCKET,
|
||||
contentType: multerS3.AUTO_CONTENT_TYPE,
|
||||
metadata: function (req, file, cb) {
|
||||
cb(null, { fieldName: file.fieldname });
|
||||
},
|
||||
key: function (req, file, cb) {
|
||||
cb(null, Date.now().toString());
|
||||
},
|
||||
}),
|
||||
});
|
||||
}
|
||||
}
|
||||
10
packages/server/src/services/Attachments/UploadDocument.ts
Normal file
10
packages/server/src/services/Attachments/UploadDocument.ts
Normal file
@@ -0,0 +1,10 @@
|
||||
import { Service } from 'typedi';
|
||||
import { S3Client, PutObjectCommand } from '@aws-sdk/client-s3';
|
||||
|
||||
@Service()
|
||||
export class UploadDocument {
|
||||
|
||||
async upload(tenantId: number, file: any) {
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user