异步上传保存文件到本地问题修复

This commit is contained in:
ktianc 2024-03-02 19:53:24 +08:00
parent 20a425ed8c
commit 7036c1fc02

View File

@ -63,19 +63,21 @@ class FileManage(FileBase):
async def async_save_local(self) -> dict: async def async_save_local(self) -> dict:
""" """
保存文件到本地 保存文件到本地
:return: :return: 示例
{
'local_path': 'D:\\project\\kinit_dev\\kinit-api\\static\\system\\20240301\\1709303205HuYB3mrC.png',
'remote_path': '/media/system/20240301/1709303205HuYB3mrC.png'
}
""" """
path = self.path path = AsyncPath(self.path)
if sys.platform == "win32": if sys.platform == "win32":
path = self.path.replace("/", "\\") path = AsyncPath(self.path.replace("/", "\\"))
save_path = AsyncPath(STATIC_ROOT) / path if not await path.parent.exists():
if not await save_path.parent.exists(): await path.parent.mkdir(parents=True, exist_ok=True)
await save_path.parent.mkdir(parents=True, exist_ok=True) await path.write_bytes(await self.file.read())
await save_path.write_bytes(await self.file.read())
remote_path = path.replace(STATIC_ROOT, '').replace("\\", '/')
return { return {
"local_path": path, "local_path": str(path),
"remote_path": f"{STATIC_URL}/{remote_path}" "remote_path": STATIC_URL + str(path).replace(STATIC_ROOT, '').replace("\\", '/')
} }
@classmethod @classmethod