예제 #1
0
def upload_file(path, filepath):
    drive = AliyunDrive(DATA['DRIVE_ID'], DATA['ROOT_PATH'],
                        DATA['CHUNK_SIZE'])
    # 刷新token
    drive.token_refresh()
    realpath = path + filepath
    drive.load_file(filepath, realpath)
    # 创建目录
    LOCK.acquire()
    try:
        parent_folder_id = drive.get_parent_folder_id(filepath)
    finally:
        LOCK.release()
    # 断点续传
    if DATA['RESUME'] and drive.filepath_hash in DATA['tasks']:
        c_task = DATA['tasks'][drive.filepath_hash]
        if 0 not in (
                c_task['drive_id'],
                c_task['file_id'],
                c_task['upload_id'],
                c_task['part_number'],
                c_task['chunk_size'],
        ):
            drive.drive_id = c_task['drive_id']
            drive.file_id = c_task['file_id']
            drive.upload_id = c_task['upload_id']
            drive.part_number = c_task['part_number']
            drive.chunk_size = c_task['chunk_size']
            # 获取上传地址
            drive.part_upload_url_list = drive.get_upload_url()
            # 上传
            drive.upload()
            # 提交
            if drive.complete():
                return drive.filepath_hash
            return False

    # 创建上传
    create_post_json = drive.create(parent_folder_id)
    if 'rapid_upload' in create_post_json and create_post_json['rapid_upload']:
        print_success('【{filename}】秒传成功!消耗{s}秒'.format(filename=drive.filename,
                                                       s=time.time() -
                                                       drive.start_time))
        return drive.filepath_hash
    # 上传
    drive.upload()
    # 提交
    if drive.complete():
        return drive.filepath_hash
    return False
예제 #2
0
    def upload_file(self, task):
        save_task(task['id'], {'status': 2})
        drive = AliyunDrive(DATA['config']['DRIVE_ID'],
                            DATA['config']['ROOT_PATH'],
                            DATA['config']['CHUNK_SIZE'])
        # 加载任务队列
        drive.load_task(task)
        # 刷新token
        if not os.path.exists(task['realpath']):
            drive.status = -1
            return drive
        drive.load_file(task['filepath'], task['realpath'])
        # 创建目录
        LOCK.acquire()
        try:
            parent_folder_id = drive.get_parent_folder_id(drive.filepath)
        finally:
            LOCK.release()
        # 断点续传
        if DATA['config']['RESUME'] and DATA['config']['DRIVE_ID'] == task[
                'drive_id']:
            if 0 not in [
                    drive.drive_id,
                    drive.part_number,
                    drive.chunk_size,
            ] and not drive.file_id and not drive.upload_id:
                # 获取上传地址
                drive.part_upload_url_list = drive.get_upload_url()
                # 上传
                return self.__upload(drive)

        # 创建上传
        create_post_json = drive.create(parent_folder_id)
        if 'rapid_upload' in create_post_json and create_post_json[
                'rapid_upload']:
            drive.finish_time = get_timestamp()
            drive.spend_time = drive.finish_time - drive.start_time

            self.print(
                '【{filename}】秒传成功!消耗{s}秒'.format(filename=drive.filename,
                                                 s=drive.spend_time),
                'success', drive.id)
            drive.status = 1
            return drive
        # 上传
        return self.__upload(drive)