# coding:utf-8
import os
import sys
import time
local_path = os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))
if local_path not in sys.path:
    sys.path.append(local_path)
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "kedong.settings")
from django.conf import settings
from pyftpdlib.authorizers import DummyAuthorizer
from pyftpdlib.handlers import FTPHandler, _strerror, FilesystemError
from pyftpdlib.servers import FTPServer

ftp_redis_pool = settings.ftp_redis_pool
CONFIG = settings.CONFIG
ftp_dict = CONFIG['ftp_config']
ftp_config = CONFIG['ftp']
user_list = [
    ("ftpuser", "yf_123456", "/home/ftpsite")
]
user_list.extend(ftp_dict.values())
FTPHandler.permit_foreign_addresses = True


class MyFTPHandler(FTPHandler):

    def _get_redis_key(self, file, prefix):
        key = None
        for k in ftp_dict.keys():
            if file.startswith(k):
                key = f"{prefix}{k}"
        return key

    def push_redis(self, key, data):
        ftp_redis_pool.push(key, data)

    def on_file_received(self, file):
        """上传结束回调"""
        if not file.endswith('.jpg'):
            return None
        key = self._get_redis_key(file, prefix="/up_complete")
        if key:
            data = {
                'path': file,
                'create_time': int(time.time())
            }
            self.push_redis(key, data)

    def on_incomplete_file_received(self, file):
        if not file.endswith('.jpg'):
            return None
        if file.startswith("/data/ftp/photo/cbd/"):
            key = self._get_redis_key(file, prefix="/up_complete")
            if key:
                data = {
                    'path': file,
                    'create_time': int(time.time())
                }
                self.push_redis(key, data)

    def ftp_MFMT(self, path, timeval):
        """真正上传完成调用"""
        result = super().ftp_MFMT(path, timeval)
        try:
            file = result[1]
            key = self._get_redis_key(file, prefix="/up_success")
            if key:
                data = {
                    'path': file,
                    'create_time': int(time.time())
                }
                self.push_redis(key, data)
        except Exception as e:
            pass
        return result

    def ftp_SIZE(self, path):
        line = self.fs.fs2ftp(path)
        if self._current_type == 'a':
            why = "SIZE not allowed in ASCII mode"
            self.respond("550 %s." % why)
            return
        real_path = self.fs.realpath(path)
        is_file = self.fs.isfile(real_path)
        if not is_file:
            why = "%s is not retrievable" % line
            self.respond("550 %s." % why)
            return
        try:
            size = self.run_as_current_user(self.fs.getsize, path)
        except (OSError, FilesystemError) as err:
            why = _strerror(err)
            self.respond('550 %s.' % why)
        else:
            self.respond("213 %s" % size)


def main():
    # 启动FTP服务
    authorizer = DummyAuthorizer()
    for i in user_list:
        path = i[2]
        if not os.path.isdir(path):
            os.makedirs(path)
        authorizer.add_user(i[0], i[1], path, perm="elradfmwMT")
    handler = MyFTPHandler
    handler.authorizer = authorizer
    handler.passive_ports = range(2000, 6333)
    server = FTPServer((ftp_config['ip'], ftp_config['port']), handler)
    server.max_cons = 2048
    server.serve_forever()


if __name__ == '__main__':
    main()