chore: add service image and audio
This commit is contained in:
@@ -1,8 +0,0 @@
|
||||
export interface IFile {
|
||||
fieldname: string;
|
||||
originalname: string;
|
||||
encoding: string;
|
||||
mimetype: string;
|
||||
buffer: Buffer;
|
||||
size: number;
|
||||
}
|
||||
@@ -1,57 +0,0 @@
|
||||
import { randomUUID } from "node:crypto";
|
||||
import { extname } from "node:path";
|
||||
|
||||
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";
|
||||
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,
|
||||
region: this.s3Configs.region,
|
||||
credentials: {
|
||||
accessKeyId: this.s3Configs.accessKeyId,
|
||||
secretAccessKey: this.s3Configs.secretAccessKey,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
async upload(file: IFile) {
|
||||
const extName = extname(file.originalname);
|
||||
const params: PutObjectCommandInput = {
|
||||
Body: file.buffer,
|
||||
Bucket: this.s3Configs.bucket,
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -1,34 +1,15 @@
|
||||
import { Module } from "@nestjs/common";
|
||||
|
||||
import { S3_CONFIG, SMS_CONFIG } from "./constants";
|
||||
import { SMS_CONFIG } from "./constants";
|
||||
import { CacheService } from "./providers/cache.service";
|
||||
import { EmailService } from "./providers/email.service";
|
||||
import { OTPService } from "./providers/otp.service";
|
||||
import { PasswordService } from "./providers/password.service";
|
||||
import { S3Service } from "./providers/s3.service";
|
||||
import { SmsService } from "./providers/sms.service";
|
||||
import { S3Configs } from "../../configs/s3.config";
|
||||
import { smsConfigs } from "../../configs/sms.config";
|
||||
import { smsConfigsProvider } from "../../configs/sms.config";
|
||||
|
||||
@Module({
|
||||
providers: [
|
||||
EmailService,
|
||||
OTPService,
|
||||
PasswordService,
|
||||
CacheService,
|
||||
SmsService,
|
||||
S3Service,
|
||||
{
|
||||
provide: SMS_CONFIG,
|
||||
useFactory: smsConfigs().useFactory,
|
||||
inject: smsConfigs().inject,
|
||||
},
|
||||
{
|
||||
provide: S3_CONFIG,
|
||||
useFactory: S3Configs().useFactory,
|
||||
inject: S3Configs().inject,
|
||||
},
|
||||
],
|
||||
exports: [SMS_CONFIG, S3_CONFIG, S3Service, OTPService, PasswordService, CacheService, SmsService, EmailService],
|
||||
providers: [EmailService, OTPService, PasswordService, CacheService, SmsService, smsConfigsProvider],
|
||||
exports: [SMS_CONFIG, OTPService, PasswordService, CacheService, SmsService, EmailService],
|
||||
})
|
||||
export class UtilsModule {}
|
||||
|
||||
Reference in New Issue
Block a user