Example #1
0
def create_record_for_a_folder(bucket="", folder_path=""):
    if not is_valid_bucket_name(bucket):
        return
    folder_path = folder_path.strip("/")
    if not folder_path:
        return
    if has_record_by_path(bucket, folder_path):
        # 再校验一次 folder 是否存在
        return
    folder_record_data = dict(
        path = folder_path,
        relative_path = folder_path,
        is_dir = True,
        slash_number = folder_path.count("/"),
        type = "folder",
        _type = "folder",
        _order = int(time.time()),
        title = os.path.split(folder_path)[-1],

    )
    object_id = str(ObjectId())
    folder_record_data["_id"] = object_id
    hset(bucket, object_id, folder_record_data, ignore_if_exists=False)

    after_path_related_record_created(bucket, object_id, folder_record_data)
def extend_bucket_expired_date_yearly_by_alipay(bucket, try_price2=False):
    title = "FarBox"
    if bucket:
        title = "FarBox-%s" % bucket
    price = BUCKET_PRICE
    if try_price2:
        price = BUCKET_PRICE2
    if price <= 0:
        price = 128
    if request.method == 'POST':
        # # notify 过来的,要告诉 alipay 已经成功了
        cache_client = get_cache_client()
        cache_client.set('alipay_notify',
                         json_dumps(alipay.payment_doc),
                         zipped=True,
                         expiration=24 * 60 * 60)
        set_response_in_request(alipay.success_response)
    current_url = request.url.split('?')[0]  # 不处理 GET 参数
    if bucket and not is_valid_bucket_name(bucket):
        return 'not a valid bucket name'
    payment_doc = alipay.payment_doc
    if not payment_doc:
        alipay_url_to_redirect = alipay.pay(
            price=price,
            title=title,
            content=bucket,
            notify_url=current_url,
            callback_url=current_url,
        )
        force_redirect(alipay_url_to_redirect)
    else:
        # bucket 增加了一年的有效期
        bucket = payment_doc.get('body') or bucket
        bucket_service_info = get_bucket_service_info(bucket)
        order_id = payment_doc.get("order_id")
        price = payment_doc.get("total_fee")
        to_handle = True
        order_id_for_db = None
        if order_id:
            order_ids_for_db = bucket_service_info.get("order_id_list")
            order_id_for_db = "%s-%s" % (order_id, price)
            if isinstance(
                    order_ids_for_db,
                (list, tuple)) and order_id_for_db in order_ids_for_db:
                # 已经处理过了
                to_handle = False
        if to_handle:
            change_bucket_expired_date(
                bucket,
                days=367,
                order_id=order_id_for_db,
            )

        if request.method == "POST":
            return force_response(alipay.success_response)

        return to_handle
Example #3
0
def show_namespaces():  # also bucket names
    # just bucket names, cursor 是上一个 bucket name
    per_page = to_per_page(1000,
                           request.values.get('per_page'),
                           max_per_page=10000)
    cursor = request.values.get('cursor') or ''
    bucket_names = hlist(name_start=cursor, limit=per_page)
    bucket_names = [
        name for name in bucket_names if is_valid_bucket_name(name)
    ]

    return jsonify(bucket_names)
Example #4
0
def set_bucket_api_token(bucket, db_name="_bucket_api_token"):
    """
    :param bucket: 需要
    :param db_name: 提供了默认值
    :return: new_api_token
    """
    if not is_valid_bucket_name(bucket):
        return ""
    if not has_bucket(bucket):
        return ""
    new_token = get_a_random_api_token()
    hset(db_name, bucket, new_token)
    return new_token
Example #5
0
def set_bucket_service_info(bucket, order_id=None, **kwargs):
    if not is_valid_bucket_name(bucket):
        return
    info = get_bucket_service_info(bucket)
    if not info.get("bucket"):
        info["bucket"] = bucket
    if order_id:
        order_id_list = info.get("order_id_list")
        if not isinstance(order_id_list, (list, tuple)):
            order_id_list = []
        if isinstance(order_id_list, tuple):
            order_id_list = list(order_id_list)
        if order_id not in order_id_list:
            order_id_list.append(order_id)
        info["order_id_list"] = order_id_list
    info.update(kwargs)
    hset("_bucket_info", bucket, info)
Example #6
0
def get_verified_message_from_web_request(raw_data=None):
    # return dict, if done
    # else return a response for webview
    raw_data = raw_data or request.values
    bucket = raw_data.get('bucket')
    timestamp = raw_data.get('timestamp')
    signature = raw_data.get('signature')  # sign all data in request
    action = raw_data.get('action')  # record & config
    data = raw_data.get('data')  # if action==create, data is None
    try:
        data = ungzip_content(data, base64=True)
    except:
        pass
    try:
        timestamp = int(timestamp)
    except:
        return 'timestamp is error'

    if not bucket or not timestamp or not signature:
        return 'fields not filled, bucket is %s, timestamp is %s, signature is %s' % (
            bucket, timestamp, signature)
    if not data and not allowed_empty_data_for_action(action):
        # show_xxx, config_xxx, create_bucket, the data can be {}, []
        return 'fields not filled, action is %s, has data:%s' % (action,
                                                                 bool(data))

    current_timestamp = time.time()
    if abs(current_timestamp - timestamp) > 60 * 60:
        return 'compared to now, the offset is more than one hour'

    max_size = MAX_RECORD_SIZE
    if action.startswith('config_'):
        max_size = MAX_RECORD_SIZE_FOR_CONFIG  # `设置`类型的限制

    if data:
        data_size = len(data)
        if data_size > max_size:
            return 'data should be less than %sk per record, current is %sk' % (
                max_size / 1024., data_size / 1024.)

    public_key = get_public_key_from_bucket(bucket)
    if not public_key:
        return 'bucket is not on current node now or no public_key in bucket'

    if not public_key:
        return 'bucket lost public key, error!'

    # verify the message
    content_to_verify = dict(
        bucket=bucket,
        timestamp=timestamp,
    )
    if data:
        content_to_verify['data'] = data
    if action:
        content_to_verify['action'] = action

    public_key_in_request = request.values.get("public_key")
    if public_key_in_request:
        content_to_verify['public_key'] = public_key_in_request

    verified = verify_by_public_key(public_key, signature, content_to_verify)
    if not verified:
        return 'verify failed'

    if not action:
        action = 'record'  # default action

    message = dict(
        bucket=bucket,
        action=action,
        data=data,  # string
        public_key=public_key,
    )

    # patch by helper?
    helper_bucket = cache_client.get("system_assistant_bucket", zipped=False)
    if helper_bucket and bucket == helper_bucket:
        helper_bucket_for_user = cache_client.get(
            "system_assistant_bucket_for_user", zipped=False)
        if helper_bucket_for_user and is_valid_bucket_name(
                helper_bucket_for_user):
            user_public_key = get_public_key_from_bucket(
                helper_bucket_for_user)
            if user_public_key:
                message["bucket"] = bucket
                message["public_key"] = user_public_key

    return message
Example #7
0
def get_bucket_service_info(bucket):
    if not bucket:
        return {}
    if not is_valid_bucket_name(bucket):
        return {}
    return hget("_bucket_info", bucket, force_dict=True)