Slugy, webp, hromada zmen
This commit is contained in:
+14
-3
@@ -1,16 +1,27 @@
|
||||
import os
|
||||
import re
|
||||
import uuid
|
||||
from strawberry.file_uploads import Upload
|
||||
|
||||
from utils.file import check_directories
|
||||
|
||||
|
||||
async def handle_file_upload(file: Upload, path: str) -> str:
|
||||
async def handle_file_upload(file: Upload, path: str, filename_maxlength: int = 64, uid_prefix: bool = True, overwrite: bool = True) -> str:
|
||||
check_directories(path)
|
||||
|
||||
filename = f"{uuid.uuid4()}-{file.filename}"
|
||||
prefix = f"{uuid.uuid4()}-" if uid_prefix else ""
|
||||
filename = f"{prefix}{file.filename}"[-1 * filename_maxlength:]
|
||||
|
||||
# sanitize filename
|
||||
filename = re.sub('[^\w_. -]', '', filename).replace(" ", "-")
|
||||
|
||||
target_path = f"{path}/{filename}"
|
||||
|
||||
if os.path.exists(target_path) and not overwrite:
|
||||
raise FileExistsError(f"File {filename} already exists")
|
||||
|
||||
content = await file.read()
|
||||
image = open(path + "/" + filename, "wb")
|
||||
image = open(target_path, "wb")
|
||||
image.write(content)
|
||||
image.close()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user