Skip to content

Commit

Permalink
[35] Delete any existing Font files when adding Font via API
Browse files Browse the repository at this point in the history
  • Loading branch information
CollinHeist committed Sep 5, 2024
1 parent d265ef7 commit 7bcc09b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
15 changes: 13 additions & 2 deletions app/routers/fonts.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ def create_font(

@font_router.put('/{font_id}/file')
async def add_font_file(
request: Request,
font_id: int,
file: UploadFile,
db: Session = Depends(get_database),
Expand All @@ -77,13 +78,23 @@ async def add_font_file(
font = get_font(db, font_id, raise_exc=True)

# Download file, raise 400 if contentless
file_content = await file.read()
if not file_content:
if not (file_content := await file.read()):
raise HTTPException(
status_code=400,
detail='Font file is invalid',
)

# Delete existing file (if present)
if (existing_font := font.file):
try:
existing_font.unlink(missing_ok=True)
except OSError as exc:
request.state.log.exception('Unable to delete Font file')
raise HTTPException(
status_code=400,
detail=f'Error deleting Font file - {exc}',
)

# Write to file
font_directory = preferences.asset_directory / 'fonts'
file_path = font_directory / str(font.id) / file.filename
Expand Down
2 changes: 1 addition & 1 deletion modules/ref/version_webui
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v2.0-alpha.12.0-webui34
v2.0-alpha.12.0-webui35

0 comments on commit 7bcc09b

Please sign in to comment.