Exemple #1
0
def sync_download_file_by_web_request(record_id, bucket=None):
    # return response or abort error
    bucket = bucket or get_logined_bucket_by_token()  # by api token
    if not bucket:
        error_info = 'no bucket matched'
    else:
        error_info = ""
    record = get_record(bucket=bucket,
                        zero_ids_allowed=False,
                        record_id=record_id,
                        force_dict=True)
    if not record:
        error_info = "no record found"
    if record.get("is_dir"):
        error_info = "is dir, can not download"
    elif record.get("is_deleted"):
        error_info = "is deleted, can not download"
    if not error_info:
        file_version = record.get("version")
        version_from_client = request.values.get("version")
        if file_version == version_from_client:
            # 内容跟客户端一致,不需要下载回去
            return Response("")
        response = storage.get_download_response_for_record(
            bucket, record) or Response("")
        return response
    else:
        abort(404, error_info)
Exemple #2
0
def sync_file_by_web_request():
    relative_path = (request.values.get('path')
                     or request.values.get('relative_path') or '').strip()
    relative_path = relative_path.lstrip('/')
    real_relative_path = request.values.get("real_path",
                                            "").strip().lstrip("/")
    content = get_file_content_in_request() or request.values.get(
        'raw_content') or request.values.get('content')
    is_dir = request.values.get('is_dir') == 'true'
    is_deleted = request.values.get('is_deleted') == 'true'
    bucket = get_logined_bucket()
    should_check_login = True
    if not bucket:
        bucket = get_logined_bucket_by_token()  # by api token
        if bucket and request.values.get(
                "action") == "check":  # 仅仅是校验当前的 token 是否正确了
            return jsonify(dict(status='ok'))
        should_check_login = False
    if not relative_path:
        error_info = 'set path first'
    elif not bucket:
        error_info = 'no bucket matched'
    elif should_check_login and not is_bucket_login(bucket=bucket):
        error_info = 'bucket is not login'
    elif is_deleted and is_dir and get_paths_under(bucket=bucket,
                                                   under=relative_path):
        error_info = 'a non-empty folder is not allowed to delete on web file manager'
    elif content and len(content) > MAX_FILE_SIZE:
        error_info = "max file size is %s" % MAX_FILE_SIZE
    else:
        # 处理 .configs/sorts.json -> orders
        content_handled = False
        error_info = ""
        if relative_path in [".configs/sorts.json", "configs/sorts.json"]:
            try:
                raw_sorts_data = json.loads(content)
                if isinstance(raw_sorts_data,
                              dict) and "__positions" in raw_sorts_data:
                    sorts_data = raw_sorts_data.get("__positions")
                    if isinstance(sorts_data, dict):
                        set_bucket_configs(bucket,
                                           configs=sorts_data,
                                           config_type="orders")
                        content_handled = True
            except:
                pass
        if not content_handled:
            error_info = sync_file_by_server_side(
                bucket=bucket,
                relative_path=relative_path,
                content=content,
                is_dir=is_dir,
                is_deleted=is_deleted,
                real_relative_path=real_relative_path)
    if not error_info:
        return jsonify(dict(status='ok'))
    else:
        return json_if_error(400, dict(status='failed', message=error_info))
Exemple #3
0
def allowed_to_display_some_bucket_info(bucket=None):
    if bucket:
        set_pending_bucket_bucket_in_request(bucket)  # 校验用的
    logined_bucket = get_logined_bucket_by_token()
    if not logined_bucket:
        raw_token = request.values.get("api_token") or request.values.get(
            "token") or ""
        if raw_token:
            endpoint_password = get_env("endpoint_password")
            if endpoint_password and endpoint_password == raw_token:
                return True
    if not logined_bucket:
        logined_bucket = get_logined_bucket()  # try again, by cookie
    if logined_bucket and logined_bucket == bucket:
        return True
    return False
Exemple #4
0
def show_bucket_records_for_web_request(bucket=None,
                                        default_records_per_page=100,
                                        includes_zero_ids=True,
                                        cursor=None,
                                        per_page=None):
    # return response or abort error
    bucket = bucket or get_logined_bucket_by_token()  # by api token
    if not bucket:
        abort(404, "no bucket matched")
    set_bucket_in_request_context(bucket)
    pre_record_id = cursor or request.values.get('cursor')
    if not includes_zero_ids and not pre_record_id:  # 不包括 zero ids 相当于
        pre_record_id = zero_id_for_finder
    per_page = per_page or to_per_page(default_records_per_page,
                                       request.values.get('per_page'),
                                       max_per_page=1000)
    records = get_records_for_bucket(bucket,
                                     start_record_id=pre_record_id,
                                     limit=per_page)
    return jsonify(records)
Exemple #5
0
def show_bucket_records_for_web_request(bucket=None, default_records_per_page=100, includes_zero_ids=True,
                                        cursor=None, per_page=None):
    # return response or abort error
    # 注意: 如果传入一个有效的 bucket,那么是不会进行校验的
    bucket = bucket or get_logined_bucket_by_token()  # by api token

    if not bucket:
        # 服务器端同步相关的逻辑在这里判断
        server_sync_token = request.values.get("server_sync_token", "")
        if server_sync_token and server_sync_token == get_env("server_sync_token"):
            bucket = get_pending_bucket_bucket_in_request()

    if not bucket:
        abort(404, "no bucket matched")
    set_bucket_in_request_context(bucket)
    pre_record_id = cursor or request.values.get('cursor')
    if not includes_zero_ids and not pre_record_id: # 不包括 zero ids 相当于
        pre_record_id = zero_id_for_finder
    per_page = per_page or to_per_page(default_records_per_page, request.values.get('per_page'), max_per_page=1000)
    records = get_records_for_bucket(bucket, start_record_id=pre_record_id, limit=per_page)
    return jsonify(records)
Exemple #6
0
def check_should_sync_files_by_web_request():
    bucket = get_logined_bucket() or get_logined_bucket_by_token()
    if not bucket:
        return jsonify([])
    raw_json_data = request.values.get("json")
    if raw_json_data:
        try:
            json_data = json.loads(raw_json_data)
        except:
            json_data = {}
    else:
        json_data = request.json or {}
    to_return = []  # 需要上传的 paths 集合
    if isinstance(json_data, dict):
        tried_times = 0
        for path, version in json_data.items():
            tried_times += 1
            if tried_times >= 1000: break
            if not isinstance(path, string_types) or not isinstance(
                    version, string_types):
                continue
            if path in to_return: continue
            matched_record = get_record_by_path(bucket, path, force_dict=True)
            if not matched_record:
                to_return.append(path)
            else:
                raw_content = matched_record.get(
                    'raw_content') or matched_record.get('content') or ''
                if not raw_content and storage.should_upload_file_by_client(
                        bucket, matched_record):
                    # 服务器上还没有,需要上传的
                    to_return.append(path)
                    continue
                matched_version = matched_record.get("version")
                if matched_version and matched_version == version:
                    continue
                else:
                    to_return.append(path)
    return jsonify(to_return)