feat: wip upload documents

This commit is contained in:
Ahmed Bouhuolia
2024-05-24 14:28:21 +02:00
parent 1227111fae
commit c8f31f33be
13 changed files with 6353 additions and 6211 deletions

View File

@@ -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);
}
}

View 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));
}
}

View 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;
}
}

View 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());
},
}),
});
}
}

View 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) {
}
}