# 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

    def saveSingleMsg(self, sender, receiver, msg):
        if sender and receiver and isinstance(msg, list) and msg[0] and msg[1]:
            users = "&&".join(sorted([sender,receiver]))
            self._single_message_queue.put((sender,users,msg))
            if not self._single_message_proc:
                self._single_message_proc = Process(target=_write_message, 
                                            args=(
                                                self._single_message_queue,
                                                self._terminator,
                                                self._encrypt_key,
                                                self._IV,
                                                get_single_msg_dir(),
                                                )  
                                        )
                self._single_message_proc.start()
            return True
        else:
            return False

    def saveGroupMsg(self, sender, group_id, msg):
        if sender and group_id and isinstance(msg, list) and msg[0] and msg[1]:
            self._group_message_queue.put((sender,str(group_id),msg))
            if not self._group_message_proc:
                self._group_message_proc = Process(target=_write_message, 
                                        args=(
                                            self._group_message_queue,
                                            self._terminator,
                                            self._encrypt_key,
                                            self._IV,

# 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


    def saveSingleMsg(self, sender, receiver, msg):
        if sender and receiver and isinstance(msg, list) and msg[0] and msg[1]:
            users = "&&".join(sorted([sender,receiver]))
            self._single_message_queue.put((sender,users,msg))
            if not self._single_message_proc:
                self._single_message_proc = Process(target=_write_message, 
                                            args=(
                                                self._single_message_queue,
                                                self._terminator,
                                                self._encrypt_key,
                                                self._IV,
                                                get_single_msg_dir(),
                                                )  
                                        )
                self._single_message_proc.start()
            return True
        else:
            return False

    def saveGroupMsg(self, sender, group_id, msg):
        if sender and group_id and isinstance(msg, list) and msg[0] and msg[1]:
            self._group_message_queue.put((sender,str(group_id),msg))
            if not self._group_message_proc:
                self._group_message_proc = Process(target=_write_message, 
                                        args=(
                                            self._group_message_queue,
                                            self._terminator,
                                            self._encrypt_key,
                                            self._IV,

                                            get_group_msg_dir(),