mirror of
https://github.com/Radiquum/photos.git
synced 2025-04-06 16:24:32 +00:00
feat/admin: update generated sizes
This commit is contained in:
parent
cb2b744d01
commit
27b0d27f01
1 changed files with 59 additions and 30 deletions
|
@ -19,6 +19,13 @@ load_dotenv()
|
||||||
UPLOAD_FOLDER = "./temp"
|
UPLOAD_FOLDER = "./temp"
|
||||||
ALLOWED_EXTENSIONS = {"png", "jpg", "jpeg"}
|
ALLOWED_EXTENSIONS = {"png", "jpg", "jpeg"}
|
||||||
|
|
||||||
|
SIZES = [
|
||||||
|
(2048, 2048),
|
||||||
|
(1024, 1024),
|
||||||
|
(512, 512),
|
||||||
|
(256, 256),
|
||||||
|
]
|
||||||
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
app.config["UPLOAD_FOLDER"] = UPLOAD_FOLDER
|
app.config["UPLOAD_FOLDER"] = UPLOAD_FOLDER
|
||||||
|
|
||||||
|
@ -158,6 +165,9 @@ def ApiUpload():
|
||||||
'{"status": "error", "message": "FLASK ERR: UNSUPPORTED EXTENSION"}', 500
|
'{"status": "error", "message": "FLASK ERR: UNSUPPORTED EXTENSION"}', 500
|
||||||
)
|
)
|
||||||
|
|
||||||
|
file_path = filename.split(".")[0]
|
||||||
|
file_ext = filename.split(".")[-1]
|
||||||
|
|
||||||
Image = PIL.Image.open(os.path.join(app.config["UPLOAD_FOLDER"], filename))
|
Image = PIL.Image.open(os.path.join(app.config["UPLOAD_FOLDER"], filename))
|
||||||
try:
|
try:
|
||||||
db.collection(os.getenv("PREFIX")).add(
|
db.collection(os.getenv("PREFIX")).add(
|
||||||
|
@ -182,53 +192,72 @@ def ApiUpload():
|
||||||
return Response(
|
return Response(
|
||||||
json.dumps({"status": "error", "message": f"FIRESTORE ERR: {e}"}), 400
|
json.dumps({"status": "error", "message": f"FIRESTORE ERR: {e}"}), 400
|
||||||
)
|
)
|
||||||
|
Image.close()
|
||||||
file_path = filename.split(".")[0]
|
|
||||||
file_ext = filename.split(".")[-1]
|
|
||||||
|
|
||||||
temp_file = open(os.path.join(app.config["UPLOAD_FOLDER"], filename), "rb")
|
temp_file = open(os.path.join(app.config["UPLOAD_FOLDER"], filename), "rb")
|
||||||
s3OrigFileResponse = upload_file(
|
s3OrigFileResponse = upload_file(
|
||||||
temp_file, os.getenv("AWS_BUCKET"), f"{file_path}/{filename}", file.mimetype
|
temp_file, os.getenv("AWS_BUCKET"), f"{file_path}/{filename}", file.mimetype
|
||||||
)
|
)
|
||||||
if s3OrigFileResponse is not True:
|
if s3OrigFileResponse is not True:
|
||||||
|
db.collection(os.getenv("PREFIX")).document(
|
||||||
|
request.files["file"].filename
|
||||||
|
).delete()
|
||||||
|
s3.delete_object(Bucket=os.getenv("AWS_BUCKET"), Key=f"{file_path}/{filename}")
|
||||||
|
s3.delete_object(Bucket=os.getenv("AWS_BUCKET"), Key=f"{file_path}/")
|
||||||
return Response(
|
return Response(
|
||||||
json.dumps({"status": "error", "message": f"S3 ERR: {s3OrigFileResponse}"}),
|
json.dumps({"status": "error", "message": f"S3 ERR: {s3OrigFileResponse}"}),
|
||||||
500,
|
500,
|
||||||
)
|
)
|
||||||
temp_file.close()
|
temp_file.close()
|
||||||
|
|
||||||
size = 512, 512
|
for size in SIZES:
|
||||||
|
Image = PIL.Image.open(os.path.join(app.config["UPLOAD_FOLDER"], filename))
|
||||||
Image.thumbnail(size, PIL.Image.Resampling.LANCZOS)
|
Image.thumbnail(size, PIL.Image.Resampling.LANCZOS)
|
||||||
Image.save(os.path.join(app.config["UPLOAD_FOLDER"], f"{file_path}-512.{file_ext}"))
|
Image.save(os.path.join(app.config["UPLOAD_FOLDER"], f"{file_path}-{size[0]}.{file_ext}"))
|
||||||
Image.close()
|
Image.close()
|
||||||
|
|
||||||
temp_file = open(
|
temp_file = open(
|
||||||
os.path.join(app.config["UPLOAD_FOLDER"], f"{file_path}-512.{file_ext}"), "rb"
|
os.path.join(app.config["UPLOAD_FOLDER"], f"{file_path}-{size[0]}.{file_ext}"), "rb"
|
||||||
)
|
)
|
||||||
s3BlurFileResponse = upload_file(
|
s3Response = upload_file(
|
||||||
temp_file,
|
temp_file,
|
||||||
os.getenv("AWS_BUCKET"),
|
os.getenv("AWS_BUCKET"),
|
||||||
f"{file_path}/{file_path}-512.{file_ext}",
|
f"{file_path}/{file_path}-{size[0]}.{file_ext}",
|
||||||
file.mimetype,
|
file.mimetype,
|
||||||
)
|
)
|
||||||
if s3BlurFileResponse is not True:
|
if s3Response is not True:
|
||||||
db.collection(os.getenv("PREFIX")).document(
|
db.collection(os.getenv("PREFIX")).document(
|
||||||
request.files["file"].filename
|
request.files["file"].filename
|
||||||
).delete()
|
).delete()
|
||||||
s3.delete_object(Bucket=os.getenv("AWS_BUCKET"), Key=f"{file_path}/{filename}")
|
s3.delete_object(Bucket=os.getenv("AWS_BUCKET"), Key=f"{file_path}/{filename}")
|
||||||
s3.delete_object(
|
s3.delete_object(
|
||||||
Bucket=os.getenv("AWS_BUCKET"),
|
Bucket=os.getenv("AWS_BUCKET"),
|
||||||
Key=f"{file_path}/{file_path}-512.{file_ext}",
|
Key=f"{file_path}/{file_path}-{SIZES[0][0]}.{file_ext}",
|
||||||
|
)
|
||||||
|
s3.delete_object(
|
||||||
|
Bucket=os.getenv("AWS_BUCKET"),
|
||||||
|
Key=f"{file_path}/{file_path}-{SIZES[1][0]}.{file_ext}",
|
||||||
|
)
|
||||||
|
s3.delete_object(
|
||||||
|
Bucket=os.getenv("AWS_BUCKET"),
|
||||||
|
Key=f"{file_path}/{file_path}-{SIZES[2][0]}.{file_ext}",
|
||||||
|
)
|
||||||
|
s3.delete_object(
|
||||||
|
Bucket=os.getenv("AWS_BUCKET"),
|
||||||
|
Key=f"{file_path}/{file_path}-{SIZES[3][0]}.{file_ext}",
|
||||||
)
|
)
|
||||||
s3.delete_object(Bucket=os.getenv("AWS_BUCKET"), Key=f"{file_path}/")
|
s3.delete_object(Bucket=os.getenv("AWS_BUCKET"), Key=f"{file_path}/")
|
||||||
return Response(
|
return Response(
|
||||||
json.dumps({"status": "error", "message": f"S3 ERR: {s3BlurFileResponse}"}),
|
json.dumps({"status": "error", "message": f"S3 ERR: {s3Response}"}),
|
||||||
500,
|
500,
|
||||||
)
|
)
|
||||||
temp_file.close()
|
temp_file.close()
|
||||||
|
|
||||||
os.remove(os.path.join(app.config["UPLOAD_FOLDER"], filename))
|
os.remove(os.path.join(app.config["UPLOAD_FOLDER"], filename))
|
||||||
os.remove(os.path.join(app.config["UPLOAD_FOLDER"], f"{file_path}-512.{file_ext}"))
|
os.remove(os.path.join(app.config["UPLOAD_FOLDER"], f"{file_path}-{SIZES[0][0]}.{file_ext}"))
|
||||||
|
os.remove(os.path.join(app.config["UPLOAD_FOLDER"], f"{file_path}-{SIZES[1][0]}.{file_ext}"))
|
||||||
|
os.remove(os.path.join(app.config["UPLOAD_FOLDER"], f"{file_path}-{SIZES[2][0]}.{file_ext}"))
|
||||||
|
os.remove(os.path.join(app.config["UPLOAD_FOLDER"], f"{file_path}-{SIZES[3][0]}.{file_ext}"))
|
||||||
|
|
||||||
return {"status": "ok", "message": "Uploaded"}
|
return {"status": "ok", "message": "Uploaded"}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue