| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141 |
- from typing import Dict, Any, List, Optional
- class FileService:
- def __init__(self, http_client):
- self.http_client = http_client
-
- def list_files(self, parent_id: str = None, keywords: str = None,
- page: int = 1, size: int = 20, orderby: str = "create_time",
- desc: bool = True) -> List[Dict[str, Any]]:
- endpoint = "/api/v1/file/list"
-
- params = {"page": page, "page_size": size, "orderby": orderby, "desc": int(desc)}
- if parent_id is not None:
- params["parent_id"] = parent_id
- if keywords is not None:
- params["keywords"] = keywords
-
- response = self.http_client.get(endpoint, params=params)
-
- if response.get("code") == 0 and response.get("data"):
- return response["data"]
- else:
- raise Exception(f"列出文件失败: {response.get('message', '未知错误')}")
-
- def get_root_folder(self) -> Dict[str, Any]:
- endpoint = "/api/v1/file/root_folder"
-
- response = self.http_client.get(endpoint)
-
- if response.get("code") == 0 and response.get("data"):
- return response["data"]
- else:
- raise Exception(f"获取根目录失败: {response.get('message', '未知错误')}")
-
- def get_parent_folder(self, file_id: str) -> Dict[str, Any]:
- endpoint = "/api/v1/file/parent_folder"
-
- response = self.http_client.get(endpoint, params={"file_id": file_id})
-
- if response.get("code") == 0 and response.get("data"):
- return response["data"]
- else:
- raise Exception(f"获取父目录失败: {response.get('message', '未知错误')}")
-
- def get_all_parent_folders(self, file_id: str) -> List[Dict[str, Any]]:
- endpoint = "/api/v1/file/all_parent_folder"
-
- response = self.http_client.get(endpoint, params={"file_id": file_id})
-
- if response.get("code") == 0 and response.get("data"):
- return response["data"]
- else:
- raise Exception(f"获取所有父目录失败: {response.get('message', '未知错误')}")
-
- def get_file(self, file_id: str) -> Dict[str, Any]:
- endpoint = f"/api/v1/file/get/{file_id}"
-
- response = self.http_client.get(endpoint)
-
- if response.get("code") == 0 and response.get("data"):
- return response["data"]
- else:
- raise Exception(f"获取文件失败: {response.get('message', '未知错误')}")
-
- def upload_file(self, file_path: str) -> Dict[str, Any]:
- endpoint = "/api/v1/file/upload"
-
- with open(file_path, 'rb') as f:
- files = {'file': (file_path.split('/')[-1], f)}
- headers = {'Content-Type': 'multipart/form-data'}
-
- response = self.http_client.post(endpoint, files=files, headers=headers)
-
- if response.get("code") == 0:
- return response.get("data", {})
- else:
- raise Exception(f"上传文件失败: {response.get('message', '未知错误')}")
-
- def create_file(self, file_id: str, tenant_id: str = None) -> Dict[str, Any]:
- endpoint = "/api/v1/file/create"
-
- data = {"file_id": file_id}
- if tenant_id is not None:
- data["tenant_id"] = tenant_id
-
- response = self.http_client.post(endpoint, json_data=data)
-
- if response.get("code") == 0:
- return response.get("data", {})
- else:
- raise Exception(f"创建文件失败: {response.get('message', '未知错误')}")
-
- def delete_file(self, file_id: str) -> bool:
- endpoint = "/api/v1/file/rm"
-
- response = self.http_client.post(endpoint, json_data={"file_id": file_id})
-
- if response.get("code") == 0:
- return True
- else:
- raise Exception(f"删除文件失败: {response.get('message', '未知错误')}")
-
- def rename_file(self, file_id: str, new_name: str) -> Dict[str, Any]:
- endpoint = "/api/v1/file/rename"
-
- data = {
- "file_id": file_id,
- "new_name": new_name
- }
-
- response = self.http_client.post(endpoint, json=data)
-
- if response.get("code") == 0:
- return response.get("data", {})
- else:
- raise Exception(f"重命名文件失败: {response.get('message', '未知错误')}")
-
- def move_file(self, file_id: str, parent_id: str) -> Dict[str, Any]:
- endpoint = "/api/v1/file/mv"
-
- data = {
- "file_id": file_id,
- "parent_id": parent_id
- }
-
- response = self.http_client.post(endpoint, json=data)
-
- if response.get("code") == 0:
- return response.get("data", {})
- else:
- raise Exception(f"移动文件失败: {response.get('message', '未知错误')}")
-
- def convert_file(self, file_id: str) -> Dict[str, Any]:
- endpoint = "/api/v1/file/convert"
-
- response = self.http_client.post(endpoint, json={"file_id": file_id})
-
- if response.get("code") == 0:
- return response.get("data", {})
- else:
- raise Exception(f"转换文件失败: {response.get('message', '未知错误')}")
|