From 025e7d72b10b7817693564b530ede36692f347a5 Mon Sep 17 00:00:00 2001 From: Do1e Date: Sat, 19 Aug 2023 20:20:01 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20OneDriveFileStorage=E4=B8=8B=E8=BD=BD?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E7=9A=84=E6=96=87=E4=BB=B6=E5=90=8D=E4=B8=8E?= =?UTF-8?q?=E4=B8=8A=E4=BC=A0=E6=97=B6=E4=B8=8D=E4=B8=80=E8=87=B4=E7=9A=84?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/storage.py | 28 +++++++++++++++++++--------- main.py | 2 +- readme_onedrive.md | 4 +--- 3 files changed, 21 insertions(+), 13 deletions(-) diff --git a/core/storage.py b/core/storage.py index bf4ba8f..de96a27 100644 --- a/core/storage.py +++ b/core/storage.py @@ -85,6 +85,7 @@ class OneDriveFileStorage: self.client_id = settings.onedrive_client_id self.username = settings.onedrive_username self.password = settings.onedrive_password + self._ClientRequestException = ClientRequestException try: client = GraphClient(self.acquire_token_pwd) @@ -116,19 +117,28 @@ class OneDriveFileStorage: path = str(path).replace('\\', '/').replace('//', '/').split('/') else: raise TypeError('path must be str or Path') - return '/'.join(path[:-1]), path[-1] + path[-1] = path[-1].split('.')[0] + return '/'.join(path) def _save(self, file, save_path): - content = file.read() - path, name = self._get_path_str(save_path) + content = file.file.read() + name = file.filename + path = self._get_path_str(save_path) self.root_path.get_by_path(path).upload(name, content).execute_query() async def save_file(self, file: UploadFile, save_path: str): - await asyncio.to_thread(self._save, file.file, save_path) + await asyncio.to_thread(self._save, file, save_path) def _delete(self, save_path): - path, name = self._get_path_str(save_path) - self.root_path.get_by_path(path + '/' + name).delete_object().execute_query() + path = self._get_path_str(save_path) + try: + self.root_path.get_by_path(path).delete_object().execute_query() + except self._ClientRequestException as e: + if e.code == 'itemNotFound': + pass + else: + raise e + async def delete_file(self, file_code: FileCodes): await asyncio.to_thread(self._delete, await file_code.get_file_path()) @@ -139,8 +149,8 @@ class OneDriveFileStorage: p3 = re.search(rf'{p2}\/(.+)', link).group(1) return f'https://{p1}.sharepoint.com/personal/{p2}/_layouts/52/download.aspx?share={p3}' - def _get_file_url(self, save_path): - path, name = self._get_path_str(save_path) + def _get_file_url(self, save_path, name): + path = self._get_path_str(save_path) remote_file = self.root_path.get_by_path(path + '/' + name) expiration_datetime = datetime.datetime.now(tz=datetime.timezone.utc) + datetime.timedelta(hours=1) expiration_datetime = expiration_datetime.strftime("%Y-%m-%dT%H:%M:%SZ") @@ -150,7 +160,7 @@ class OneDriveFileStorage: async def get_file_url(self, file_code: FileCodes): if file_code.prefix == '文本分享': return file_code.text - result = await asyncio.to_thread(self._get_file_url, await file_code.get_file_path()) + result = await asyncio.to_thread(self._get_file_url, await file_code.get_file_path(), f'{file_code.prefix}{file_code.suffix}') return result diff --git a/main.py b/main.py index c9cff10..bfce18b 100644 --- a/main.py +++ b/main.py @@ -70,4 +70,4 @@ async def index(): if __name__ == '__main__': import uvicorn - uvicorn.run(app='main:app', host="0.0.0.0", port=12345, reload=False, workers=1) + uvicorn.run(app='main:app', host="0.0.0.0", port=settings.port, reload=False, workers=1) diff --git a/readme_onedrive.md b/readme_onedrive.md index fd864bd..796a3c0 100644 --- a/readme_onedrive.md +++ b/readme_onedrive.md @@ -1,8 +1,6 @@ # OneDrive作为存储的配置方法 -**仅支持工作或学习账户,并且需要有管理员权限以授权API** - -已知问题:下载文件时文件名会变为uuid,与上传时的文件名不一致。 +**仅支持工作或学校账户,并且需要有管理员权限以授权API** ## 1. 需要配置的参数