# -*- coding: utf-8 -*-

import hashlib
import json
import os
import tempfile


class UploadProgressRecorder(object):
    """持久化上传记录类

    该类默认保存每个文件的上传记录到文件系统中，用于断点续传
    上传记录为json格式：
    {
        "size": file_size,
        "offset": upload_offset,
        "modify_time": file_modify_time,
        "contexts": contexts
    }

    Attributes:
        record_folder: 保存上传记录的目录
    """

    def __init__(self, record_folder=tempfile.gettempdir()):
        self.record_folder = record_folder

    def get_upload_record(self, file_name, key):
        record_key = '{0}/{1}'.format(key, file_name)

        record_file_name = hashlib.md5(record_key.encode('utf-8')).hexdigest()
        upload_record_file_path = os.path.join(self.record_folder, record_file_name)
        if not os.path.isfile(upload_record_file_path):
            return None
        with open(upload_record_file_path, 'r') as f:
            json_data = json.load(f)
        return json_data

    def set_upload_record(self, file_name, key, data):
        record_key = '{0}/{1}'.format(key, file_name)
        record_file_name = hashlib.md5(record_key.encode('utf-8')).hexdigest()
        upload_record_file_path = os.path.join(self.record_folder, record_file_name)
        with open(upload_record_file_path, 'w') as f:
            json.dump(data, f)

    def delete_upload_record(self, file_name, key):
        record_key = '{0}/{1}'.format(key, file_name)
        record_file_name = hashlib.md5(record_key.encode('utf-8')).hexdigest()
        upload_record_file_path = os.path.join(self.record_folder, record_file_name)
        os.remove(upload_record_file_path)

@@ -71,16 +71,56 @@
 empfile%0A
+from qiniu.compat import is_py2, is_py3%0A
 %0A%0Aclass 
@@ -627,17 +627,120 @@
 e_name)%0A
-%0A
+        if is_py2:%0A            record_file_name = hashlib.md5(record_key).hexdigest()%0A        else:%0A    
         
@@ -802,32 +802,33 @@
 ')).hexdigest()%0A
+%0A
         upload_r
@@ -1194,32 +1194,136 @@
 key, file_name)%0A
+        if is_py2:%0A            record_file_name = hashlib.md5(record_key).hexdigest()%0A        else:%0A    
         record_f
@@ -1377,32 +1377,33 @@
 ')).hexdigest()%0A
+%0A
         upload_r
@@ -1655,32 +1655,136 @@
 key, file_name)%0A
+        if is_py2:%0A            record_file_name = hashlib.md5(record_key).hexdigest()%0A        else:%0A    
         record_f
@@ -1838,32 +1838,33 @@
 ')).hexdigest()%0A
+%0A
         upload_r

fix the resume uploader recorder key error under python2 when using chinese character