chore: uploader service

This commit is contained in:
Matin
2025-01-26 16:08:52 +03:30
parent d6985fff26
commit e4425a07d6
13 changed files with 1426 additions and 6 deletions
+1
View File
@@ -1 +1,2 @@
export const SMS_CONFIG = "SMS_CONFIG";
export const S3_CONFIG = "S3_CONFIG";
+8
View File
@@ -0,0 +1,8 @@
export interface IFile {
fieldname: string;
originalname: string;
encoding: string;
mimetype: string;
buffer: Buffer;
size: number;
}
+40
View File
@@ -0,0 +1,40 @@
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 { IS3Configs } from "../../../configs/s3.config";
import { S3_CONFIG } from "../constants";
import { IFile } from "../interfaces/IFile";
@Injectable()
export class S3Service {
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 = {
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,
};
}
}
+10 -2
View File
@@ -1,10 +1,12 @@
import { Module } from "@nestjs/common";
import { SMS_CONFIG } from "./constants";
import { S3_CONFIG, SMS_CONFIG } from "./constants";
import { CacheService } from "./providers/cache.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";
@Module({
@@ -13,12 +15,18 @@ import { smsConfigs } from "../../configs/sms.config";
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, OTPService, PasswordService, CacheService, SmsService],
exports: [SMS_CONFIG, S3_CONFIG, S3Service, OTPService, PasswordService, CacheService, SmsService],
})
export class UtilsModule {}