changed save image
This commit is contained in:
@@ -53,6 +53,9 @@ class Settings(BaseSettings):
|
||||
POSTGRES_USER: str
|
||||
POSTGRES_PASSWORD: str = ""
|
||||
POSTGRES_DB: str = ""
|
||||
AccountID: str = ""
|
||||
access_key_id: str = ""
|
||||
secret_access_key: str = ""
|
||||
|
||||
@computed_field # type: ignore[prop-decorator]
|
||||
@property
|
||||
|
@@ -1,5 +1,7 @@
|
||||
import os
|
||||
import logging
|
||||
import boto3
|
||||
from botocore.exceptions import ClientError
|
||||
from dataclasses import dataclass
|
||||
from datetime import datetime, timedelta, timezone
|
||||
from pathlib import Path
|
||||
@@ -153,25 +155,49 @@ def validate_file_size_type(file: IO):
|
||||
if real_file_size > FILE_SIZE:
|
||||
raise HTTPException(status_code=status.HTTP_413_REQUEST_ENTITY_TOO_LARGE, detail="Too large")
|
||||
|
||||
async def save_picture(file, folderName: str = '', fileName: str = None):
|
||||
randon_uid = str(uuid4())
|
||||
_, f_ext = os.path.splitext(file.filename)
|
||||
# async def save_picture(file, folderName: str = '', fileName: str = None):
|
||||
# randon_uid = str(uuid4())
|
||||
# _, f_ext = os.path.splitext(file.filename)
|
||||
|
||||
picture_name = (randon_uid if fileName==None else fileName.lower().replace(' ', '')) + f_ext
|
||||
# picture_name = (randon_uid if fileName==None else fileName.lower().replace(' ', '')) + f_ext
|
||||
|
||||
path = os.path.join(static,folderName)
|
||||
if not os.path.exists(path):
|
||||
os.makedirs(path)
|
||||
# path = os.path.join(static,folderName)
|
||||
# if not os.path.exists(path):
|
||||
# os.makedirs(path)
|
||||
|
||||
picture_path = os.path.join(path,picture_name)
|
||||
# picture_path = os.path.join(path,picture_name)
|
||||
|
||||
# #output_size = (125,125)
|
||||
# img = Image.open(file.file)
|
||||
|
||||
# #img.thumbnail(output_size)
|
||||
# img.save(picture_path)
|
||||
|
||||
# return f'{static}/{folderName}/{picture_name}'
|
||||
|
||||
async def save_picture(file, folderName: str = "", fileName: str = None):
|
||||
random_uid = str(uuid4())
|
||||
_, f_ext = os.path.splitext(file.filename)
|
||||
|
||||
picture_name = (fileName.lower().replace(" ", "") if fileName else random_uid) + f_ext
|
||||
|
||||
r2 = boto3.client(
|
||||
"s3",
|
||||
endpoint_url=f"https://{settings.AccountID}.r2.cloudflarestorage.com",
|
||||
aws_access_key_id=settings.access_key_id,
|
||||
aws_secret_access_key=settings.secret_access_key,
|
||||
)
|
||||
|
||||
#output_size = (125,125)
|
||||
img = Image.open(file.file)
|
||||
|
||||
#img.thumbnail(output_size)
|
||||
img.save(picture_path)
|
||||
|
||||
return f'{static}/{folderName}/{picture_name}'
|
||||
img_byte_arr = io.BytesIO()
|
||||
img.save(img_byte_arr, format='PNG')
|
||||
img_byte_arr = img_byte_arr.getvalue()
|
||||
|
||||
r2.put_object(Bucket=settings.bucket_name, Key=f"{folderName}/{picture_name}", Body=img_byte_arr)
|
||||
|
||||
r2_url = f"https://{settings.AccountID}.r2.cloudflarestorage.com/{settings.bucket_name}/{folderName}/{picture_name}"
|
||||
return r2_url
|
||||
|
||||
|
||||
async def del_picture(picture_path):
|
||||
try:
|
||||
|
Reference in New Issue
Block a user