mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-16 21:00:31 +00:00
feat: getting presigned url of the uploaded attachment
This commit is contained in:
@@ -5,6 +5,7 @@ import { GetAttachment } from './GetAttachment';
|
||||
import { AttachmentUploadPipeline } from './S3UploadPipeline';
|
||||
import { LinkAttachment } from './LinkAttachment';
|
||||
import { UnlinkAttachment } from './UnlinkAttachment';
|
||||
import { getAttachmentPresignedUrl } from './GetAttachmentPresignedUrl';
|
||||
|
||||
@Service()
|
||||
export class AttachmentsApplication {
|
||||
@@ -26,6 +27,9 @@ export class AttachmentsApplication {
|
||||
@Inject()
|
||||
private unlinkDocumentService: UnlinkAttachment;
|
||||
|
||||
@Inject()
|
||||
private getPresignedUrlService: getAttachmentPresignedUrl;
|
||||
|
||||
/**
|
||||
*
|
||||
* @returns
|
||||
@@ -101,4 +105,13 @@ export class AttachmentsApplication {
|
||||
modelId
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the presigned url of the given attachment key.
|
||||
* @param {string} key
|
||||
* @returns {Promise<string>}
|
||||
*/
|
||||
public getPresignedUrl(key: string): Promise<string> {
|
||||
return this.getPresignedUrlService.getPresignedUrl(key);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
import { GetObjectCommand } from '@aws-sdk/client-s3';
|
||||
import { getSignedUrl } from '@aws-sdk/s3-request-presigner';
|
||||
import { s3 } from '@/lib/S3/S3';
|
||||
import { Service } from 'typedi';
|
||||
|
||||
@Service()
|
||||
export class getAttachmentPresignedUrl {
|
||||
/**
|
||||
* Retrieves the presigned url of the given attachment key.
|
||||
* @param {string} key
|
||||
* @returns {Promise<string?>}
|
||||
*/
|
||||
async getPresignedUrl(key: string) {
|
||||
const params = {
|
||||
Bucket: process.env.AWS_BUCKET,
|
||||
Key: key,
|
||||
Expires: 60 * 5, // 5 minutes
|
||||
};
|
||||
const command = new GetObjectCommand({
|
||||
Bucket: process.env.AWS_BUCKET,
|
||||
Key: key,
|
||||
});
|
||||
const signedUrl = await getSignedUrl(s3, command, { expiresIn: 300 });
|
||||
|
||||
return signedUrl;
|
||||
}
|
||||
}
|
||||
@@ -6,12 +6,20 @@ export class UploadDocument {
|
||||
@Inject()
|
||||
private tenancy: HasTenancyService;
|
||||
|
||||
/**
|
||||
* Inserts the document metadata.
|
||||
* @param {number} tenantId
|
||||
* @param {} file
|
||||
* @returns {}
|
||||
*/
|
||||
async upload(tenantId: number, file: any) {
|
||||
const { Document } = this.tenancy.models(tenantId);
|
||||
|
||||
const insertedDocument = await Document.query().insert({
|
||||
key: file.key,
|
||||
extension: file.mimetype,
|
||||
mimeType: file.mimetype,
|
||||
size: file.size,
|
||||
originName: file.originalname,
|
||||
});
|
||||
return insertedDocument;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user