# -*- coding: utf-8 -*-
import os
import oss2
import requests
import uuid

# from common import convert_to_pdf


class MyOSS:
    def __init__(self):
        self.AccessKeyID = 'LTAIPp6YI1JUzpz2'
        self.AccessKeySecret = 'UWc04c4TpUeVot9cYqbkRpU4YhE25S'
        self.bucket_name = 'tederen'
        self.root_name = 'xdcjing'
        self.auth = oss2.Auth(self.AccessKeyID, self.AccessKeySecret)
        self.endpoint = 'http://oss-cn-shanghai.aliyuncs.com'
        self.bucket = oss2.Bucket(self.auth, self.endpoint, self.bucket_name)
        self.domain = 'http://tederen.oss-cn-shanghai.aliyuncs.com'

    def upload_from_str(self,content_str=None,filename=None):
        '''
        通过字符串上传
        byte,unicode,str
        '''
        filename = os.path.join(self.root_name,filename)

        result = self.bucket.put_object(filename,content_str)

        if result.status == 200:
            url = os.path.join(self.domain,filename)
        else:
            url = ''

        return url

    def upload_from_local(self, localfile=None, ossfile=None):
        '''
        上传本地文件到oss
        '''
        ossfile = os.path.join(self.root_name, ossfile)

        with open(localfile, 'rb') as fileobj:
            result = self.bucket.put_object(ossfile, fileobj)

        if result.status == 200:
            url = os.path.join(self.domain, ossfile)
        else:
            url = ''

        return url

    def resumable_upload_from_local(self, localfile=None, ossfile=None):
        ossfile = os.path.join(self.root_name, ossfile)

        result = oss2.resumable_upload(self.bucket,ossfile,localfile,
            store=oss2.ResumableStore(root='/tmp'), 
            multipart_threshold=100*1024,
            part_size=100*1024,
            num_threads=4,
            progress_callback=self.progress_callback
        )

        if result.status == 200:
            url = os.path.join(self.domain,ossfile)
        else:
            url = ''

        return url

    def upload_from_url(self, url=None, ossfile=None):
        '''
        '''
        ossfile = os.path.join(self.root_name,ossfile)
        
        resp = requests.get(url)

        result = self.bucket.put_object(ossfile,resp)

        if result.status == 200:
            url = os.path.join(self.domain,ossfile)
        else:
            url = ''

        return url

    def upload_from_ajax(self, request, path_name, key):
        """
        上传文件
        :param request: 请求
        :param path_name: 要存放到服务器上的路径
        :param key: 从request取文件的form的name
        :return: dict
        """
        file_obj = request.FILES.get(key, None)
        if file_obj:
            file_name = file_obj.name.split(".")[0]
            file_type = file_obj.name.split(".")[-1]
            file_size = file_obj.size
            origin_file_name = path_name + str(uuid.uuid1()) + '.' + file_type
            pdf_file_name = path_name + str(uuid.uuid1()) + '.pdf'

            # 把文件写入tmp，上传到server后，删除本地
            tmp_file_name = os.path.join("/tmp", file_obj.name)
            print "tmp_file_name --> ", tmp_file_name

            with open(tmp_file_name, "wb+") as fobj:
                for chunk in file_obj.chunks():
                    fobj.write(chunk)
            file_url = MyOSS().resumable_upload_from_local(tmp_file_name,
                                                           origin_file_name)

            result = {"name": file_name, "type": file_type,
                      "size": file_size, "url": file_url}

            # 只有资源、任务才进行转换
            if key in ["resource", "task", "url"]:
                res = convert_to_pdf(tmp_file_name)
                print "convert result: -->", res

                # 转换为pdf，再次上传到server
                tmp_pdf_name = os.path.join("/tmp", file_name + ".pdf")
                print "tmp_pdf_name --> ", tmp_pdf_name

                if res:
                    pdf_file_url = MyOSS().resumable_upload_from_local(tmp_pdf_name,
                                                                       pdf_file_name)
                    if pdf_file_url:
                        result["pdf_url"] = pdf_file_url
                        os.remove(tmp_pdf_name)

            # 删除源文件
            if file_url:
                os.remove(tmp_file_name)

            print "upload_file_info: --> ", result
        else:
            result = {}

        return result

    def progress_callback(self, consumed_bytes, total_bytes):
        if total_bytes:
            rate = int(100*float(consumed_bytes)/float(total_bytes))
            if rate % 50 == 0:
                print '\r{0}%'.format(rate)


        



if __name__ == '__main__':
    myoss = MyOSS()
    #url = myoss.upload_from_str('test','test/test.txt')
    #print url

    #localfile = 'localtest.txt'
    #ossfile = 'localtooss/text.txt'
    #url = myoss.upload_from_local(localfile,ossfile)
    #print url

    #ossfile = 'test/test.jpg'
    #url = myoss.upload_from_url('http://img.ph.126.net/ocT0cPlMSiTs2BgbZ8bHFw==/631348372762626203.jpg',ossfile)
    #print url

    localfile = 'test.mp4'
    ossfile = 'localtooss/test.mp4'
    url = myoss.resumable_upload_from_local(localfile,ossfile)
    print url
