mirror of
https://github.com/aykhans/portfolio-blog.git
synced 2025-09-08 15:10:45 +00:00
First Commit
This commit is contained in:
0
src/app/utils/__init__.py
Normal file
0
src/app/utils/__init__.py
Normal file
0
src/app/utils/create_user.py
Normal file
0
src/app/utils/create_user.py
Normal file
12
src/app/utils/file_operations.py
Normal file
12
src/app/utils/file_operations.py
Normal file
@@ -0,0 +1,12 @@
|
||||
from pathlib import Path
|
||||
from os import remove
|
||||
|
||||
|
||||
async def mkdir_if_not_exists(path: Path) -> None:
|
||||
if not path.exists():
|
||||
path.mkdir(parents=True)
|
||||
|
||||
|
||||
def remove_file(file_path: str) -> None:
|
||||
try: remove(file_path)
|
||||
except FileNotFoundError: ...
|
25
src/app/utils/image_operations.py
Normal file
25
src/app/utils/image_operations.py
Normal file
@@ -0,0 +1,25 @@
|
||||
from pathlib import Path
|
||||
from PIL import Image
|
||||
|
||||
from app.utils.file_operations import mkdir_if_not_exists
|
||||
|
||||
|
||||
async def generate_unique_image_name(
|
||||
path: Path,
|
||||
image_name: Path | str,
|
||||
image_format: str
|
||||
) -> Path | str:
|
||||
|
||||
number = 1
|
||||
temp_image_name = image_name
|
||||
|
||||
while (path / temp_image_name).exists():
|
||||
temp_image_name = f'{image_name}-{number}.{image_format}'
|
||||
number += 1
|
||||
|
||||
return temp_image_name
|
||||
|
||||
|
||||
async def save_image(image: Image, image_path: Path) -> None:
|
||||
await mkdir_if_not_exists(image_path.parent)
|
||||
image.save(image_path)
|
Reference in New Issue
Block a user