# 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_project_root():
# 	return os.path.dirname(os.path.dirname(__file__))


import os
import json
from .encrypt import Encryptor
from allchat.path import get_project_root
#!/usr/bin/env python
#coding:utf-8
"""
  Author:   --<alen-alex>
  Purpose: provide a message content retrieval interface to web server,
           retrieve from /data directory
  Created: 2014/11/4
"""


#----------------------------------------------------------------------
def getMessages(directory, date):
    """retrieve the messages stored in files
       param: directory: the place where files should be searched from;
       param: date: the date of files to retrieve, format by "yyyy-mm-dd";
    """
    try:
        query_year, query_month, query_day = date.split("-")
    except ValueError:
        return ("Wrong date format", 404)
    file_ = os.path.join(directory,query_year,query_month,query_day+".bin")
    if not os.path.exists(file_):
        return ("File does not exist", 404)
    messages = _read_file(file_)
    return json.JSONEncoder().encode(messages)

#----------------------------------------------------------------------
def _read_file(file_path):
    """"""
    key,IV = _get_encrypt_key()

# 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_project_root():
# 	return os.path.dirname(os.path.dirname(__file__))


# Path: allchat/filestore/retrieve.py
import os
import json
from .encrypt import Encryptor
from allchat.path import get_project_root

#!/usr/bin/env python
#coding:utf-8
"""
  Author:   --<alen-alex>
  Purpose: provide a message content retrieval interface to web server,
           retrieve from /data directory
  Created: 2014/11/4
"""


#----------------------------------------------------------------------
def getMessages(directory, date):
    """retrieve the messages stored in files
       param: directory: the place where files should be searched from;
       param: date: the date of files to retrieve, format by "yyyy-mm-dd";
    """
    try:
        query_year, query_month, query_day = date.split("-")
    except ValueError:
        return ("Wrong date format", 404)
    file_ = os.path.join(directory,query_year,query_month,query_day+".bin")
    if not os.path.exists(file_):
        return ("File does not exist", 404)
    messages = _read_file(file_)
    return json.JSONEncoder().encode(messages)

#----------------------------------------------------------------------
def _read_file(file_path):
    """"""
    key,IV = _get_encrypt_key()

    encryptor = Encryptor(key,IV)