Изменил бекграунды

This commit is contained in:
urec56 2024-02-19 15:27:03 +03:00
parent f5ba50e65e
commit cdf4aae867

View file

@ -1,10 +1,12 @@
import random import random
import shutil import shutil
import string import string
import os
from fastapi import FastAPI, UploadFile from fastapi import FastAPI, UploadFile, Query
from fastapi.staticfiles import StaticFiles from fastapi.staticfiles import StaticFiles
from fastapi.middleware.cors import CORSMiddleware from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel
def generate_random_string(length=16): def generate_random_string(length=16):
@ -46,16 +48,6 @@ async def upload_avatar(file: UploadFile):
return {'image_url': image_url} return {'image_url': image_url}
@app.post('/upload_background', response_model=dict[str, str])
async def upload_background(file: UploadFile):
name = file.filename.split('.')[0]
file_format = file.filename.split('.')[-1]
image_url = f'static/images/backgrounds/{name}_background.{file_format}'
with open('app/' + image_url, 'wb+') as file_object:
shutil.copyfileobj(file.file, file_object)
return {'image_url': image_url}
@app.post('/upload_image', response_model=dict[str, str]) @app.post('/upload_image', response_model=dict[str, str])
async def upload_image(file: UploadFile): async def upload_image(file: UploadFile):
name = generate_random_string(50) name = generate_random_string(50)
@ -63,3 +55,13 @@ async def upload_image(file: UploadFile):
with open('app/' + image_url, 'wb+') as file_object: with open('app/' + image_url, 'wb+') as file_object:
shutil.copyfileobj(file.file, file_object) shutil.copyfileobj(file.file, file_object)
return {'image_url': image_url} 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'
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')]
return backgrounds