
import oss2
import traceback
from BaseDataMaintenance.common.Utils import log
import time
import os

current_path = os.path.dirname(__file__)

def does_bucket_exist(bucket):
    try:
        bucket.get_bucket_info()

    except oss2.exceptions.NoSuchBucket:
        return False
    except:
        raise
    return True

import hashlib
def getMDFFromFile(path):
    _length = 0
    try:
        _md5 = hashlib.md5()
        with open(path,"rb") as f:
            while True:
                data = f.read(4096)
                if not data:
                    break
                _length += len(data)
                _md5.update(data)
        return _md5.hexdigest(),_length
    except Exception as e:
        traceback.print_exc()
        return None,_length

def uploadFileByPath(bucket,filepath,uploadpath,headers=None,trytimes=3):
    for _i in range(trytimes):
        try:
            start_time = time.time()
            log("uploading file of %s"%filepath)
            with open(filepath,"rb") as f:
                bucket.put_object(uploadpath,f,headers=headers)
            log("upload file of %s takes %ds"%(filepath,time.time()-start_time))
            return True
        except Exception as e:
            traceback.print_exc()
            log("upload object failed of %s"%(filepath))
    return False

def deleteObject(bucket,objectName):
    try:
        bucket.delete_object(objectName)
    except Exception as e:
        log("delete object failed of %s"%objectName)

def downloadFile(bucket,objectPath,localPath,retry=3):
    for i in range(retry):
        try:
            # bucket.get_object_to_file(objectPath, localPath)
            oss2.resumable_download(bucket, objectPath, localPath,
                                    store=oss2.ResumableDownloadStore(),
                                    num_threads=1)
            return True
        except oss2.exceptions.NotFound as e:
            log("file not found:%s"%(objectPath))
            raise e
        except Exception as e:
            traceback.print_exc()
            log("download object failed of %s"%str(objectPath))
    return False


def getBucketConnect():
    from BaseDataMaintenance.dataSource.source import getAuth,getConnect_ots,is_internal

    auth = getAuth()

    if is_internal:
        bucket_url = "http://oss-cn-hangzhou-internal.aliyuncs.com"
    else:
        bucket_url = "http://oss-cn-hangzhou.aliyuncs.com"
    attachment_bucket_name = "attachment-hub"
    log("-----===---bucket_url:%s"%(bucket_url))
    bucket = oss2.Bucket(auth,bucket_url,attachment_bucket_name)
    ots_client = getConnect_ots()
    return bucket,ots_client


bucket,ots_client = getBucketConnect()

def test_download(filemd5):
    from BaseDataMaintenance.model.ots.attachment import attachment,attachment_filemd5,attachment_path,attachment_filetype

    atta = attachment({attachment_filemd5:filemd5})
    atta.fix_columns(ots_client,[attachment_path,attachment_filetype],True)
    _filetype = atta.getProperties().get(attachment_filetype)
    _path = "%s/%s/%s.%s"%(os.path.dirname(__file__),"download",filemd5,_filetype)
    object_path = atta.getProperties().get(attachment_path)
    try:
        downloadFile(bucket,object_path,_path)
    except Exception as e:
        print(str(e))
    return _path


if __name__=="__main__":
    # print(getMDFFromFile('8a9c96a68803c2ad01881d0ee93618e5.pdf'))
    test_download("892bde698088f1d61b5310782550d0e1")
    # print(bucket.sign_url("GET","0015//20220623/2022-06-22/WGH001018/1655926900020.png",86500*30))
    # print(time.strftime("%Y-%m-%d",time.localtime(1658655178)))