commit
71dc166516
@ -81,3 +81,6 @@ yarl==1.8.1
|
|||||||
zipp==3.10.0
|
zipp==3.10.0
|
||||||
zope.event==4.5.0
|
zope.event==4.5.0
|
||||||
zope.interface==5.5.2
|
zope.interface==5.5.2
|
||||||
|
aiofiles==0.8.0
|
||||||
|
aiopathlib==0.5.0
|
||||||
|
aioshutil==1.3
|
||||||
|
@ -12,6 +12,8 @@ from application.settings import TEMP_DIR, STATIC_ROOT, BASE_DIR, STATIC_URL, ST
|
|||||||
from fastapi import UploadFile
|
from fastapi import UploadFile
|
||||||
import sys
|
import sys
|
||||||
from utils.file.file_base import FileBase
|
from utils.file.file_base import FileBase
|
||||||
|
from aiopathlib import AsyncPath as Path
|
||||||
|
import aioshutil
|
||||||
|
|
||||||
|
|
||||||
class FileManage(FileBase):
|
class FileManage(FileBase):
|
||||||
@ -39,11 +41,11 @@ class FileManage(FileBase):
|
|||||||
path = self.path
|
path = self.path
|
||||||
if sys.platform == "win32":
|
if sys.platform == "win32":
|
||||||
path = self.path.replace("/", "\\")
|
path = self.path.replace("/", "\\")
|
||||||
save_path = os.path.join(STATIC_ROOT, path)
|
# save_path = os.path.join(STATIC_ROOT, path)
|
||||||
if not os.path.exists(os.path.dirname(save_path)):
|
save_path = Path(STATIC_ROOT) / path
|
||||||
os.mkdir(os.path.dirname(save_path))
|
if not await save_path.parent.exists():
|
||||||
with open(save_path, "wb") as f:
|
await save_path.parent.mkdir(parents=True, exist_ok=True)
|
||||||
f.write(await self.file.read())
|
await save_path.write_bytes(await self.file.read())
|
||||||
return {
|
return {
|
||||||
"local_path": f"{STATIC_DIR}/{self.path}",
|
"local_path": f"{STATIC_DIR}/{self.path}",
|
||||||
"remote_path": f"{STATIC_URL}/{self.path}"
|
"remote_path": f"{STATIC_URL}/{self.path}"
|
||||||
@ -55,13 +57,13 @@ class FileManage(FileBase):
|
|||||||
保存临时文件
|
保存临时文件
|
||||||
"""
|
"""
|
||||||
date = datetime.datetime.strftime(datetime.datetime.now(), "%Y%m%d")
|
date = datetime.datetime.strftime(datetime.datetime.now(), "%Y%m%d")
|
||||||
file_dir = os.path.join(TEMP_DIR, date)
|
# file_dir = os.path.join(TEMP_DIR, date)
|
||||||
if not os.path.exists(file_dir):
|
file_dir = Path(TEMP_DIR) / date
|
||||||
os.mkdir(file_dir)
|
if not await file_dir.exists():
|
||||||
filename = os.path.join(file_dir, str(int(datetime.datetime.now().timestamp())) + file.filename)
|
await file_dir.mkdir(parents=True, exist_ok=True)
|
||||||
with open(filename, "wb") as f:
|
filename = file_dir / str(int(datetime.datetime.now().timestamp())) + file.filename
|
||||||
f.write(await file.read())
|
await filename.write_bytes(await self.file.read())
|
||||||
return filename
|
return str(filename)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def copy(src: str, dst: str):
|
def copy(src: str, dst: str):
|
||||||
@ -82,6 +84,17 @@ class FileManage(FileBase):
|
|||||||
os.mkdir(os.path.dirname(dst))
|
os.mkdir(os.path.dirname(dst))
|
||||||
shutil.copyfile(src, dst)
|
shutil.copyfile(src, dst)
|
||||||
|
|
||||||
|
async def async_copy(src: str, dst: str):
|
||||||
|
if src[0] == "/":
|
||||||
|
src = src.lstrip("/")
|
||||||
|
if sys.platform == "win32":
|
||||||
|
src = src.replace("/", "\\")
|
||||||
|
dst = dst.replace("/", "\\")
|
||||||
|
src = Path(BASE_DIR) / src
|
||||||
|
dst = Path(dst)
|
||||||
|
if not await dst.parent.exists():
|
||||||
|
await dst.parent.mkdir(parents=True, exist_ok=True)
|
||||||
|
await aioshutil.copyfile(src, dst)
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
# src = r"D:\ktianc\private\vv-reserve\reserve-api\static\system\2022-12-07\16703958210ab33912.ico"
|
# src = r"D:\ktianc\private\vv-reserve\reserve-api\static\system\2022-12-07\16703958210ab33912.ico"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user