def new_confirm(path: str, name: str, id: str, data: http.RequestData, mongo: Database, auth: Auth): query = {"path": path, "name": name} target = mongo.files.find_one(query) if target is not None: return {"status": 0, "msg": "Target already exists", "code": 500} query['link'] = id query['type'] = 'file' query['owner'] = auth.get_display_name() mongo.files.insert_one(query) return {"status": 1, "code": 200}
def rm_file(path: str, name: str, mongo: Database, auth: Auth): target = mongo.files.find_one({"path": path, "name": name}) if not target: return {"status": 0, "msg": "Target doesn't exist", "code": 500} elif target['type'] == "folder": return {"status": 0, "msg": "Target is a file", "code": 500} if target['owner'] != auth.get_display_name() and not auth.user.is_admin: return {"status": 0, "msg": "You can't delete other people's file."} else: if "link" in target: mongo.strategy.remove({"fileId": target['link']}) mongo.files.remove({"_id": target["_id"]}) return {"status": 1, "code": 200}
def new_file_api(path: str, name: str, data: http.RequestData, mongo: Database, auth: Auth): query = {"path": path, "name": name} target = mongo.files.find_one(query) if target is not None: return {"status": 0, "msg": "Target already exists", "code": 500} file_id = uuid4().hex data['file_id'] = file_id mongo.strategy.insert_one(data) query['link'] = file_id query['type'] = 'file' query['owner'] = auth.get_display_name() mongo.files.insert_one(query) return {"status": 1, "code": 200}
def rm_dir(path: str, name: str, mongo: Database, auth: Auth): target = mongo.files.find_one({"path": path, "name": name}) if not target: return {"status": 0, "msg": "Target doesn't exist", "code": 500} elif target['type'] != "folder": return {"status": 0, "msg": "Target is not a folder", "code": 500} if target['owner'] != auth.get_display_name() and not auth.user.is_admin: return {"status": 0, "msg": "You can't delete other people's file."} # Recursive remove new_path = f"{path}/{name}" for content in ls(new_path, mongo): if content['type'] == 'folder': rm_dir(content['path'], content['name']) else: rm_file(content['path'], content['name']) mongo.files.remove({"_id": target["_id"]}) return {"status": 1, "code": 200}
def current_user(auth: Auth, mongo: Database): user = mongo.users.find_one({'username': auth.get_display_name()}, {'_id': 0, 'password': 0}) return {'code': 200, 'data': user}
def has_permission(self, auth: Auth): return auth.is_authenticated() and auth.user.is_admin
def has_permission(self, auth: Auth): return auth.is_authenticated()