2023-01-31 23:44:44 +00:00
|
|
|
from flask import Blueprint, current_app, send_from_directory, send_file, request, g, abort, flash, jsonify
|
2023-01-10 14:40:43 +00:00
|
|
|
from werkzeug.utils import secure_filename
|
2023-03-01 23:29:34 +00:00
|
|
|
|
2023-01-11 19:46:31 +00:00
|
|
|
from gallery.auth import login_required
|
|
|
|
from gallery.db import get_db
|
2023-03-01 23:29:34 +00:00
|
|
|
|
2023-01-13 18:29:07 +00:00
|
|
|
from PIL import Image, ImageOps
|
2023-01-31 17:32:22 +00:00
|
|
|
from . import metadata as mt
|
2023-03-01 23:29:34 +00:00
|
|
|
|
|
|
|
from .logger import logger
|
|
|
|
|
|
|
|
from uuid import uuid4
|
2023-01-13 18:29:07 +00:00
|
|
|
import io
|
2023-01-10 18:12:55 +00:00
|
|
|
import os
|
2023-03-01 23:29:34 +00:00
|
|
|
import time
|
2023-01-10 14:40:43 +00:00
|
|
|
|
2023-03-01 23:29:34 +00:00
|
|
|
blueprint = Blueprint('api', __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:
|
2023-01-25 15:13:56 +00:00
|
|
|
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
|
2023-03-01 23:29:34 +00:00
|
|
|
set_ext = current_app.config['ALLOWED_EXTENSIONS']
|
2023-01-13 18:29:07 +00:00
|
|
|
buff = io.BytesIO()
|
2023-01-25 15:13:56 +00:00
|
|
|
|
2023-01-13 18:29:07 +00:00
|
|
|
# Open image and set extension
|
2023-03-01 23:29:34 +00:00
|
|
|
try:
|
|
|
|
img = Image.open(
|
|
|
|
os.path.join(current_app.config['UPLOAD_FOLDER'],
|
|
|
|
secure_filename(file)))
|
|
|
|
except Exception as e:
|
|
|
|
logger.server(600, f"Error opening image: {e}")
|
|
|
|
abort(500)
|
|
|
|
|
2023-01-25 15:13:56 +00:00
|
|
|
img_ext = os.path.splitext(secure_filename(file))[-1].lower().replace(
|
|
|
|
'.', '')
|
2023-01-13 18:29:07 +00:00
|
|
|
img_ext = set_ext[img_ext]
|
2023-01-25 15:13:56 +00:00
|
|
|
img_icc = img.info.get(
|
|
|
|
"icc_profile") # Get ICC profile as it alters colours
|
|
|
|
|
2023-01-13 18:29:07 +00:00
|
|
|
# Resize image and orientate correctly
|
|
|
|
img.thumbnail((quality, quality), Image.LANCZOS)
|
|
|
|
img = ImageOps.exif_transpose(img)
|
2023-03-01 23:29:34 +00:00
|
|
|
try:
|
|
|
|
img.save(buff, img_ext, icc_profile=img_icc)
|
|
|
|
except OSError:
|
|
|
|
# This usually happens when saving a JPEG with an ICC profile
|
|
|
|
# Convert to RGB and try again
|
|
|
|
img = img.convert('RGB')
|
|
|
|
img.save(buff, img_ext, icc_profile=img_icc)
|
|
|
|
except:
|
|
|
|
logger.server(600, f"Error resizing image: {file}")
|
|
|
|
abort(500)
|
|
|
|
|
2023-01-17 22:13:52 +00:00
|
|
|
img.close()
|
2023-01-13 18:29:07 +00:00
|
|
|
|
|
|
|
# Seek to beginning of buffer and return
|
|
|
|
buff.seek(0)
|
2023-01-25 15:13:56 +00:00
|
|
|
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-25 15:13:56 +00:00
|
|
|
|
2023-03-01 23:29:34 +00:00
|
|
|
img_ext = os.path.splitext(secure_filename(form_file.filename))[-1].replace('.', '').lower()
|
|
|
|
img_name = f"GWAGWA_{uuid4().__str__()}.{img_ext}"
|
2023-01-11 19:46:31 +00:00
|
|
|
|
2023-03-01 23:29:34 +00:00
|
|
|
if not img_ext in current_app.config['ALLOWED_EXTENSIONS'].keys():
|
|
|
|
logger.add(303, f"File extension not allowed: {img_ext}")
|
2023-01-14 01:46:11 +00:00
|
|
|
abort(403)
|
2023-03-01 23:29:34 +00:00
|
|
|
|
|
|
|
if os.path.isdir(current_app.config['UPLOAD_FOLDER']) == False:
|
|
|
|
os.mkdir(current_app.config['UPLOAD_FOLDER'])
|
2023-01-25 15:13:56 +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 (?, ?, ?, ?)',
|
2023-01-25 15:13:56 +00:00
|
|
|
(img_name, g.user['id'], form['description'], form['alt']))
|
2023-01-11 19:46:31 +00:00
|
|
|
except Exception as e:
|
2023-03-01 23:29:34 +00:00
|
|
|
logger.server(600, f"Error saving to database: {e}")
|
2023-01-13 18:29:07 +00:00
|
|
|
abort(500)
|
2023-03-01 23:29:34 +00:00
|
|
|
|
2023-01-25 15:13:56 +00:00
|
|
|
# Save file
|
2023-01-13 18:29:07 +00:00
|
|
|
try:
|
2023-01-25 15:13:56 +00:00
|
|
|
form_file.save(
|
|
|
|
os.path.join(current_app.config['UPLOAD_FOLDER'], img_name))
|
2023-03-01 23:29:34 +00:00
|
|
|
db.commit()
|
|
|
|
except Exception as e:
|
|
|
|
logger.server(600, f"Error saving file: {e}")
|
2023-01-13 18:29:07 +00:00
|
|
|
abort(500)
|
2023-01-25 15:13:56 +00:00
|
|
|
|
2023-01-11 19:46:31 +00:00
|
|
|
return 'Gwa Gwa'
|
|
|
|
|
2023-01-25 15:13:56 +00:00
|
|
|
|
2023-01-11 19:46:31 +00:00
|
|
|
@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 = ?',
|
2023-01-25 15:13:56 +00:00
|
|
|
(id, )).fetchone()
|
2023-01-11 19:46:31 +00:00
|
|
|
|
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)
|
2023-01-25 15:13:56 +00:00
|
|
|
|
2023-01-11 19:46:31 +00:00
|
|
|
try:
|
2023-01-25 15:13:56 +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-03-01 23:29:34 +00:00
|
|
|
logger.server(600, f"Error removing file: {e}")
|
2023-01-13 18:29:07 +00:00
|
|
|
abort(500)
|
2023-01-25 15:13:56 +00:00
|
|
|
|
2023-01-11 19:46:31 +00:00
|
|
|
try:
|
|
|
|
db = get_db()
|
2023-01-25 15:13:56 +00:00
|
|
|
db.execute('DELETE FROM posts WHERE id = ?', (id, ))
|
2023-01-11 19:46:31 +00:00
|
|
|
db.commit()
|
|
|
|
except:
|
2023-03-01 23:29:34 +00:00
|
|
|
logger.server(600, f"Error removing from database: {e}")
|
2023-01-13 18:29:07 +00:00
|
|
|
abort(500)
|
2023-01-25 15:13:56 +00:00
|
|
|
|
2023-03-01 23:29:34 +00:00
|
|
|
logger.server(301, f"Removed image {id}")
|
2023-01-25 15:13:56 +00:00
|
|
|
flash(['Image was all in Le Head!', 1])
|
2023-01-31 17:32:22 +00:00
|
|
|
return 'Gwa Gwa'
|
|
|
|
|
2023-01-31 23:44:44 +00:00
|
|
|
|
2023-01-31 17:32:22 +00:00
|
|
|
@blueprint.route('/metadata/<int:id>', methods=['GET'])
|
|
|
|
def metadata(id):
|
|
|
|
img = get_db().execute(
|
|
|
|
'SELECT file_name, description, alt FROM posts WHERE id = ?',
|
|
|
|
(id, )).fetchone()
|
|
|
|
|
|
|
|
if img is None:
|
|
|
|
abort(404)
|
2023-01-31 23:44:44 +00:00
|
|
|
|
|
|
|
exif = mt.metadata.yoink(
|
|
|
|
os.path.join(current_app.config['UPLOAD_FOLDER'], img['file_name']))
|
2023-01-31 17:32:22 +00:00
|
|
|
|
2023-03-01 23:29:34 +00:00
|
|
|
return jsonify(exif)
|
|
|
|
|
|
|
|
@blueprint.route('/logfile')
|
|
|
|
@login_required
|
|
|
|
def logfile():
|
|
|
|
filename = logger.filename()
|
|
|
|
log_dict = {}
|
|
|
|
i = 0
|
|
|
|
|
|
|
|
with open(filename) as f:
|
|
|
|
for line in f:
|
|
|
|
line = line.split(' : ')
|
|
|
|
|
|
|
|
event = line[0].strip().split(' ')
|
|
|
|
event_data = {
|
|
|
|
'date': event[0],
|
|
|
|
'time': event[1],
|
|
|
|
'severity': event[2],
|
|
|
|
'owner': event[3]
|
|
|
|
}
|
|
|
|
|
|
|
|
message = line[1].strip()
|
|
|
|
try:
|
|
|
|
message_data = {
|
|
|
|
'code': int(message[1:4]),
|
|
|
|
'message': message[5:].strip()
|
|
|
|
}
|
|
|
|
except:
|
|
|
|
message_data = {
|
|
|
|
'code': 0,
|
|
|
|
'message': message
|
|
|
|
}
|
|
|
|
|
|
|
|
log_dict[i] = {
|
|
|
|
'event': event_data,
|
|
|
|
'message': message_data
|
|
|
|
}
|
|
|
|
|
|
|
|
i += 1 # Line number, starts at 0
|
|
|
|
|
|
|
|
return jsonify(log_dict)
|