fix: bug in approve of the deposit request

This commit is contained in:
mahyargdz
2025-02-24 17:16:17 +03:30
parent d563c254e2
commit 17e39fde09
5 changed files with 57 additions and 39 deletions
+28 -11
View File
@@ -1,8 +1,8 @@
import { randomUUID } from "node:crypto";
import { extname } from "node:path";
import { PutObjectCommand, S3Client } from "@aws-sdk/client-s3";
import { Inject, Injectable } from "@nestjs/common";
import { PutObjectCommand, PutObjectCommandInput, S3Client } from "@aws-sdk/client-s3";
import { Inject, Injectable, InternalServerErrorException, Logger } from "@nestjs/common";
import { IS3Configs } from "../../../configs/s3.config";
import { S3_CONFIG } from "../constants";
@@ -10,7 +10,10 @@ import { IFile } from "../interfaces/IFile";
@Injectable()
export class S3Service {
private readonly logger = new Logger(S3Service.name);
private client: S3Client;
//
constructor(@Inject(S3_CONFIG) private s3Configs: IS3Configs) {
this.client = new S3Client({
endpoint: this.s3Configs.endpoint,
@@ -24,17 +27,31 @@ export class S3Service {
async upload(file: IFile) {
const extName = extname(file.originalname);
const params = {
const params: PutObjectCommandInput = {
Body: file.buffer,
Bucket: this.s3Configs.bucket,
Key: "images/" + randomUUID() + Date.now() + extName,
};
await this.client.send(new PutObjectCommand(params));
const url = `${this.s3Configs.url}/${params.Key}`;
return {
url,
originalname: file.originalname,
size: file.size,
Key: `images/${randomUUID()}${Date.now()}${extName}`,
ACL: "public-read",
};
//
try {
await this.client.send(new PutObjectCommand(params));
const url = `${this.s3Configs.url}/${params.Key}`;
return {
url,
originalname: file.originalname,
size: file.size,
};
} catch (error) {
this.logger.error(error);
throw new InternalServerErrorException("Failed to upload file");
}
}
async uploadMultiple(files: IFile[]) {
const uploadPromises = files.map((file) => this.upload(file));
const results = await Promise.all(uploadPromises);
return results;
}
}