Refaktoring a bugfixing

This commit is contained in:
Michal Kváček
2023-10-13 23:24:53 +02:00
parent ce2c023eaf
commit 7f7f79dad6
36 changed files with 698 additions and 722 deletions
+14
View File
@@ -0,0 +1,14 @@
import os
def delete_file(path: str, silent: bool = False):
try:
os.remove(path)
except Exception:
if not silent:
raise
def check_directories(path: str):
if not os.path.isdir(path):
os.makedirs(path)
+6
View File
@@ -0,0 +1,6 @@
from typing import Tuple
def gps_to_decimal(input: Tuple[float, float, float]) -> float:
d, m, s = input
return d + (m / 60.0) + (s / 3600.0)
+70
View File
@@ -0,0 +1,70 @@
from datetime import datetime
from typing import Optional
import exif
from PIL import Image, UnidentifiedImageError
from utils.file import check_directories
from utils.gps import gps_to_decimal
async def parse_exif_info(path: str, filename: str) -> dict:
with open(f"{path}/{filename}", "rb") as f:
img = exif.Image(f)
if not img.has_exif:
return {}
exif_info = img.get_all()
for datetime_field in ("datetime", "datetime_original", "datetime_digitized"):
if exif_info.get(datetime_field):
exif_info[datetime_field] = datetime.strptime(exif_info[datetime_field], "%Y:%m:%d %H:%M:%S")
if exif_info.get("gps_latitude"):
exif_info["gps_latitude"] = gps_to_decimal(exif_info["gps_latitude"])
if exif_info.get("gps_longitude"):
exif_info["gps_longitude"] = gps_to_decimal(exif_info["gps_longitude"])
return exif_info
async def resize_image(
path: str, filename: str, new_width: int, quality: int = 90,
dest_path: Optional[str] = None,
dest_filename: Optional[str] = None
):
if not dest_path:
dest_path = path
if not dest_filename:
dest_filename = filename
try:
image = Image.open(f"{path}/{filename}")
width, height = image.size
new_height = int(new_width * height / width)
image = image.resize((new_width, new_height), Image.LANCZOS)
check_directories(dest_path)
image.save(f"{dest_path}/{dest_filename}", 'JPEG', quality=quality)
except UnidentifiedImageError:
pass
async def rotate_image(
path: str,
filename: str,
angle: int,
dest_path: Optional[str] = None,
dest_filename: Optional[str] = None
):
if not dest_path:
dest_path = path
if not dest_filename:
dest_filename = filename
img = Image.open(f"{path}/{filename}")
img = img.rotate(angle, Image.LANCZOS, expand=True)
check_directories(dest_path)
img.save(f"{dest_path}/{dest_filename}", 'JPEG', quality=100)
+17
View File
@@ -0,0 +1,17 @@
import uuid
from strawberry.file_uploads import Upload
from utils.file import check_directories
async def handle_file_upload(file: Upload, path: str) -> str:
check_directories(path)
filename = f"{uuid.uuid4()}-{file.filename}"
content = await file.read()
image = open(path + "/" + filename, "wb")
image.write(content)
image.close()
return filename