Skip to content

Commit

Permalink
perf: Use Path instead of os.path
Browse files Browse the repository at this point in the history
  • Loading branch information
amisadmin committed Aug 5, 2023
1 parent 8f4e317 commit 7186493
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 8 deletions.
8 changes: 4 additions & 4 deletions fastapi_amis_admin/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,22 @@
__url__ = "https://github.com/amisadmin/fastapi_amis_admin"

import gettext
import os
from pathlib import Path

from .utils.translation import i18n

BASE_DIR = os.path.dirname(os.path.abspath(__file__))
BASE_DIR = Path(__file__).resolve().parent

i18n.load_translations(
{
"zh_CN": gettext.translation(
domain="messages",
localedir=os.path.join(BASE_DIR, "locale"),
localedir=BASE_DIR / "locale",
languages=["zh_CN"],
),
"de_DE": gettext.translation(
domain="messages",
localedir=os.path.join(BASE_DIR, "locale"),
localedir=BASE_DIR / "locale",
languages=["de_DE"],
),
}
Expand Down
6 changes: 2 additions & 4 deletions fastapi_amis_admin/admin/site.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,14 +92,14 @@ class FileAdmin(admin.RouterAdmin):
def __init__(self, app: "AdminApp"):
super().__init__(app)
self.file_directory = self.file_directory or self.file_path
os.makedirs(self.file_directory, exist_ok=True)
self.static_path = self.mount_staticfile()

def get_filename(self, file: UploadFile):
filename = str(uuid.uuid4()).replace("-", "") + os.path.splitext(file.filename)[1]
return Path().joinpath(time.strftime("%Y%m"), filename).as_posix()

def mount_staticfile(self) -> str:
os.path.exists(self.file_directory) or os.makedirs(self.file_directory)
self.site.fastapi.mount(
self.file_path,
StaticFiles(directory=self.file_directory),
Expand All @@ -111,9 +111,7 @@ def register_router(self):
@self.router.post(self.file_path, response_model=BaseApiOut[self.UploadOutSchema])
async def file_upload(file: UploadFile = File(...)):
filename = self.get_filename(file)
file_path = os.path.join(self.file_directory, filename)
file_dir = os.path.dirname(file_path)
os.path.exists(file_dir) or os.makedirs(file_dir)
file_path = Path(self.file_directory) / filename
try:
res = await file.read()
if self.file_max_size and len(res) > self.file_max_size:
Expand Down

0 comments on commit 7186493

Please sign in to comment.