Ejemplo n.º 1
0
 def modify_size(self, size):
     """
     修改写入数据的大小
     :param size:
     :return:
     """
     user_database = database.get(self.login)
     if user_database:
         total_size = user_database.get("qutota")
         used_size = user_database.get("used")
         avail_size = user_database.get("avail")
         if not total_size:
             total_size = QUTOTA_SIZE
         if not used_size:
             used_size = 0
         if not avail_size:
             avail_size = 0
         if (size + int(used_size)) < int(total_size):
             used_size = int(used_size) + size
             avail_size = int(total_size) - int(used_size)
             user_database["qutota"] = total_size
             user_database["used"] = used_size
             user_database["avail"] = avail_size
             database[self.login] = user_database
             connect_database["data"] = database
             return database
Ejemplo n.º 2
0
 def modify_size(self, size):
     """
     修改写入数据的大小
     :param size:
     :return:
     """
     user_database = database.get(self.login)
     if user_database:
         total_size = user_database.get("qutota")
         used_size = user_database.get("used")
         avail_size = user_database.get("avail")
         if not total_size:
             total_size = QUTOTA_SIZE
         if not used_size:
             used_size = 0
         if not avail_size:
             avail_size = 0
         if (size + int(used_size)) < int(total_size):
             used_size = int(used_size) + size
             avail_size = int(total_size) - int(used_size)
             user_database["qutota"] = total_size
             user_database["used"] = used_size
             user_database["avail"] = avail_size
             database[self.login] = user_database
             connect_database["data"] = database
             return database
Ejemplo n.º 3
0
 def info(self):
     user_database = database.get(self.login)
     if user_database:
         total_size = user_database.get("qutota")
         used_size = user_database.get("used")
         avail_size = user_database.get("avail")
         msg = """用户名     [{}]
         总大小     [{}]
         已使用     [{}]
         可用空间    [{}]
         """.format(self.login, total_size, used_size, avail_size)
         self.send_all("200", {"data": msg})
         return True
Ejemplo n.º 4
0
 def info(self):
     user_database = database.get(self.login)
     if user_database:
         total_size = user_database.get("qutota")
         used_size = user_database.get("used")
         avail_size = user_database.get("avail")
         msg = """用户名     [{}]
         总大小     [{}]
         已使用     [{}]
         可用空间    [{}]
         """.format(self.login, total_size, used_size, avail_size)
         self.send_all("200", {"data": msg})
         return True
Ejemplo n.º 5
0
 def get_size(self, size):
     """
     匿名用户不计算大小,返回为真,普通用户进配额检测
     :param size: 准备写入的数据大小
     :return: 如果空间可用,返回真,匿名用户不参与计算配额,
     """
     if not size:
         return True
     user_database = database.get(self.login)
     if user_database:
         total_size = user_database.get("qutota")
         used_size = user_database.get("used")
         if not total_size:
             total_size = QUTOTA_SIZE
         if not used_size:
             used_size = 0
         if (size + int(used_size)) < int(total_size):
             return True
         return False
     return True
Ejemplo n.º 6
0
 def get_size(self, size):
     """
     匿名用户不计算大小,返回为真,普通用户进配额检测
     :param size: 准备写入的数据大小
     :return: 如果空间可用,返回真,匿名用户不参与计算配额,
     """
     if not size:
         return True
     user_database = database.get(self.login)
     if user_database:
         total_size = user_database.get("qutota")
         used_size = user_database.get("used")
         if not total_size:
             total_size = QUTOTA_SIZE
         if not used_size:
             used_size = 0
         if (size + int(used_size)) < int(total_size):
             return True
         return False
     return True
Ejemplo n.º 7
0
    def put(self):
        self.logger.debug("——————————————————进入put操作模式————————————————————")
        get_args = self.parse_cmd()
        if get_args:
            self.logger.debug("指令解析成功[{}]".format(get_args))
            to_client_location, in_server_file = get_args
        else:
            self.logger.debug("指令解析失败,返回False")
            return False
        in_server_file = self.is_owner(in_server_file)
        if not in_server_file:
            self.logger.info("权限不足,准备向客户端通告[{}]".format(in_server_file))
            self.send_all("452", {"data": self.ERROR_CODE.get(452)})
            self.logger.debug("权限不足,通告完毕,返回False")
            return False
        if os.path.isdir(in_server_file):
            in_server_file = os.path.join(in_server_file, to_client_location)
        seek = 0
        file_info = self.str_to_json(
            self.bytes_to_str(self.request_message).split("|")[1])
        last_seek = self.check_file_path(in_server_file)
        if last_seek:
            self.logger.debug("检查到上一次seek大小[{}]".format(last_seek))
            seek = last_seek
        file_type = file_info.get("type")
        if str(file_type) == "dir":
            if not os.path.exists(in_server_file):
                os.makedirs(in_server_file)
                if self.get_size(os.stat(in_server_file).st_size):
                    self.logger.info("目录[{}]创建成功".format(in_server_file))
                    self.modify_size(os.stat(in_server_file).st_size)
                    self.send_all("453", {"data": self.ERROR_CODE.get(453)})
                    return True
                else:
                    os.removedirs(in_server_file)
                    self.send_all("455", {"data": self.ERROR_CODE.get(455)})
            return True
        self.logger.debug("准备告知客户端已准备接收文件,并告知客户端接收seek位置")
        avail_size = database.get(self.login)
        if avail_size:
            avail_size = database[self.login]["avail"]
        else:
            avail_size = QUTOTA_SIZE
        self.send_all("208", {
            "data": self.ERROR_CODE.get(208),
            "seek": seek,
            "avail": avail_size
        })
        self.logger.debug(" 通告消息发送完毕")
        received_size = 0
        total_size = file_info.get("file_size")
        if not self.get_size(total_size):
            self.logger.warning("该用户[{}]空间不够".format(self.login))
            self.send_all("455", {"data": self.ERROR_CODE.get(455)})
            return False
        if not total_size or total_size <= seek:
            self.logger.info("上传数据小于本地已存在的文件大小")
            return False
        percent = 0  # 初始百分比
        total_size -= seek
        while received_size != total_size:
            self.logger.debug("==========接收文件中 ...============")
            data = self.recv_all()
            try:
                self.request_message = str(self.request_message,
                                           ENCODING).split("|")
            except TypeError:
                break
            self.logger.debug("收到[{}]".format(self.request_message))
            respond_json_data = self.str_to_json(self.request_message[1])
            if respond_json_data.get("md5") and self.encode_data(
                    data) != respond_json_data.get("md5"):
                self.logger.debug("数据接收不完整 !!!继续接收完,但不写入")
                received_size += len(data)
                continue

            seek = respond_json_data.get("seek")
            self.writefile(in_server_file, data, total_size, seek, "a+b")
            if total_size % RECV_BUFFER == 0:
                times = int(total_size / RECV_BUFFER)  # 刚好传完的次数
            else:
                times = int(total_size / RECV_BUFFER) + 1  # 否则,次数再加一次
            count = 100 / times  # 计算每次增加的百分比
            percent += float(count)
            hashes = "#" * int(percent / 100.0 * 65)
            spaces = " " * (65 - len(hashes))
            percent_format = "{:.2f}".format(percent)
            space = " " * (6 - len(percent_format))
            sys.stdout.write("\r已完成:[{}%] [{}] ".format(
                percent_format + space, hashes + spaces))
            sys.stdout.flush()
            received_size += len(data)
        print()
        self.logger.debug("——————————————put请求完毕————————————————————————")
Ejemplo n.º 8
0
    def put(self):
        self.logger.debug("——————————————————进入put操作模式————————————————————")
        get_args = self.parse_cmd()
        if get_args:
            self.logger.debug("指令解析成功[{}]".format(get_args))
            to_client_location, in_server_file = get_args
        else:
            self.logger.debug("指令解析失败,返回False")
            return False
        in_server_file = self.is_owner(in_server_file)
        if not in_server_file:
            self.logger.info("权限不足,准备向客户端通告[{}]".format(in_server_file))
            self.send_all("452", {"data": self.ERROR_CODE.get(452)})
            self.logger.debug("权限不足,通告完毕,返回False")
            return False
        if os.path.isdir(in_server_file):
            in_server_file = os.path.join(in_server_file, to_client_location)
        seek = 0
        file_info = self.str_to_json(self.bytes_to_str(self.request_message).split("|")[1])
        last_seek = self.check_file_path(in_server_file)
        if last_seek:
            self.logger.debug("检查到上一次seek大小[{}]".format(last_seek))
            seek = last_seek
        file_type = file_info.get("type")
        if str(file_type) == "dir":
            if not os.path.exists(in_server_file):
                os.makedirs(in_server_file)
                if self.get_size(os.stat(in_server_file).st_size):
                    self.logger.info("目录[{}]创建成功".format(in_server_file))
                    self.modify_size(os.stat(in_server_file).st_size)
                    self.send_all("453", {"data": self.ERROR_CODE.get(453)})
                    return True
                else:
                    os.removedirs(in_server_file)
                    self.send_all("455", {"data": self.ERROR_CODE.get(455)})
            return True
        self.logger.debug("准备告知客户端已准备接收文件,并告知客户端接收seek位置")
        avail_size = database.get(self.login)
        if avail_size:
            avail_size = database[self.login]["avail"]
        else:
            avail_size = QUTOTA_SIZE
        self.send_all("208", {"data": self.ERROR_CODE.get(208), "seek": seek, "avail": avail_size})
        self.logger.debug(" 通告消息发送完毕")
        received_size = 0
        total_size = file_info.get("file_size")
        if not self.get_size(total_size):
            self.logger.warning("该用户[{}]空间不够".format(self.login))
            self.send_all("455", {"data": self.ERROR_CODE.get(455)})
            return False
        if not total_size or total_size <= seek:
            self.logger.info("上传数据小于本地已存在的文件大小")
            return False
        percent = 0  # 初始百分比
        total_size -= seek
        while received_size != total_size:
            self.logger.debug("==========接收文件中 ...============")
            data = self.recv_all()
            try:
                self.request_message = str(self.request_message, ENCODING).split("|")
            except TypeError:
                break
            self.logger.debug("收到[{}]".format(self.request_message))
            respond_json_data = self.str_to_json(self.request_message[1])
            if respond_json_data.get("md5") and self.encode_data(data) != respond_json_data.get("md5"):
                self.logger.debug("数据接收不完整 !!!继续接收完,但不写入")
                received_size += len(data)
                continue

            seek = respond_json_data.get("seek")
            self.writefile(in_server_file, data, total_size, seek, "a+b")
            if total_size % RECV_BUFFER == 0:
                times = int(total_size / RECV_BUFFER)  # 刚好传完的次数
            else:
                times = int(total_size / RECV_BUFFER) + 1  # 否则,次数再加一次
            count = 100 / times  # 计算每次增加的百分比
            percent += float(count)
            hashes = "#" * int(percent / 100.0 * 65)
            spaces = " " * (65 - len(hashes))
            percent_format = "{:.2f}".format(percent)
            space = " " * (6 - len(percent_format))
            sys.stdout.write("\r已完成:[{}%] [{}] ".format(percent_format + space, hashes + spaces))
            sys.stdout.flush()
            received_size += len(data)
        print()
        self.logger.debug("——————————————put请求完毕————————————————————————")