made the presigned url middleware
This commit is contained in:
18
src/common/middlewares/aws/getPreSignedUrl.ts
Normal file
18
src/common/middlewares/aws/getPreSignedUrl.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
// common/utils/awsPresign.ts
|
||||
import config from "@/config/config";
|
||||
import { S3Client, GetObjectCommand } from "@aws-sdk/client-s3";
|
||||
import { getSignedUrl } from "@aws-sdk/s3-request-presigner";
|
||||
|
||||
const s3 = new S3Client({
|
||||
region: config.aws.region, // e.g. ap-south-1
|
||||
});
|
||||
|
||||
export const getPresignedUrl = async (bucket: string, key: string) => {
|
||||
const command = new GetObjectCommand({
|
||||
Bucket: bucket,
|
||||
Key: key,
|
||||
});
|
||||
|
||||
// URL valid for 1 hour
|
||||
return await getSignedUrl(s3, command, { expiresIn: 3600 });
|
||||
};
|
||||
@@ -10,6 +10,8 @@ import { hostCompanyDetailsSchema } from '@/common/utils/validation/host/hostCom
|
||||
import { HOST_STATUS_DISPLAY, HOST_STATUS_INTERNAL, STEPPER } from '@/common/utils/constants/host.constant';
|
||||
import { MINGLAR_STATUS_DISPLAY, MINGLAR_STATUS_INTERNAL } from '@/common/utils/constants/minglar.constant';
|
||||
import { ROLE } from '@/common/utils/constants/common.constant';
|
||||
import { getPresignedUrl } from '@/common/middlewares/aws/getPreSignedUrl';
|
||||
import config from '@/config/config';
|
||||
|
||||
type HostCompanyDetailsInput = z.infer<typeof hostCompanyDetailsSchema>;
|
||||
|
||||
@@ -58,6 +60,23 @@ export class HostService {
|
||||
return { stepper: STEPPER.NOT_SUBMITTED } as any;
|
||||
}
|
||||
|
||||
if (host.HostDocuments?.length) {
|
||||
const bucket = config.aws.bucketName;
|
||||
|
||||
for (const doc of host.HostDocuments) {
|
||||
if (doc.filePath) {
|
||||
const filePath = doc.filePath;
|
||||
|
||||
// If full URL is saved, extract only key
|
||||
const key = filePath.startsWith("http")
|
||||
? filePath.split(".com/")[1]
|
||||
: filePath;
|
||||
|
||||
(doc as any).presignedUrl = await getPresignedUrl(bucket, key);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return host;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user