2023-01-13 18:29:07 +00:00
|
|
|
from flask import Blueprint, render_template, current_app, send_from_directory, send_file, request, g, abort, flash
|
2023-01-10 14:40:43 +00:00
|
|
|
from werkzeug.utils import secure_filename
|
2023-01-11 19:46:31 +00:00
|
|
|
from gallery.auth import login_required
|
|
|
|
from gallery.db import get_db
|
2023-01-13 18:29:07 +00:00
|
|
|
from PIL import Image, ImageOps
|
|
|
|
import io
|
2023-01-10 18:12:55 +00:00
|
|
|
import os
|
2023-01-11 19:46:31 +00:00
|
|
|
from uuid import uuid4
|
2023-01-10 14:40:43 +00:00
|
|
|
|
2023-01-11 19:46:31 +00:00
|
|
|
blueprint = Blueprint('viewsbp', __name__, url_prefix='/api')
|
2023-01-10 14:40:43 +00:00
|
|
|
|
|
|
|
|
2023-01-13 18:29:07 +00:00
|
|
|
@blueprint.route('/uploads/<file>/<int:quality>', methods=['GET'])
|
|
|
|
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)
|
2023-01-10 14:40:43 +00:00
|
|
|
|
2023-01-13 18:29:07 +00:00
|
|
|
# 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_ext = set_ext[img_ext]
|
|
|
|
|
|
|
|
# Resize image and orientate correctly
|
|
|
|
img.thumbnail((quality, quality), Image.LANCZOS)
|
|
|
|
img = ImageOps.exif_transpose(img)
|
|
|
|
img.save(buff, img_ext)
|
|
|
|
|
|
|
|
# Seek to beginning of buffer and return
|
|
|
|
buff.seek(0)
|
|
|
|
return send_file(buff, mimetype='image/'+img_ext)
|
2023-01-11 19:46:31 +00:00
|
|
|
|
|
|
|
@blueprint.route('/upload', methods=['POST'])
|
|
|
|
@login_required
|
|
|
|
def upload():
|
2023-01-13 18:29:07 +00:00
|
|
|
form_file = request.files['file']
|
2023-01-11 19:46:31 +00:00
|
|
|
form = request.form
|
2023-01-13 18:29:07 +00:00
|
|
|
|
|
|
|
if not form_file:
|
2023-01-11 19:46:31 +00:00
|
|
|
return abort(404)
|
|
|
|
|
2023-01-13 18:29:07 +00:00
|
|
|
img_ext = os.path.splitext(secure_filename(form_file.filename))[-1].lower()
|
|
|
|
img_name = f"GWAGWA_{uuid4().__str__()}{img_ext}"
|
2023-01-11 19:46:31 +00:00
|
|
|
|
2023-01-13 18:29:07 +00:00
|
|
|
if not img_ext in current_app.config['ALLOWED_EXTENSIONS']:
|
|
|
|
return 'File extension not allowed: '+img_ext
|
2023-01-11 19:46:31 +00:00
|
|
|
|
2023-01-13 18:29:07 +00:00
|
|
|
# Save to database
|
2023-01-11 19:46:31 +00:00
|
|
|
try:
|
2023-01-13 18:29:07 +00:00
|
|
|
db = get_db()
|
|
|
|
db.execute(
|
|
|
|
'INSERT INTO posts (file_name, author_id, description, alt)'
|
|
|
|
' VALUES (?, ?, ?, ?)',
|
|
|
|
(img_name, g.user['id'], form['description'], form['alt'])
|
|
|
|
)
|
|
|
|
db.commit()
|
2023-01-11 19:46:31 +00:00
|
|
|
except Exception as e:
|
2023-01-13 18:29:07 +00:00
|
|
|
abort(500)
|
2023-01-11 19:46:31 +00:00
|
|
|
|
2023-01-13 18:29:07 +00:00
|
|
|
# Save file
|
|
|
|
try:
|
|
|
|
form_file.save(os.path.join(current_app.config['UPLOAD_FOLDER'], img_name))
|
|
|
|
except:
|
|
|
|
abort(500)
|
2023-01-11 19:46:31 +00:00
|
|
|
|
|
|
|
return 'Gwa Gwa'
|
|
|
|
|
|
|
|
@blueprint.route('/remove/<int:id>', methods=['POST'])
|
|
|
|
@login_required
|
|
|
|
def remove(id):
|
2023-01-13 18:29:07 +00:00
|
|
|
img = get_db().execute(
|
2023-01-11 19:46:31 +00:00
|
|
|
'SELECT author_id, file_name FROM posts WHERE id = ?',
|
|
|
|
(id,)
|
|
|
|
).fetchone()
|
|
|
|
|
2023-01-13 18:29:07 +00:00
|
|
|
if img is None:
|
2023-01-11 19:46:31 +00:00
|
|
|
abort(404)
|
2023-01-13 18:29:07 +00:00
|
|
|
if img['author_id'] != g.user['id']:
|
2023-01-11 19:46:31 +00:00
|
|
|
abort(403)
|
|
|
|
|
|
|
|
try:
|
2023-01-13 18:29:07 +00:00
|
|
|
os.remove(os.path.join(current_app.config['UPLOAD_FOLDER'], img['file_name']))
|
2023-01-11 19:46:31 +00:00
|
|
|
except Exception as e:
|
2023-01-13 18:29:07 +00:00
|
|
|
abort(500)
|
2023-01-11 19:46:31 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
db = get_db()
|
|
|
|
db.execute('DELETE FROM posts WHERE id = ?', (id,))
|
|
|
|
db.commit()
|
|
|
|
except:
|
2023-01-13 18:29:07 +00:00
|
|
|
abort(500)
|
2023-01-11 19:46:31 +00:00
|
|
|
|
|
|
|
return 'Gwa Gwa'
|