mirror of
https://github.com/Tria-plc/edr-platform.git
synced 2026-08-29 19:58:11 +00:00
implement contract booking functionality and MinIO setup
This commit is contained in:
2
apps/edr-freight-api/src/modules/minio/index.ts
Normal file
2
apps/edr-freight-api/src/modules/minio/index.ts
Normal file
@@ -0,0 +1,2 @@
|
||||
export * from "./minio.module";
|
||||
export * from "./minio.service";
|
||||
10
apps/edr-freight-api/src/modules/minio/minio.config.ts
Normal file
10
apps/edr-freight-api/src/modules/minio/minio.config.ts
Normal file
@@ -0,0 +1,10 @@
|
||||
import { registerAs } from "@nestjs/config";
|
||||
|
||||
export const minioConfig = registerAs("minio", () => ({
|
||||
endPoint: process.env.MINIO_ENDPOINT || "minio-dev.smart.aaca.gov.et",
|
||||
port: parseInt(process.env.MINIO_PORT || "443", 10),
|
||||
useSSL: process.env.MINIO_USE_SSL !== "false",
|
||||
accessKey: process.env.MINIO_ACCESS_KEY || "",
|
||||
secretKey: process.env.MINIO_SECRET_KEY || "",
|
||||
bucket: process.env.MINIO_BUCKET || "fhc",
|
||||
}));
|
||||
11
apps/edr-freight-api/src/modules/minio/minio.module.ts
Normal file
11
apps/edr-freight-api/src/modules/minio/minio.module.ts
Normal file
@@ -0,0 +1,11 @@
|
||||
import { Module } from "@nestjs/common";
|
||||
import { ConfigModule } from "@nestjs/config";
|
||||
import { minioConfig } from "./minio.config";
|
||||
import { MinioService } from "./minio.service";
|
||||
|
||||
@Module({
|
||||
imports: [ConfigModule.forFeature(minioConfig)],
|
||||
providers: [MinioService],
|
||||
exports: [MinioService],
|
||||
})
|
||||
export class MinioModule {}
|
||||
65
apps/edr-freight-api/src/modules/minio/minio.service.ts
Normal file
65
apps/edr-freight-api/src/modules/minio/minio.service.ts
Normal file
@@ -0,0 +1,65 @@
|
||||
import { Inject, Injectable, Logger } from "@nestjs/common";
|
||||
import { ConfigType } from "@nestjs/config";
|
||||
import { Client } from "minio";
|
||||
import { minioConfig } from "./minio.config";
|
||||
|
||||
@Injectable()
|
||||
export class MinioService {
|
||||
private readonly client: Client;
|
||||
private readonly logger = new Logger(MinioService.name);
|
||||
private readonly bucket: string;
|
||||
|
||||
constructor(
|
||||
@Inject(minioConfig.KEY)
|
||||
private readonly config: ConfigType<typeof minioConfig>,
|
||||
) {
|
||||
console.log('[MinioService] Configuration loaded:', {
|
||||
endPoint: config.endPoint,
|
||||
port: config.port,
|
||||
useSSL: config.useSSL,
|
||||
accessKey: config.accessKey,
|
||||
secretKey: config.secretKey ? '***HIDDEN***' : 'EMPTY',
|
||||
bucket: config.bucket,
|
||||
});
|
||||
this.bucket = config.bucket;
|
||||
this.client = new Client({
|
||||
endPoint: config.endPoint,
|
||||
port: config.port,
|
||||
useSSL: config.useSSL,
|
||||
accessKey: config.accessKey,
|
||||
secretKey: config.secretKey,
|
||||
});
|
||||
}
|
||||
|
||||
async uploadFile(
|
||||
objectName: string,
|
||||
buffer: Buffer,
|
||||
contentType: string,
|
||||
): Promise<string> {
|
||||
try {
|
||||
await this.client.putObject(this.bucket, objectName, buffer, buffer.length, {
|
||||
"Content-Type": contentType,
|
||||
});
|
||||
this.logger.log(`File uploaded successfully: ${objectName}`);
|
||||
return this.getPublicUrl(objectName);
|
||||
} catch (error) {
|
||||
this.logger.error(`Failed to upload file ${objectName}:`, error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
getPublicUrl(objectName: string): string {
|
||||
const protocol = this.config.useSSL ? "https" : "http";
|
||||
return `${protocol}://${this.config.endPoint}:${this.config.port}/${this.bucket}/${objectName}`;
|
||||
}
|
||||
|
||||
async deleteFile(objectName: string): Promise<void> {
|
||||
try {
|
||||
await this.client.removeObject(this.bucket, objectName);
|
||||
this.logger.log(`File deleted successfully: ${objectName}`);
|
||||
} catch (error) {
|
||||
this.logger.error(`Failed to delete file ${objectName}:`, error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user