unreal-chat/apps/api/src/services/minio.service.ts
Juan Sebastian Montoya 857ffcd6b4
Some checks failed
CI / test (push) Failing after 43s
chore: add ESLint configuration for api and web packages
- Introduced ESLint configuration files for both api and web packages to enforce coding standards.
- Updated MinioService and TokenService to improve error handling and type definitions.
- Refactored localStorage access in web components to use window.localStorage for consistency.
- Enhanced ESLint rules in base configuration to improve code quality and maintainability.
2025-05-06 01:16:16 -05:00

106 lines
2.9 KiB
TypeScript

import { Client } from 'minio';
import { MinioConfig } from '../config';
/**
* Service for handling MinIO operations
*/
export class MinioService {
private client: Client;
private bucketName: string;
private readonly defaultExpiry = 60 * 60; // 1 hour in seconds
private initialized = false;
/**
* Creates a new MinioService instance
* @param config - MinIO configuration
*/
constructor(config: MinioConfig) {
this.client = new Client(config);
this.bucketName = config.bucketName;
}
/**
* Initializes the MinIO service by ensuring the bucket exists
*/
public async initialize(): Promise<void> {
if (this.initialized) {
return;
}
const bucketExists = await this.client.bucketExists(this.bucketName);
if (!bucketExists) {
await this.client.makeBucket(this.bucketName, 'us-east-1');
// Set the bucket policy to allow public read access
const policy = {
Version: '2012-10-17',
Statement: [
{
Effect: 'Allow',
Principal: { AWS: ['*'] },
Action: ['s3:GetObject'],
Resource: [`arn:aws:s3:::${this.bucketName}/*`],
},
],
};
await this.client.setBucketPolicy(
this.bucketName,
JSON.stringify(policy)
);
}
this.initialized = true;
}
/**
* Generates a presigned URL for uploading a file
* @param objectName - Name of the object to upload
* @param expiryInSeconds - Expiry time in seconds (default: 1 hour)
* @returns Presigned URL for uploading
*/
public async generateUploadUrl(
objectName: string,
expiryInSeconds: number = this.defaultExpiry
): Promise<string> {
return this.client.presignedPutObject(
this.bucketName,
objectName,
expiryInSeconds
);
}
/**
* Generates a presigned URL for downloading a file
* @param objectName - Name of the object to download
* @param expiryInSeconds - Expiry time in seconds (default: 1 hour)
* @returns Presigned URL for downloading
*/
public async generateDownloadUrl(
objectName: string,
expiryInSeconds: number = this.defaultExpiry
): Promise<string> {
return this.client.presignedGetObject(
this.bucketName,
objectName,
expiryInSeconds
);
}
/**
* Checks if an object exists in the bucket
* @param objectName - Name of the object to check
* @returns True if the object exists, false otherwise
*/
public async objectExists(objectName: string): Promise<boolean> {
try {
await this.client.statObject(this.bucketName, objectName);
return true;
} catch (_error) {
return false;
}
}
/**
* Deletes an object from the bucket
* @param objectName - Name of the object to delete
*/
public async deleteObject(objectName: string): Promise<void> {
await this.client.removeObject(this.bucketName, objectName);
}
}