# Path: allchat/filestore/encrypt.py
# class Encryptor(object):
#     def __init__(self, key, IV):
#         self.key = key
#         self.IV = IV
#         self.blocksize = 16
# 
#     def EncryptStr(self,content):
#         if sys.version_info.major == 2:
#             content = content.encode("utf-8")
#         elif sys.version_info.major == 3:
#             content = bytes(content,"utf-8")
#         while len(content)%self.blocksize != 0:
#             content += b"\0"
#         obj = AES.new(self.key,AES.MODE_CBC,self.IV)
#         return obj.encrypt(content)
# 
#     def DecryptStr(self,content):
#         obj = AES.new(self.key,AES.MODE_CBC,self.IV)
#         content = obj.decrypt(content).strip(b"\0")
#         return content.decode("utf-8")
# 
# Path: allchat/path.py
# def get_single_msg_dir():
# 	return os.path.join(get_project_root(),"Data","single_messages")
# 
# def get_group_msg_dir():
# 	return os.path.join(get_project_root(),"Data","group_messages")
# 
# def get_picture_dir():
# 	return os.path.join(get_project_root(),"Data","picture")
# 
# def get_project_root():
# 	return os.path.dirname(os.path.dirname(__file__))


import os
import time
import hashlib
from multiprocessing import Process, Queue
from .encrypt import Encryptor
from allchat.path import get_single_msg_dir, get_group_msg_dir, get_picture_dir, get_project_root
            return False

    def savePicture(self, content, format_):
        if content and format_:
            md5 = hashlib.md5()
            md5.update(content)
            pic_name = md5.hexdigest()
            path = os.path.join(get_picture_dir(),pic_name+"."+format_)
            if not os.path.exists(os.path.dirname(path)):
                os.makedirs(os.path.dirname(path))
            with open(path,"wb") as output:
                output.write(content)
            return pic_name+"."+format_
        else:
            return False

def _write_message(queue, terminator, encrypt_key, IV, root_dir):
    encryptor = Encryptor(encrypt_key, IV)
    while True:
        message = queue.get()
        if message == terminator:
            break
        sender, sub_dir, msg = message
        date = time.strftime("%Y-%m-%d", time.localtime())
        year, month, day = date.split("-")
        directory = os.path.join(root_dir, sub_dir, year, month)
        if not os.path.exists(directory):
            os.makedirs(directory)
        file_name = os.path.join(directory, day+".bin")
        msg_sep = "\t\t"

# Path: allchat/filestore/encrypt.py
# class Encryptor(object):
#     def __init__(self, key, IV):
#         self.key = key
#         self.IV = IV
#         self.blocksize = 16
# 
#     def EncryptStr(self,content):
#         if sys.version_info.major == 2:
#             content = content.encode("utf-8")
#         elif sys.version_info.major == 3:
#             content = bytes(content,"utf-8")
#         while len(content)%self.blocksize != 0:
#             content += b"\0"
#         obj = AES.new(self.key,AES.MODE_CBC,self.IV)
#         return obj.encrypt(content)
# 
#     def DecryptStr(self,content):
#         obj = AES.new(self.key,AES.MODE_CBC,self.IV)
#         content = obj.decrypt(content).strip(b"\0")
#         return content.decode("utf-8")
# 
# Path: allchat/path.py
# def get_single_msg_dir():
# 	return os.path.join(get_project_root(),"Data","single_messages")
# 
# def get_group_msg_dir():
# 	return os.path.join(get_project_root(),"Data","group_messages")
# 
# def get_picture_dir():
# 	return os.path.join(get_project_root(),"Data","picture")
# 
# def get_project_root():
# 	return os.path.dirname(os.path.dirname(__file__))


# Path: allchat/filestore/storage.py
import os
import time
import hashlib
from multiprocessing import Process, Queue
from .encrypt import Encryptor
from allchat.path import get_single_msg_dir, get_group_msg_dir, get_picture_dir, get_project_root

            return False

    def savePicture(self, content, format_):
        if content and format_:
            md5 = hashlib.md5()
            md5.update(content)
            pic_name = md5.hexdigest()
            path = os.path.join(get_picture_dir(),pic_name+"."+format_)
            if not os.path.exists(os.path.dirname(path)):
                os.makedirs(os.path.dirname(path))
            with open(path,"wb") as output:
                output.write(content)
            return pic_name+"."+format_
        else:
            return False

def _write_message(queue, terminator, encrypt_key, IV, root_dir):
    encryptor = Encryptor(encrypt_key, IV)
    while True:
        message = queue.get()
        if message == terminator:
            break
        sender, sub_dir, msg = message
        date = time.strftime("%Y-%m-%d", time.localtime())
        year, month, day = date.split("-")
        directory = os.path.join(root_dir, sub_dir, year, month)
        if not os.path.exists(directory):
            os.makedirs(directory)
        file_name = os.path.join(directory, day+".bin")
        msg_sep = "\t\t"

        content = msg_sep.join((sender,msg[0],msg[1]))