Unifying style of the gallery

Fixed HTML and Sass layout
Moved upload and login pages to popups
Added deletion confirmation
This commit is contained in:
Michał Gdula 2023-01-25 15:13:56 +00:00
parent 0414cda5d3
commit bb98701430
44 changed files with 1777 additions and 1631 deletions

View file

@ -14,18 +14,24 @@ blueprint = Blueprint('viewsbp', __name__, url_prefix='/api')
def uploads(file, quality):
# If quality is 0, return original file
if quality == 0:
return send_from_directory(current_app.config['UPLOAD_FOLDER'], secure_filename(file), as_attachment=True)
return send_from_directory(current_app.config['UPLOAD_FOLDER'],
secure_filename(file),
as_attachment=True)
# Set variables
set_ext = {'jpg': 'jpeg', 'jpeg': 'jpeg', 'png': 'png', 'webp': 'webp'}
buff = io.BytesIO()
# Open image and set extension
img = Image.open(os.path.join(current_app.config['UPLOAD_FOLDER'], secure_filename(file)))
img_ext = os.path.splitext(secure_filename(file))[-1].lower().replace('.', '')
img = Image.open(
os.path.join(current_app.config['UPLOAD_FOLDER'],
secure_filename(file)))
img_ext = os.path.splitext(secure_filename(file))[-1].lower().replace(
'.', '')
img_ext = set_ext[img_ext]
img_icc = img.info.get("icc_profile") # Get ICC profile as it alters colours
img_icc = img.info.get(
"icc_profile") # Get ICC profile as it alters colours
# Resize image and orientate correctly
img.thumbnail((quality, quality), Image.LANCZOS)
img = ImageOps.exif_transpose(img)
@ -34,7 +40,8 @@ def uploads(file, quality):
# 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)
@blueprint.route('/upload', methods=['POST'])
@login_required
@ -44,56 +51,59 @@ def upload():
if not form_file:
return abort(404)
img_ext = os.path.splitext(secure_filename(form_file.filename))[-1].lower()
img_name = f"GWAGWA_{uuid4().__str__()}{img_ext}"
if not img_ext in current_app.config['ALLOWED_EXTENSIONS']:
abort(403)
# Save to database
try:
db = get_db()
db.execute(
'INSERT INTO posts (file_name, author_id, description, alt)'
' VALUES (?, ?, ?, ?)',
(img_name, g.user['id'], form['description'], form['alt'])
)
(img_name, g.user['id'], form['description'], form['alt']))
db.commit()
except Exception as e:
abort(500)
# Save file
# Save file
try:
form_file.save(os.path.join(current_app.config['UPLOAD_FOLDER'], img_name))
form_file.save(
os.path.join(current_app.config['UPLOAD_FOLDER'], img_name))
except:
abort(500)
return 'Gwa Gwa'
@blueprint.route('/remove/<int:id>', methods=['POST'])
@login_required
def remove(id):
img = get_db().execute(
'SELECT author_id, file_name FROM posts WHERE id = ?',
(id,)
).fetchone()
(id, )).fetchone()
if img is None:
abort(404)
if img['author_id'] != g.user['id']:
abort(403)
try:
os.remove(os.path.join(current_app.config['UPLOAD_FOLDER'], img['file_name']))
os.remove(
os.path.join(current_app.config['UPLOAD_FOLDER'],
img['file_name']))
except Exception as e:
abort(500)
try:
db = get_db()
db.execute('DELETE FROM posts WHERE id = ?', (id,))
db.execute('DELETE FROM posts WHERE id = ?', (id, ))
db.commit()
except:
abort(500)
flash(['Image was all in Le Head!', 1])
return 'Gwa Gwa'