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))
def add_new_comment_web_view(): bucket = request.values.get("bucket") site_configs = get_bucket_site_configs(bucket) if not site_configs: comments_allowed = False else: comments_allowed = site_configs.get("comments") if not comments_allowed: abort(404, 'comment is not allowed in this site') new_comment_doc = add_comment() if new_comment_doc.get('error'): # 错误信息 return jsonify(new_comment_doc) if request.values.get('format') in ['json', 'JSON']: return jsonify(new_comment_doc) else: return render_api_template('comment.jade', comment=new_comment_doc)
def show_records(self): if not self.bucket: return jsonify([]) else: per_page = to_int(self.raw_json_data.get("per_page")) or 10 response = show_bucket_records_for_web_request( self.bucket, cursor=self.raw_json_data.get("cursor"), per_page=per_page, includes_zero_ids=False) return response
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)