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


//严格模式
/*-------------------------------------------------------------------------------------------*/	
    'use strict';
/*-------------------------------------------------------------------------------------------*/	
    

//mongo grid初始化
/*-------------------------------------------------------------------------------------------*/	    
    
class db_grid
{
    static upload({url,db='fs',concent='main',writeConcern=true,meta={}}) 
    {	
        const mongodb=require('mongodb')
        const fs=require('fs')
        db = $DbMongo.$con[concent].db(db)
        
        const bucket = new mongodb.GridFSBucket(db,{writeConcern:writeConcern});
        
        return new Promise(function(resolve,reject){
            fs.createReadStream(url).pipe(bucket.openUploadStream(url,{metadata:meta})).
            on('error', function(error) 
            {
                resolve(false)
            }).
            on('finish', function(d) {
                resolve(d);
            });
        })
    }
    static down({id,type='',local,db='fs',concent='main',read={}}) 
    {	
        const mongodb=require('mongodb')
        const fs=require('fs')
        const ObjectID = require('mongodb').ObjectID
        db = $DbMongo.$con[concent].db(db)
        
        const bucket = new mongodb.GridFSBucket(db);
        
        return new Promise(function(resolve,reject){
            var _id=new ObjectID(id);
            if(local)
            {
                var str='';
                bucket.openDownloadStream(_id,read).
                on('data',function(d){
                    str+=d;
                }).
                on('error', function(error) 
                {
                    resolve(false)
                }).
                on('end', function() {
                    resolve(str);
                });
            }
            else
            {
                bucket.openDownloadStream(_id,read).pipe(fs.createWriteStream($Config.root+$Config.download+id+type)).
                on('error', function(error) 
                {
                    resolve(false)
                }).
                on('finish', function() {
                    resolve(true);
                });
            }
        })
    }
    static supload({req,db='fs',concent='main',writeConcern=true,meta={}}) 
    {	
        const mongodb=require('mongodb')
        db = $DbMongo.$con[concent].db(db)
        
        const bucket = new mongodb.GridFSBucket(db,{writeConcern:writeConcern});
        
        return new Promise(function(resolve,reject){
            req.pipe(bucket.openUploadStream('stream_upload',{metadata:meta})).
            on('error', function(error) 
            {
                resolve(false)
            }).
            on('finish', function(d) {
                resolve(d);
            });
        })
    }
    static sdown({res,id,db='fs',concent='main',read={}}) 
    {	
        const mongodb=require('mongodb')
        const ObjectID = require('mongodb').ObjectID
        db = $DbMongo.$con[concent].db(db)
        
        const bucket = new mongodb.GridFSBucket(db);
        
        return new Promise(function(resolve,reject){
            id=new ObjectID(id);
            bucket.openDownloadStream(id,read).pipe(res).
            on('error', function(error) 
            {
                resolve(false)
            }).
            on('finish', function(d) {
                resolve(d);
            });
        })
    }
    static delete({id,db='fs',concent='main'}) 
    {	
        if(!id) return false;
        const mongodb=require('mongodb')
        const ObjectID = require('mongodb').ObjectID
        db = $DbMongo.$con[concent].db(db)
        
        const bucket = new mongodb.GridFSBucket(db);
        
        return new Promise(function(resolve,reject){
            id=new ObjectID(id);
            bucket.delete(id,function(err){
                resolve(err)
            })
        })
    }
}

module.exports=db_grid;