feat: integrate MinIO for video storage, add video routes, and update README with API documentation
This commit is contained in:
45
Backend/utils/minio.ts
Normal file
45
Backend/utils/minio.ts
Normal file
@@ -0,0 +1,45 @@
|
||||
import { Client } from 'minio';
|
||||
|
||||
const endpoint = process.env.MINIO_ENDPOINT ?? 'localhost';
|
||||
const port = Number(process.env.MINIO_PORT ?? 9000);
|
||||
const useSSL = (process.env.MINIO_USE_SSL ?? 'false').toLowerCase() === 'true';
|
||||
const accessKey = process.env.MINIO_ACCESS_KEY;
|
||||
const secretKey = process.env.MINIO_SECRET_KEY;
|
||||
|
||||
if (!accessKey || !secretKey) {
|
||||
throw new Error('MINIO_ACCESS_KEY and MINIO_SECRET_KEY must be set');
|
||||
}
|
||||
|
||||
export const minioBucket = process.env.MINIO_BUCKET ?? 'videos';
|
||||
export const minioPresignedExpirySeconds = Number(process.env.MINIO_PRESIGNED_EXPIRY_SECONDS ?? 60 * 10);
|
||||
|
||||
export const minioClient = new Client({
|
||||
endPoint: endpoint,
|
||||
port,
|
||||
useSSL,
|
||||
accessKey,
|
||||
secretKey,
|
||||
});
|
||||
|
||||
let ensureBucketPromise: Promise<void> | null = null;
|
||||
|
||||
export const ensureMinioBucket = async (): Promise<void> => {
|
||||
if (ensureBucketPromise) {
|
||||
return ensureBucketPromise;
|
||||
}
|
||||
|
||||
ensureBucketPromise = (async () => {
|
||||
const exists = await minioClient.bucketExists(minioBucket);
|
||||
|
||||
if (!exists) {
|
||||
await minioClient.makeBucket(minioBucket, process.env.MINIO_REGION ?? 'us-east-1');
|
||||
}
|
||||
})();
|
||||
|
||||
try {
|
||||
await ensureBucketPromise;
|
||||
} catch (error) {
|
||||
ensureBucketPromise = null;
|
||||
throw error;
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user