All files index.js

100% Statements 32/32
100% Branches 14/14
100% Functions 8/8
100% Lines 32/32

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71        15x 15x       14x 1x   13x 3x     10x 10x 10x     10x 10x 8x 8x 6x 6x       10x           10x 10x       4x 1x     3x 3x       3x 1x   2x       1x     15x 15x 14x 4x 3x       1x    
import Minio, { Client } from 'minio';
 
class MinioWrapper {
  constructor() {
    this.client = null;
    this.initialized = false;
  }
 
  init(config) {
    if (!config) {
      throw new Error("Minio的配置不能为空");
    }
    if (!config.endPoint) {
      throw new Error("endPoint配置不能为空");
    }
 
    const endPoint = config.endPoint;
    let endPointToUse = endPoint;
    let portToUse = null;
 
    // Simple port parsing - only split if there's exactly one colon
    const parts = endPoint.split(':');
    if (parts.length === 2) {
      const port = parseInt(parts[1], 10);
      if (!isNaN(port)) {
        endPointToUse = parts[0];
        portToUse = port;
      }
    }
 
    this.client = new Client({
      ...config,
      endPoint: endPointToUse,
      ...(portToUse !== null && { port: portToUse })
    });
 
    this.initialized = true;
    return this.client;
  }
 
  putObjectBuffer(bucketName, fileName, file, meta, callback) {
    if (!this.initialized) {
      throw new Error("请先初始化Minio");
    }
 
    const buf = Buffer.from(file);
    this.client.putObject(bucketName, fileName, buf, meta, callback);
  }
 
  getClient() {
    if (!this.initialized) {
      throw new Error("请先初始化Minio");
    }
    return this.client;
  }
}
 
let minioInstance = null;
 
export function createMinio() {
  minioInstance = new MinioWrapper();
  return {
    initMinio: (config) => minioInstance.init(config),
    putObjectBuffer: (...args) => minioInstance.putObjectBuffer(...args),
    getMinioClient: () => minioInstance.getClient()
  };
}
 
const { initMinio, putObjectBuffer, getMinioClient } = createMinio();
export { initMinio, putObjectBuffer, getMinioClient, Minio };