
/*=============================================================================================
	author:ZhaoJun
	version:1.2
	email:2732810281@qq.com
=============================================================================================*/


//严格模式
/*-------------------------------------------------------------------------------------------*/	
    'use strict';
/*-------------------------------------------------------------------------------------------*/	
    
const Minio=require("minio");

//redis 初始化
/*-------------------------------------------------------------------------------------------*/	    
    
class db_minio
{
    static async con() 
    {	
        const cdb=$DbMinio.$opt;

        for (const key in cdb) 
        {
            let cur=cdb[key]
            try 
            {
                $DbMinio.$con[key] = new Minio.Client(cur);
                console.info(`MINIO进程：`+process.pid+`------链接成功[${key}]`);
            } 
            catch (error) 
            {
                console.info(`MINIO进程：`+process.pid+`------链接错误[${key}]`);  
            }
        }
    }

    
    static async bucket({name,user="us-east-1",key='main'})
    {
        try 
        {
            await minioClient.bucketExists(name)   
        } 
        catch (error) 
        {
            return $DbMinio.$con[key].makeBucket(name, user)   
        }
    }

    static removeBucket({name,key='main'})
    {
        return $DbMinio.$con[key].removeBucket(name)
    }

    static upload({name,filename,file,metaData={'Content-Type': 'application/octet-stream' },key='main'})
    {
        return $DbMinio.$con[key].fPutObject(name,filename, file, metaData);
    }

    static supload({name,filename,file,metaData={'Content-Type': 'application/octet-stream' },key='main'})
    {
        return $DbMinio.$con[key].putObject(name,filename, file, metaData);
    }

    static down({name,filename,file,key='main'})
    {
        return $DbMinio.$con[key].getObject(name,filename,file);
    }

    static sdown({name,filename,start=0,end=99999999,key='main'})
    {
        //返回流
        return $DbMinio.$con[key].getPartialObject(name,filename,start,end);
    }

    static stat({name,filename,key='main'})
    {
        return $DbMinio.$con[key].statObject(name,filename,key);
    }

    static remove({name,filename,key='main'})
    {
        return $DbMinio.$con[key].removeObject(name,filename,key);
    }

    static url({name,filename,time=7*24*3600,key='main',param={}})
    {
        return new Promise
        (
            function(reslove,reject)
            {
                $DbMinio.$con[key].presignedUrl
                (
                    'GET', name, filename, time,function(err,url)
                    {
                        if(err)
                        {
                            reject(err)
                        }
                        else
                        {
                            reslove(url)
                        }
                    }
                )
            }
        )
    }
}

module.exports=db_minio;