2023-03-04 13:45:26 +00:00
|
|
|
"""
|
|
|
|
Onlylegs - API endpoints
|
|
|
|
Used intermally by the frontend and possibly by other applications
|
|
|
|
"""
|
|
|
|
from uuid import uuid4
|
|
|
|
import os
|
|
|
|
import io
|
|
|
|
import logging
|
2023-03-05 16:22:11 +00:00
|
|
|
from datetime import datetime as dt
|
2023-03-01 23:29:34 +00:00
|
|
|
|
2023-03-04 13:45:26 +00:00
|
|
|
from flask import (
|
|
|
|
Blueprint, current_app, send_from_directory, send_file, request, g, abort, flash, jsonify)
|
|
|
|
from werkzeug.utils import secure_filename
|
2023-03-03 00:26:46 +00:00
|
|
|
|
2023-03-05 16:22:11 +00:00
|
|
|
from colorthief import ColorThief
|
2023-03-08 09:01:20 +00:00
|
|
|
from PIL import Image, ImageOps, ImageFilter
|
2023-03-03 00:26:46 +00:00
|
|
|
from sqlalchemy.orm import sessionmaker
|
2023-03-01 23:29:34 +00:00
|
|
|
|
2023-03-04 13:45:26 +00:00
|
|
|
from gallery.auth import login_required
|
2023-03-01 23:29:34 +00:00
|
|
|
|
2023-03-04 13:45:26 +00:00
|
|
|
from . import db # Import db to create a session
|
|
|
|
from . import metadata as mt
|
2023-03-01 23:29:34 +00:00
|
|
|
|
2023-01-10 14:40:43 +00:00
|
|
|
|
2023-03-01 23:29:34 +00:00
|
|
|
blueprint = Blueprint('api', __name__, url_prefix='/api')
|
2023-03-04 13:45:26 +00:00
|
|
|
db_session = sessionmaker(bind=db.engine)
|
|
|
|
db_session = db_session()
|
2023-01-10 14:40:43 +00:00
|
|
|
|
|
|
|
|
2023-03-02 13:19:10 +00:00
|
|
|
@blueprint.route('/uploads/<file>', methods=['GET'])
|
|
|
|
def uploads(file):
|
2023-03-04 13:45:26 +00:00
|
|
|
"""
|
|
|
|
Returns a file from the uploads folder
|
|
|
|
w and h are the width and height of the image for resizing
|
|
|
|
f is whether to apply filters to the image, such as blurring NSFW images
|
2023-03-08 09:01:20 +00:00
|
|
|
b is whether to force blur the image, even if it's not NSFW
|
2023-03-04 13:45:26 +00:00
|
|
|
"""
|
2023-03-02 13:19:10 +00:00
|
|
|
# Get args
|
|
|
|
width = request.args.get('w', default=0, type=int) # Width of image
|
|
|
|
height = request.args.get('h', default=0, type=int) # Height of image
|
2023-03-08 09:01:20 +00:00
|
|
|
filtered = request.args.get('f', default=False, type=bool) # Whether to apply filters
|
|
|
|
blur = request.args.get('b', default=False, type=bool) # Whether to force blur
|
2023-03-02 13:19:10 +00:00
|
|
|
|
|
|
|
# if no args are passed, return the raw file
|
|
|
|
if width == 0 and height == 0 and not filtered:
|
2023-03-04 13:45:26 +00:00
|
|
|
if not os.path.exists(os.path.join(current_app.config['UPLOAD_FOLDER'],
|
|
|
|
secure_filename(file))):
|
|
|
|
abort(404)
|
|
|
|
return send_from_directory(current_app.config['UPLOAD_FOLDER'], file ,as_attachment=True)
|
2023-01-10 14:40:43 +00:00
|
|
|
|
2023-03-02 13:19:10 +00:00
|
|
|
# Of either width or height is 0, set it to the other value to keep aspect ratio
|
|
|
|
if width > 0 and height == 0:
|
|
|
|
height = width
|
|
|
|
elif width == 0 and height > 0:
|
|
|
|
width = height
|
|
|
|
|
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:
|
2023-03-04 13:45:26 +00:00
|
|
|
img = Image.open(os.path.join(current_app.config['UPLOAD_FOLDER'],file))
|
|
|
|
except FileNotFoundError:
|
|
|
|
logging.error('File not found: %s, possibly broken upload', file)
|
|
|
|
abort(404)
|
|
|
|
except Exception as err:
|
|
|
|
logging.error('Error opening image: %s', err)
|
2023-03-01 23:29:34 +00:00
|
|
|
abort(500)
|
2023-03-02 13:19:10 +00:00
|
|
|
|
2023-03-04 13:45:26 +00:00
|
|
|
img_ext = os.path.splitext(file)[-1].lower().replace('.', '')
|
2023-01-13 18:29:07 +00:00
|
|
|
img_ext = set_ext[img_ext]
|
2023-03-04 13:45:26 +00:00
|
|
|
# Get ICC profile as it alters colours when saving
|
|
|
|
img_icc = img.info.get("icc_profile")
|
2023-01-25 15:13:56 +00:00
|
|
|
|
2023-01-13 18:29:07 +00:00
|
|
|
# Resize image and orientate correctly
|
2023-03-02 13:19:10 +00:00
|
|
|
img.thumbnail((width, height), Image.LANCZOS)
|
2023-01-13 18:29:07 +00:00
|
|
|
img = ImageOps.exif_transpose(img)
|
2023-03-04 13:45:26 +00:00
|
|
|
|
2023-03-02 13:19:10 +00:00
|
|
|
# If has NSFW tag, blur image, etc.
|
|
|
|
if filtered:
|
2023-03-04 13:45:26 +00:00
|
|
|
#img = img.filter(ImageFilter.GaussianBlur(20))
|
|
|
|
pass
|
2023-03-08 09:01:20 +00:00
|
|
|
|
|
|
|
# If forced to blur, blur image
|
|
|
|
if blur:
|
|
|
|
img = img.filter(ImageFilter.GaussianBlur(20))
|
2023-03-04 13:45:26 +00:00
|
|
|
|
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)
|
2023-03-04 13:45:26 +00:00
|
|
|
except Exception as err:
|
|
|
|
logging.error('Could not resize image %s, error: %s', file, err)
|
2023-03-01 23:29:34 +00:00
|
|
|
abort(500)
|
2023-03-02 13:19:10 +00:00
|
|
|
|
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-03-04 13:45:26 +00:00
|
|
|
"""
|
|
|
|
Uploads an image to the server and saves it to the database
|
|
|
|
"""
|
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-03-05 16:22:11 +00:00
|
|
|
form_description = form['description']
|
|
|
|
form_alt = form['alt']
|
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-04 13:45:26 +00:00
|
|
|
img_ext = os.path.splitext(form_file.filename)[-1].replace('.', '').lower()
|
2023-03-05 16:22:11 +00:00
|
|
|
img_name = "GWAGWA_"+str(uuid4())
|
|
|
|
img_path = os.path.join(current_app.config['UPLOAD_FOLDER'], img_name+'.'+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():
|
2023-03-04 13:45:26 +00:00
|
|
|
logging.info('File extension not allowed: %s', img_ext)
|
2023-01-14 01:46:11 +00:00
|
|
|
abort(403)
|
2023-03-02 13:19:10 +00:00
|
|
|
|
2023-03-04 13:45:26 +00:00
|
|
|
if os.path.isdir(current_app.config['UPLOAD_FOLDER']) is False:
|
2023-03-01 23:29:34 +00:00
|
|
|
os.mkdir(current_app.config['UPLOAD_FOLDER'])
|
2023-01-25 15:13:56 +00:00
|
|
|
|
2023-03-02 13:19:10 +00:00
|
|
|
|
2023-01-25 15:13:56 +00:00
|
|
|
# Save file
|
2023-01-13 18:29:07 +00:00
|
|
|
try:
|
2023-03-05 16:22:11 +00:00
|
|
|
form_file.save(img_path)
|
2023-03-04 13:45:26 +00:00
|
|
|
except Exception as err:
|
|
|
|
logging.error('Could not save file: %s', err)
|
2023-01-13 18:29:07 +00:00
|
|
|
abort(500)
|
2023-01-25 15:13:56 +00:00
|
|
|
|
2023-03-05 16:22:11 +00:00
|
|
|
# Get metadata and colors
|
|
|
|
img_exif = mt.Metadata(img_path).yoink()
|
|
|
|
img_colors = ColorThief(img_path).get_palette(color_count=3)
|
2023-03-05 18:16:28 +00:00
|
|
|
|
2023-03-05 16:22:11 +00:00
|
|
|
# Save to database
|
|
|
|
try:
|
|
|
|
query = db.Posts(author_id = g.user.id,
|
2023-03-08 21:58:58 +00:00
|
|
|
created_at = dt.utcnow(),
|
2023-03-05 16:22:11 +00:00
|
|
|
file_name = img_name+'.'+img_ext,
|
|
|
|
file_type = img_ext,
|
|
|
|
image_exif = img_exif,
|
|
|
|
image_colours = img_colors,
|
|
|
|
post_description = form_description,
|
|
|
|
post_alt = form_alt)
|
|
|
|
|
|
|
|
db_session.add(query)
|
|
|
|
db_session.commit()
|
|
|
|
except Exception as err:
|
|
|
|
logging.error('Could not save to database: %s', err)
|
|
|
|
abort(500)
|
|
|
|
|
2023-01-11 19:46:31 +00:00
|
|
|
return 'Gwa Gwa'
|
|
|
|
|
2023-01-25 15:13:56 +00:00
|
|
|
|
2023-03-04 13:45:26 +00:00
|
|
|
@blueprint.route('/remove/<int:img_id>', methods=['POST'])
|
2023-01-11 19:46:31 +00:00
|
|
|
@login_required
|
2023-03-04 13:45:26 +00:00
|
|
|
def remove(img_id):
|
|
|
|
"""
|
|
|
|
Deletes an image from the server and database
|
|
|
|
"""
|
2023-03-05 16:22:11 +00:00
|
|
|
img = db_session.query(db.Posts).filter_by(id=img_id).first()
|
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-03-03 00:26:46 +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-03-04 13:45:26 +00:00
|
|
|
os.remove(os.path.join(current_app.config['UPLOAD_FOLDER'],img.file_name))
|
|
|
|
except FileNotFoundError:
|
|
|
|
# File was already deleted or doesn't exist
|
|
|
|
logging.warning('File not found: %s, already deleted or never existed', img.file_name)
|
|
|
|
except Exception as err:
|
|
|
|
logging.error('Could not remove file: %s', err)
|
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:
|
2023-03-05 16:22:11 +00:00
|
|
|
db_session.query(db.Posts).filter_by(id=img_id).delete()
|
2023-03-03 00:26:46 +00:00
|
|
|
db_session.commit()
|
2023-03-04 13:45:26 +00:00
|
|
|
except Exception as err:
|
|
|
|
logging.error('Could not remove from database: %s', err)
|
2023-01-13 18:29:07 +00:00
|
|
|
abort(500)
|
2023-01-25 15:13:56 +00:00
|
|
|
|
2023-03-04 13:45:26 +00:00
|
|
|
logging.info('Removed image (%s) %s', img_id, img.file_name)
|
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-03-04 13:45:26 +00:00
|
|
|
@blueprint.route('/metadata/<int:img_id>', methods=['GET'])
|
|
|
|
def metadata(img_id):
|
|
|
|
"""
|
|
|
|
Yoinks metadata from an image
|
|
|
|
"""
|
2023-03-05 16:22:11 +00:00
|
|
|
img = db_session.query(db.Posts).filter_by(id=img_id).first()
|
2023-01-31 17:32:22 +00:00
|
|
|
|
|
|
|
if img is None:
|
|
|
|
abort(404)
|
2023-01-31 23:44:44 +00:00
|
|
|
|
2023-03-04 21:08:42 +00:00
|
|
|
img_path = os.path.join(current_app.config['UPLOAD_FOLDER'], img.file_name)
|
|
|
|
exif = mt.Metadata(img_path).yoink()
|
2023-01-31 17:32:22 +00:00
|
|
|
|
2023-03-01 23:29:34 +00:00
|
|
|
return jsonify(exif)
|
|
|
|
|
2023-03-02 13:19:10 +00:00
|
|
|
|
2023-03-01 23:29:34 +00:00
|
|
|
@blueprint.route('/logfile')
|
|
|
|
@login_required
|
|
|
|
def logfile():
|
2023-03-04 13:45:26 +00:00
|
|
|
"""
|
|
|
|
Gets the log file and returns it as a JSON object
|
|
|
|
"""
|
|
|
|
filename = logging.getLoggerClass().root.handlers[0].baseFilename
|
2023-03-01 23:29:34 +00:00
|
|
|
log_dict = {}
|
|
|
|
i = 0
|
2023-03-02 13:19:10 +00:00
|
|
|
|
2023-03-04 13:45:26 +00:00
|
|
|
with open(filename, encoding='utf-8') as file:
|
|
|
|
for line in file:
|
2023-03-01 23:29:34 +00:00
|
|
|
line = line.split(' : ')
|
2023-03-02 13:19:10 +00:00
|
|
|
|
2023-03-01 23:29:34 +00:00
|
|
|
event = line[0].strip().split(' ')
|
|
|
|
event_data = {
|
|
|
|
'date': event[0],
|
|
|
|
'time': event[1],
|
|
|
|
'severity': event[2],
|
|
|
|
'owner': event[3]
|
|
|
|
}
|
2023-03-02 13:19:10 +00:00
|
|
|
|
2023-03-01 23:29:34 +00:00
|
|
|
message = line[1].strip()
|
|
|
|
try:
|
|
|
|
message_data = {
|
|
|
|
'code': int(message[1:4]),
|
|
|
|
'message': message[5:].strip()
|
|
|
|
}
|
2023-03-04 13:45:26 +00:00
|
|
|
except ValueError:
|
2023-03-02 13:19:10 +00:00
|
|
|
message_data = {'code': 0, 'message': message}
|
2023-03-04 13:45:26 +00:00
|
|
|
except Exception as err:
|
|
|
|
logging.error('Could not parse log file: %s', err)
|
|
|
|
abort(500)
|
2023-03-02 13:19:10 +00:00
|
|
|
|
|
|
|
log_dict[i] = {'event': event_data, 'message': message_data}
|
|
|
|
|
|
|
|
i += 1 # Line number, starts at 0
|
|
|
|
|
2023-03-04 13:45:26 +00:00
|
|
|
return jsonify(log_dict)
|