Random cleany

This commit is contained in:
Michał 2023-03-12 14:52:24 +00:00
parent dea3cca4a6
commit 7ef4a1dd91

View file

@ -6,17 +6,16 @@ from uuid import uuid4
import os import os
import io import io
import logging import logging
import json
from datetime import datetime as dt from datetime import datetime as dt
from flask import ( from flask import (Blueprint, send_from_directory, send_file,
Blueprint, send_from_directory, send_file, abort, flash, jsonify, request, g, current_app) abort, flash, jsonify, request, g, current_app)
from werkzeug.utils import secure_filename from werkzeug.utils import secure_filename
from colorthief import ColorThief from colorthief import ColorThief
from PIL import Image, ImageOps, ImageFilter from PIL import Image, ImageOps, ImageFilter
from sqlalchemy.orm import sessionmaker
from sqlalchemy.orm import sessionmaker
from gallery.auth import login_required from gallery.auth import login_required
from . import db from . import db
@ -55,7 +54,6 @@ def uploads(file):
elif width == 0 and height > 0: elif width == 0 and height > 0:
width = height width = height
set_ext = current_app.config['ALLOWED_EXTENSIONS']
buff = io.BytesIO() buff = io.BytesIO()
# Open image and set extension # Open image and set extension
@ -69,9 +67,8 @@ def uploads(file):
abort(500) abort(500)
img_ext = os.path.splitext(file)[-1].lower().replace('.', '') img_ext = os.path.splitext(file)[-1].lower().replace('.', '')
img_ext = set_ext[img_ext] img_ext = current_app.config['ALLOWED_EXTENSIONS'][img_ext]
# Get ICC profile as it alters colours when saving img_icc = img.info.get("icc_profile") # Get ICC profile as it alters colours when saving
img_icc = img.info.get("icc_profile")
# Resize image and orientate correctly # Resize image and orientate correctly
img.thumbnail((width, height), Image.LANCZOS) img.thumbnail((width, height), Image.LANCZOS)
@ -89,8 +86,8 @@ def uploads(file):
try: try:
img.save(buff, img_ext, icc_profile=img_icc) img.save(buff, img_ext, icc_profile=img_icc)
except OSError: except OSError:
# This usually happens when saving a JPEG with an ICC profile # This usually happens when saving a JPEG with an ICC profile,
# Convert to RGB and try again # so we convert to RGB and try again
img = img.convert('RGB') img = img.convert('RGB')
img.save(buff, img_ext, icc_profile=img_icc) img.save(buff, img_ext, icc_profile=img_icc)
except Exception as err: except Exception as err:
@ -98,9 +95,8 @@ def uploads(file):
abort(500) abort(500)
img.close() img.close()
buff.seek(0) # Reset buffer to start
# Seek to beginning of buffer and return
buff.seek(0)
return send_file(buff, mimetype='image/' + img_ext) return send_file(buff, mimetype='image/' + img_ext)
@ -205,13 +201,9 @@ def create_group():
""" """
Creates a group Creates a group
""" """
group_name = request.form['name'] new_group = db.Groups(name=request.form['name'],
group_description = request.form['description'] description=request.form['description'],
group_author = g.user.id author_id=g.user.id,
new_group = db.Groups(name=group_name,
description=group_description,
author_id=group_author,
created_at=dt.utcnow()) created_at=dt.utcnow())
db_session.add(new_group) db_session.add(new_group)
@ -269,11 +261,10 @@ def logfile():
""" """
Gets the log file and returns it as a JSON object Gets the log file and returns it as a JSON object
""" """
filename = 'only.log'
log_dict = {} log_dict = {}
i = 0 i = 0
with open(filename, encoding='utf-8') as file: with open('only.log', encoding='utf-8') as file:
for line in file: for line in file:
line = line.split(' : ') line = line.split(' : ')