Skip to content

Commit

Permalink
fix a test error that program crash when ./static doesn't exists
Browse files Browse the repository at this point in the history
  • Loading branch information
hv0905 committed Dec 25, 2023
1 parent 1b383d8 commit d119def
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 10 deletions.
15 changes: 6 additions & 9 deletions app/Controllers/admin.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from pathlib import Path
from typing import Annotated
from uuid import UUID

Expand All @@ -11,6 +10,7 @@
from app.Services.authentication import force_admin_token_verify
from app.Services.vector_db_context import PointNotFoundError
from app.config import config
from app.util import directories

admin_router = APIRouter(dependencies=[Depends(force_admin_token_verify)], tags=["Admin"])

Expand All @@ -28,23 +28,20 @@ async def delete_image(
db_context.deleteItems([str(point.id)])
logger.success("Image {} deleted from database.", point.id)

if point.url.startswith('/'): # local image
static_folder = Path(config.static_file.path)
image_files = list(static_folder.glob(f"{point.id}.*"))
if point.url.startswith('/') and config.static_file.enable: # local image
image_files = list(directories.static_dir.glob(f"{point.id}.*"))
assert len(image_files) <= 1

if len(image_files) == 0:
logger.warning("Image {} is a local image but not found in static folder.", point.id)
else:
deleted_folder = static_folder / "_deleted"
deleted_folder.mkdir(parents=True, exist_ok=True)
directories.deleted_dir.mkdir(parents=True, exist_ok=True)

image_files[0].rename(deleted_folder / image_files[0].name)
image_files[0].rename(directories.deleted_dir / image_files[0].name)
logger.success("Local image {} removed.", image_files[0].name)

if point.thumbnail_url is not None:
thumbnail_dir = static_folder / "thumbnails"
thumbnail_file = thumbnail_dir / f"{point.id}.webp"
thumbnail_file = directories.thumbnails_dir / f"{point.id}.webp"
if thumbnail_file.is_file():
thumbnail_file.unlink()
logger.success("Thumbnail {} removed.", thumbnail_file.name)
Expand Down
30 changes: 30 additions & 0 deletions app/util/directories.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from pathlib import Path

from loguru import logger

from app.config import config, environment

static_dir = None
thumbnails_dir = None
deleted_dir = None


def init():
global static_dir, thumbnails_dir, deleted_dir
static_dir = Path(config.static_file.path)
thumbnails_dir = static_dir / "thumbnails"
deleted_dir = static_dir / "_deleted"

if not static_dir.is_dir():
static_dir.mkdir(parents=True)
logger.warning(f"static_dir {static_dir} not found, created.")
if not thumbnails_dir.is_dir():
thumbnails_dir.mkdir(parents=True)
logger.warning(f"thumbnails_dir {thumbnails_dir} not found, created.")
if not deleted_dir.is_dir():
deleted_dir.mkdir(parents=True)
logger.warning(f"deleted_dir {deleted_dir} not found, created.")


if config.static_file.enable or environment.local_indexing:
init()
3 changes: 2 additions & 1 deletion app/webapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from app.config import config
from .Models.api_response.base import WelcomeApiResponse, WelcomeApiAuthenticationResponse, \
WelcomeApiAdminPortalAuthenticationResponse
from .util import directories
from .util.fastapi_log_handler import init_logging

app = FastAPI()
Expand All @@ -30,7 +31,7 @@
app.include_router(admin_router, prefix="/admin")

if config.static_file.enable:
app.mount("/static", StaticFiles(directory=config.static_file.path), name="static")
app.mount("/static", StaticFiles(directory=directories.static_dir), name="static")


@app.get("/", description="Default portal. Test for server availability.")
Expand Down

0 comments on commit d119def

Please sign in to comment.