def update_bucket_config(self): config_type = self.action.replace('config_', '').strip() configs = self.raw_json_data updated = set_bucket_configs(self.bucket, configs, config_type=config_type) if config_type in ['files', 'file']: # 不由 client 端设定 files 的信息 return json_with_status_code(200, 'ok') if WEBSOCKET and config_type == 'pages' and configs.get( '__changed_filepaths'): # 推送 websocket 的通知, 如果启用了 websocket 的话,这样可以实时刷新 template changed_filepaths = configs.get('__changed_filepaths') message_to_push = dict(changed_filepaths=changed_filepaths, date=time.time()) push_message_to_bucket(bucket=self.bucket, message=message_to_push) if not updated: return json_with_status_code( 400, 'configs format error or no bucket matched') else: # 先移除 ipfs 相关的逻辑 @2021-2-4 # from farbox_bucket.ipfs.server.ipfs_bucket import mark_bucket_to_sync_ipfs #if config_type == 'files': # todo 这里处理是否妥当?? #mark_bucket_to_sync_ipfs(self.bucket) return json_with_status_code(200, 'ok')
def unregister_domain(self): domain = self.get_value_from_data('domain') if not domain: return json_with_status_code(400, 'no domain in your request') else: error_info = unregister_bucket_domain(domain=domain, bucket=self.bucket) if error_info: return json_with_status_code(400, error_info) else: return json_with_status_code(200, 'ok')
def set_bucket_theme(self): # 从系统默认提供的 theme 中进行直接的设定 theme_key = self.raw_json_data.get('theme') or self.raw_json_data.get( 'theme_key') theme_content = themes.get(theme_key) or '' if not theme_content or not isinstance(theme_content, dict): return json_with_status_code(404, 'can not find the theme') else: if '_theme_key' not in theme_content: theme_content['_theme_key'] = theme_key set_bucket_configs(self.bucket, theme_content, config_type='pages') return json_with_status_code(200, 'ok')
def get_configs(self): config_type = self.raw_json_data.get('type') or self.raw_json_data.get( 'config_type') or 'site' if config_type == "files": configs = auto_update_bucket_and_get_files_info(self.bucket) else: configs = get_bucket_configs(self.bucket, config_type=config_type) return json_with_status_code(200, configs)
def should_upload_file(self): file_size = self.raw_json_data.get("size") or self.raw_json_data.get( "file_size") if file_size > MAX_FILE_SIZE: should = False else: should = storage.should_upload_file_by_client( self.bucket, self.raw_json_data) return json_with_status_code(200, 'yes' if should else 'no')
def check_filepaths(self): filepaths = self.raw_json_data.get( 'filepaths') or self.raw_json_data.get('paths') if not isinstance(filepaths, (list, tuple)): filepaths = [] result = {} for path in filepaths: path_record_id = get_record_id_by_path(self.bucket, path) or '' result[path] = path_record_id return json_with_status_code(200, result)
def upload_file(self): path = self.raw_json_data.get('path') if not path: return json_with_status_code(400, 'path required') accepted = False info = storage.accept_upload_file_from_client( self.bucket, self.raw_json_data, get_raw_content_func=get_file_content_in_request) if info == 'ok': accepted = True if accepted: # 没有路径对应的 record,直接进行上传的行为, 创建一个 record record_id = get_record_id_by_path(self.bucket, path) if not record_id: create_record(self.bucket, record_data=self.raw_json_data) return json_with_status_code(200, 'ok') else: return json_with_status_code(400, info)
def handle(self): if not isinstance(self.verified_message, dict): # 校验出错了 return json_with_status_code(500, message=self.verified_message) action = request.values.get('action') or '' action_handler = self.action_handlers.get(action) if not action_handler and action.startswith('config_'): action_handler = self.update_bucket_config if not action_handler: action_handler = self.create_record # call the action_handler, should return a response return action_handler()
def create_record(self): # the default action version = self.raw_json_data.get("version") path = self.raw_json_data.get("path") if path and path.endswith(".md"): pass if version and path: old_record = get_record_by_path(bucket=self.bucket, path=path) if old_record and old_record.get("version") == version: if DEBUG: print("same file for %s" % path) return json_with_status_code(200, 'ok') elif path and self.raw_json_data.get( "is_dir") and not self.raw_json_data.get("is_deleted", False): # folder 不需要重新处理 old_record = get_record_by_path(bucket=self.bucket, path=path) if old_record: return json_with_status_code(200, 'ok') error_info = create_record(self.bucket, self.raw_data) if error_info: return json_with_status_code(400, error_info) else: # record 已经创建成功了: # 1. visits & comments 这些动态的数据要到数据库里 # 2. 把 private_key_md5 进行存储,可以对一些数据进行加密、解密 json_data = self.raw_json_data if isinstance(json_data, dict): record_type = json_data.get('_type') path = json_data.get('path') if not isinstance(path, string_types): path = '' path = path.strip('/').lower() if record_type == 'visits' and path == '_data/visits.csv': load_all_posts_visits_from_csv(self.bucket, json_data) return json_with_status_code(200, 'ok')
def check_bucket(self): if self.bucket and has_bucket(self.bucket): return json_with_status_code(200, 'ok') else: return json_with_status_code(404, 'not found')
def show_files(self): files = auto_update_bucket_and_get_files_info(self.bucket) return json_with_status_code(200, files)