fix: OneDriveFileStorage下载文件的文件名与上传时不一致的问题

This commit is contained in:
Do1e
2023-08-19 20:20:01 +08:00
parent bc780208c4
commit c38ffe3916
3 changed files with 21 additions and 13 deletions
+19 -9
View File
@@ -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
+1 -1
View File
@@ -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)
+1 -3
View File
@@ -1,8 +1,6 @@
# OneDrive作为存储的配置方法
**仅支持工作或学账户,并且需要有管理员权限以授权API**
已知问题:下载文件时文件名会变为uuid,与上传时的文件名不一致。
**仅支持工作或学账户,并且需要有管理员权限以授权API**
## 1. 需要配置的参数