Названия теперь состоят из UUID
This commit is contained in:
parent
ba4050d65e
commit
cccba16aa5
1 changed files with 26 additions and 18 deletions
44
app/main.py
44
app/main.py
|
@ -1,7 +1,5 @@
|
|||
import random
|
||||
import shutil
|
||||
import string
|
||||
import os
|
||||
import uuid
|
||||
from io import BytesIO
|
||||
|
||||
import numpy as np
|
||||
|
@ -14,14 +12,14 @@ from fastapi.middleware.cors import CORSMiddleware
|
|||
def find_average_hex_color(image: Image.Image):
|
||||
np_image = np.array(image)
|
||||
average_color = np.mean(np_image, axis=(0, 1))
|
||||
average_color_hex = '#{:02x}{:02x}{:02x}'.format(int(average_color[0]), int(average_color[1]), int(average_color[2]))
|
||||
average_color_hex = '#{:02x}{:02x}{:02x}'.format(int(average_color[0]), int(average_color[1]),
|
||||
int(average_color[2]))
|
||||
return average_color_hex
|
||||
|
||||
|
||||
def generate_random_string(length=16):
|
||||
"""Генерирует случайную строку заданной длины."""
|
||||
characters = string.ascii_letters + string.digits
|
||||
return ''.join(random.choice(characters) for _ in range(length))
|
||||
def generate_uuid_from_file(file_content):
|
||||
namespace_oid = uuid.UUID('6ba7b812-9dad-11d1-80b4-00c04fd430c8')
|
||||
return uuid.uuid5(namespace_oid, str(file_content))
|
||||
|
||||
|
||||
app = FastAPI(
|
||||
|
@ -51,31 +49,41 @@ app.mount("/static", StaticFiles(directory="app/static"), name="static")
|
|||
@app.post('/upload_avatar', response_model=dict[str, str])
|
||||
async def upload_avatar(file: UploadFile):
|
||||
content = await file.read()
|
||||
name = generate_uuid_from_file(content)
|
||||
image = Image.open(BytesIO(content))
|
||||
|
||||
new_size = (512, 512)
|
||||
resized_image = image.resize(new_size)
|
||||
|
||||
average_color_hex = find_average_hex_color(resized_image)
|
||||
name = generate_random_string()
|
||||
image_url = f'static/images/avatars/{name}_avatar.png'
|
||||
|
||||
image_url = f'static/images/avatars/{name}_avatar.{image.format.lower()}'
|
||||
with open('app/' + image_url, 'wb+') as file_object:
|
||||
resized_image.save(file_object, format="PNG")
|
||||
resized_image.save(file_object, format=image.format.lower())
|
||||
return {'image_url': image_url, 'hex_color': average_color_hex}
|
||||
|
||||
|
||||
@app.post('/upload_image', response_model=dict[str, str])
|
||||
async def upload_image(file: UploadFile):
|
||||
name = generate_random_string(50)
|
||||
image_url = f'static/images/images/{name}_image.png'
|
||||
content = await file.read()
|
||||
name = generate_uuid_from_file(content)
|
||||
image = Image.open(BytesIO(content))
|
||||
|
||||
image_url = f'static/images/images/{name}_image.{image.format.lower()}'
|
||||
with open('app/' + image_url, 'wb+') as file_object:
|
||||
shutil.copyfileobj(file.file, file_object)
|
||||
image.save(file_object, format=image.format.lower())
|
||||
return {'image_url': image_url}
|
||||
|
||||
|
||||
@app.post('/upload_background', response_model=list[str])
|
||||
async def upload_background(file: UploadFile):
|
||||
name = generate_random_string()
|
||||
image_url = f'static/images/backgrounds/{name}_background.png'
|
||||
content = await file.read()
|
||||
name = generate_uuid_from_file(content)
|
||||
image = Image.open(BytesIO(content))
|
||||
|
||||
image_url = f'static/images/backgrounds/{name}_background.{image.format.lower()}'
|
||||
with open('app/' + image_url, 'wb+') as file_object:
|
||||
shutil.copyfileobj(file.file, file_object)
|
||||
backgrounds = ['static/images/backgrounds/' + background for background in os.listdir('app/static/images/backgrounds')]
|
||||
image.save(file_object, format=image.format.lower())
|
||||
backgrounds = ['static/images/backgrounds/' + background for background in
|
||||
os.listdir('app/static/images/backgrounds')]
|
||||
return backgrounds
|
||||
|
|
Loading…
Add table
Reference in a new issue