file_service.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. from typing import Dict, Any, List, Optional
  2. class FileService:
  3. def __init__(self, http_client):
  4. self.http_client = http_client
  5. def list_files(self, parent_id: str = None, keywords: str = None,
  6. page: int = 1, size: int = 20, orderby: str = "create_time",
  7. desc: bool = True) -> List[Dict[str, Any]]:
  8. endpoint = "/api/v1/file/list"
  9. params = {"page": page, "page_size": size, "orderby": orderby, "desc": int(desc)}
  10. if parent_id is not None:
  11. params["parent_id"] = parent_id
  12. if keywords is not None:
  13. params["keywords"] = keywords
  14. response = self.http_client.get(endpoint, params=params)
  15. if response.get("code") == 0 and response.get("data"):
  16. return response["data"]
  17. else:
  18. raise Exception(f"列出文件失败: {response.get('message', '未知错误')}")
  19. def get_root_folder(self) -> Dict[str, Any]:
  20. endpoint = "/api/v1/file/root_folder"
  21. response = self.http_client.get(endpoint)
  22. if response.get("code") == 0 and response.get("data"):
  23. return response["data"]
  24. else:
  25. raise Exception(f"获取根目录失败: {response.get('message', '未知错误')}")
  26. def get_parent_folder(self, file_id: str) -> Dict[str, Any]:
  27. endpoint = "/api/v1/file/parent_folder"
  28. response = self.http_client.get(endpoint, params={"file_id": file_id})
  29. if response.get("code") == 0 and response.get("data"):
  30. return response["data"]
  31. else:
  32. raise Exception(f"获取父目录失败: {response.get('message', '未知错误')}")
  33. def get_all_parent_folders(self, file_id: str) -> List[Dict[str, Any]]:
  34. endpoint = "/api/v1/file/all_parent_folder"
  35. response = self.http_client.get(endpoint, params={"file_id": file_id})
  36. if response.get("code") == 0 and response.get("data"):
  37. return response["data"]
  38. else:
  39. raise Exception(f"获取所有父目录失败: {response.get('message', '未知错误')}")
  40. def get_file(self, file_id: str) -> Dict[str, Any]:
  41. endpoint = f"/api/v1/file/get/{file_id}"
  42. response = self.http_client.get(endpoint)
  43. if response.get("code") == 0 and response.get("data"):
  44. return response["data"]
  45. else:
  46. raise Exception(f"获取文件失败: {response.get('message', '未知错误')}")
  47. def upload_file(self, file_path: str) -> Dict[str, Any]:
  48. endpoint = "/api/v1/file/upload"
  49. with open(file_path, 'rb') as f:
  50. files = {'file': (file_path.split('/')[-1], f)}
  51. headers = {'Content-Type': 'multipart/form-data'}
  52. response = self.http_client.post(endpoint, files=files, headers=headers)
  53. if response.get("code") == 0:
  54. return response.get("data", {})
  55. else:
  56. raise Exception(f"上传文件失败: {response.get('message', '未知错误')}")
  57. def create_file(self, file_id: str, tenant_id: str = None) -> Dict[str, Any]:
  58. endpoint = "/api/v1/file/create"
  59. data = {"file_id": file_id}
  60. if tenant_id is not None:
  61. data["tenant_id"] = tenant_id
  62. response = self.http_client.post(endpoint, json_data=data)
  63. if response.get("code") == 0:
  64. return response.get("data", {})
  65. else:
  66. raise Exception(f"创建文件失败: {response.get('message', '未知错误')}")
  67. def delete_file(self, file_id: str) -> bool:
  68. endpoint = "/api/v1/file/rm"
  69. response = self.http_client.post(endpoint, json_data={"file_id": file_id})
  70. if response.get("code") == 0:
  71. return True
  72. else:
  73. raise Exception(f"删除文件失败: {response.get('message', '未知错误')}")
  74. def rename_file(self, file_id: str, new_name: str) -> Dict[str, Any]:
  75. endpoint = "/api/v1/file/rename"
  76. data = {
  77. "file_id": file_id,
  78. "new_name": new_name
  79. }
  80. response = self.http_client.post(endpoint, json=data)
  81. if response.get("code") == 0:
  82. return response.get("data", {})
  83. else:
  84. raise Exception(f"重命名文件失败: {response.get('message', '未知错误')}")
  85. def move_file(self, file_id: str, parent_id: str) -> Dict[str, Any]:
  86. endpoint = "/api/v1/file/mv"
  87. data = {
  88. "file_id": file_id,
  89. "parent_id": parent_id
  90. }
  91. response = self.http_client.post(endpoint, json=data)
  92. if response.get("code") == 0:
  93. return response.get("data", {})
  94. else:
  95. raise Exception(f"移动文件失败: {response.get('message', '未知错误')}")
  96. def convert_file(self, file_id: str) -> Dict[str, Any]:
  97. endpoint = "/api/v1/file/convert"
  98. response = self.http_client.post(endpoint, json={"file_id": file_id})
  99. if response.get("code") == 0:
  100. return response.get("data", {})
  101. else:
  102. raise Exception(f"转换文件失败: {response.get('message', '未知错误')}")