2023-03-04 13:45:26 +00:00
|
|
|
"""
|
|
|
|
Onlylegs - API endpoints
|
2023-03-15 16:19:42 +00:00
|
|
|
Used internally by the frontend and possibly by other applications
|
2023-03-04 13:45:26 +00:00
|
|
|
"""
|
|
|
|
from uuid import uuid4
|
|
|
|
import os
|
2023-03-14 22:07:17 +00:00
|
|
|
import pathlib
|
2023-03-04 13:45:26 +00:00
|
|
|
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-12 14:52:24 +00:00
|
|
|
from flask import (Blueprint, send_from_directory, send_file,
|
|
|
|
abort, flash, jsonify, request, g, current_app)
|
2023-03-04 13:45:26 +00:00
|
|
|
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-01 23:29:34 +00:00
|
|
|
|
2023-03-12 14:52:24 +00:00
|
|
|
from sqlalchemy.orm import sessionmaker
|
2023-03-04 13:45:26 +00:00
|
|
|
from gallery.auth import login_required
|
2023-03-01 23:29:34 +00:00
|
|
|
|
2023-03-12 15:52:23 +00:00
|
|
|
from gallery import db
|
2023-03-14 22:07:17 +00:00
|
|
|
from gallery.utils 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-14 22:07:17 +00:00
|
|
|
@blueprint.route('/file/<file_name>', methods=['GET'])
|
|
|
|
def get_file(file_name):
|
2023-03-04 13:45:26 +00:00
|
|
|
"""
|
|
|
|
Returns a file from the uploads folder
|
2023-03-20 17:04:05 +00:00
|
|
|
r for resolution, 400x400 or thumb for thumbnail
|
2023-03-04 13:45:26 +00:00
|
|
|
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
|
2023-03-20 17:04:05 +00:00
|
|
|
res = request.args.get('r', default=None, type=str) # Type of file (thumb, etc)
|
2023-03-11 22:14:03 +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-15 16:19:42 +00:00
|
|
|
|
|
|
|
# Idea: instead if specifying the height and width, pass in a string like "200x200" or "200x" or "x200"
|
|
|
|
# This would remove the need for the if statements below and would be possible to pass in a string
|
|
|
|
# like 'thumb' to get the thumbnail size instead of having to specify the width and height
|
|
|
|
# This would also allow for more flexibility in the future if I wanted to add more sizes
|
|
|
|
# Another idea is to pass in a list of filters to apply to the image
|
|
|
|
# such as "blur,grayscale" or "blur,grayscale,sepia". But this would require a lot more work to implement
|
|
|
|
# and would be a lot more complicated to use, would also implement the risk of the server being overloaded
|
|
|
|
# with requests to apply a lot of filters to a lot of images at once
|
2023-03-02 13:19:10 +00:00
|
|
|
|
2023-03-14 22:07:17 +00:00
|
|
|
file_name = secure_filename(file_name) # Sanitize file name
|
|
|
|
|
2023-03-02 13:19:10 +00:00
|
|
|
# if no args are passed, return the raw file
|
2023-03-14 22:07:17 +00:00
|
|
|
if not request.args:
|
|
|
|
if not os.path.exists(os.path.join(current_app.config['UPLOAD_FOLDER'], file_name)):
|
2023-03-04 13:45:26 +00:00
|
|
|
abort(404)
|
2023-01-10 14:40:43 +00:00
|
|
|
|
2023-03-14 22:07:17 +00:00
|
|
|
return send_from_directory(current_app.config['UPLOAD_FOLDER'], file_name)
|
|
|
|
|
2023-03-20 17:04:05 +00:00
|
|
|
buff = io.BytesIO()
|
|
|
|
img = None # Image object to be set
|
2023-01-25 15:13:56 +00:00
|
|
|
|
2023-03-20 17:04:05 +00:00
|
|
|
try: # Open image and set extension
|
2023-03-14 22:07:17 +00:00
|
|
|
img = Image.open(os.path.join(current_app.config['UPLOAD_FOLDER'], file_name))
|
2023-03-20 17:04:05 +00:00
|
|
|
except FileNotFoundError: # FileNotFound is raised if the file doesn't exist
|
2023-03-14 22:07:17 +00:00
|
|
|
logging.error('File not found: %s', file_name)
|
2023-03-04 13:45:26 +00:00
|
|
|
abort(404)
|
2023-03-20 17:04:05 +00:00
|
|
|
except OSError as err: # OSError is raised if the file is broken or corrupted
|
2023-03-14 22:07:17 +00:00
|
|
|
logging.error('Possibly broken image %s, error: %s', file_name, err)
|
2023-03-01 23:29:34 +00:00
|
|
|
abort(500)
|
2023-03-02 13:19:10 +00:00
|
|
|
|
2023-03-14 22:07:17 +00:00
|
|
|
img_ext = pathlib.Path(file_name).suffix.replace('.', '').lower() # Get file extension
|
|
|
|
img_ext = current_app.config['ALLOWED_EXTENSIONS'][img_ext] # Convert to MIME type
|
|
|
|
img_icc = img.info.get("icc_profile") # Get ICC profile
|
2023-01-25 15:13:56 +00:00
|
|
|
|
2023-03-14 22:07:17 +00:00
|
|
|
img = ImageOps.exif_transpose(img) # Rotate image based on EXIF data
|
2023-03-04 13:45:26 +00:00
|
|
|
|
2023-03-20 17:04:05 +00:00
|
|
|
# Todo: If type is thumb(nail), return from database instead of file system
|
|
|
|
# as it's faster than generating a new thumbnail on every request
|
|
|
|
if res:
|
|
|
|
if res == 'thumb' or res == 'thumbnail':
|
|
|
|
width, height = 400, 400
|
|
|
|
elif res == 'prev' or res == 'preview':
|
|
|
|
width, height = 1920, 1080
|
|
|
|
else:
|
|
|
|
try:
|
|
|
|
width, height = res.split('x')
|
|
|
|
width = int(width)
|
|
|
|
height = int(height)
|
|
|
|
except ValueError:
|
|
|
|
abort(400)
|
|
|
|
|
|
|
|
img.thumbnail((width, height), Image.LANCZOS)
|
|
|
|
|
|
|
|
# Todo: If the image has a NSFW tag, blur image for example
|
|
|
|
# if filtered:
|
|
|
|
# pass
|
2023-03-14 22:07:17 +00:00
|
|
|
|
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:
|
2023-03-12 14:52:24 +00:00
|
|
|
# This usually happens when saving a JPEG with an ICC profile,
|
|
|
|
# so we convert to RGB and try again
|
2023-03-01 23:29:34 +00:00
|
|
|
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:
|
2023-03-14 22:07:17 +00:00
|
|
|
logging.error('Could not resize image %s, error: %s', file_name, err)
|
2023-03-01 23:29:34 +00:00
|
|
|
abort(500)
|
2023-03-02 13:19:10 +00:00
|
|
|
|
2023-03-14 22:07:17 +00:00
|
|
|
img.close() # Close image to free memory, learned the hard way
|
2023-03-12 14:52:24 +00:00
|
|
|
buff.seek(0) # Reset buffer to start
|
2023-03-14 22:07:17 +00:00
|
|
|
|
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-01-13 18:29:07 +00:00
|
|
|
|
2023-03-14 22:07:17 +00:00
|
|
|
# If no image is uploaded, return 404 error
|
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-14 22:07:17 +00:00
|
|
|
# Get file extension, generate random name and set file path
|
|
|
|
img_ext = pathlib.Path(form_file.filename).suffix.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-14 22:07:17 +00:00
|
|
|
# Check if file extension is allowed
|
2023-03-11 22:14:03 +00:00
|
|
|
if img_ext not 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-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-14 22:07:17 +00:00
|
|
|
img_exif = mt.Metadata(img_path).yoink() # Get EXIF data
|
|
|
|
img_colors = ColorThief(img_path).get_palette(color_count=3) # Get color palette
|
|
|
|
|
2023-03-05 16:22:11 +00:00
|
|
|
# Save to database
|
2023-03-14 22:07:17 +00:00
|
|
|
try:
|
2023-03-11 22:14:03 +00:00
|
|
|
query = db.Posts(author_id=g.user.id,
|
|
|
|
created_at=dt.utcnow(),
|
|
|
|
file_name=img_name+'.'+img_ext,
|
|
|
|
file_type=img_ext,
|
|
|
|
image_exif=img_exif,
|
|
|
|
image_colours=img_colors,
|
2023-03-14 22:07:17 +00:00
|
|
|
post_description=form['description'],
|
|
|
|
post_alt=form['alt'])
|
|
|
|
|
2023-03-05 16:22:11 +00:00
|
|
|
db_session.add(query)
|
|
|
|
db_session.commit()
|
|
|
|
except Exception as err:
|
|
|
|
logging.error('Could not save to database: %s', err)
|
|
|
|
abort(500)
|
|
|
|
|
2023-03-14 22:07:17 +00:00
|
|
|
return 'Gwa Gwa' # Return something so the browser doesn't show an error
|
2023-01-11 19:46:31 +00:00
|
|
|
|
2023-03-11 22:14:03 +00:00
|
|
|
|
2023-03-09 23:31:58 +00:00
|
|
|
@blueprint.route('/delete/<int:image_id>', methods=['POST'])
|
2023-01-11 19:46:31 +00:00
|
|
|
@login_required
|
2023-03-09 23:31:58 +00:00
|
|
|
def delete_image(image_id):
|
2023-03-04 13:45:26 +00:00
|
|
|
"""
|
|
|
|
Deletes an image from the server and database
|
|
|
|
"""
|
2023-03-09 23:31:58 +00:00
|
|
|
img = db_session.query(db.Posts).filter_by(id=image_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-09 23:31:58 +00:00
|
|
|
db_session.query(db.Posts).filter_by(id=image_id).delete()
|
2023-03-14 22:07:17 +00:00
|
|
|
|
2023-03-09 23:31:58 +00:00
|
|
|
groups = db_session.query(db.GroupJunction).filter_by(post_id=image_id).all()
|
|
|
|
for group in groups:
|
|
|
|
db_session.delete(group)
|
2023-03-14 22:07:17 +00:00
|
|
|
|
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-09 23:31:58 +00:00
|
|
|
logging.info('Removed image (%s) %s', image_id, img.file_name)
|
2023-03-12 18:53:57 +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-09 23:31:58 +00:00
|
|
|
@blueprint.route('/group/create', methods=['POST'])
|
|
|
|
@login_required
|
|
|
|
def create_group():
|
|
|
|
"""
|
|
|
|
Creates a group
|
|
|
|
"""
|
2023-03-12 14:52:24 +00:00
|
|
|
new_group = db.Groups(name=request.form['name'],
|
|
|
|
description=request.form['description'],
|
|
|
|
author_id=g.user.id,
|
2023-03-09 23:31:58 +00:00
|
|
|
created_at=dt.utcnow())
|
2023-03-14 22:07:17 +00:00
|
|
|
|
2023-03-09 23:31:58 +00:00
|
|
|
db_session.add(new_group)
|
|
|
|
db_session.commit()
|
2023-03-14 22:07:17 +00:00
|
|
|
|
2023-03-09 23:31:58 +00:00
|
|
|
return ':3'
|
|
|
|
|
|
|
|
|
|
|
|
@blueprint.route('/group/modify', methods=['POST'])
|
|
|
|
@login_required
|
|
|
|
def modify_group():
|
|
|
|
"""
|
|
|
|
Changes the images in a group
|
|
|
|
"""
|
2023-03-10 17:38:24 +00:00
|
|
|
group_id = request.form['group']
|
|
|
|
image_id = request.form['image']
|
2023-03-14 22:07:17 +00:00
|
|
|
|
2023-03-10 17:38:24 +00:00
|
|
|
group = db_session.query(db.Groups).filter_by(id=group_id).first()
|
|
|
|
|
|
|
|
if group is None:
|
|
|
|
abort(404)
|
|
|
|
elif group.author_id != g.user.id:
|
|
|
|
abort(403)
|
|
|
|
|
|
|
|
if request.form['action'] == 'add':
|
2023-03-09 23:31:58 +00:00
|
|
|
if db_session.query(db.GroupJunction).filter_by(group_id=group_id, post_id=image_id).first() is None:
|
|
|
|
db_session.add(db.GroupJunction(group_id=group_id, post_id=image_id, date_added=dt.utcnow()))
|
2023-03-10 17:38:24 +00:00
|
|
|
elif request.form['action'] == 'remove':
|
2023-03-09 23:31:58 +00:00
|
|
|
db_session.query(db.GroupJunction).filter_by(group_id=group_id, post_id=image_id).delete()
|
2023-03-14 22:07:17 +00:00
|
|
|
|
2023-03-10 17:38:24 +00:00
|
|
|
db_session.commit()
|
2023-03-14 22:07:17 +00:00
|
|
|
|
2023-03-09 23:31:58 +00:00
|
|
|
return ':3'
|
|
|
|
|
|
|
|
|
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
|
|
|
|
"""
|
2023-03-01 23:29:34 +00:00
|
|
|
log_dict = {}
|
2023-03-02 13:19:10 +00:00
|
|
|
|
2023-03-12 14:52:24 +00:00
|
|
|
with open('only.log', encoding='utf-8') as file:
|
2023-03-14 22:07:17 +00:00
|
|
|
for i, line in enumerate(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}
|
|
|
|
|
2023-03-04 13:45:26 +00:00
|
|
|
return jsonify(log_dict)
|