added image handling
This commit is contained in:
parent
9921cd7745
commit
be467e003d
|
@ -5,7 +5,7 @@ By popular demand, I'm building a better anonkun. It doesn't do much right now t
|
||||||
Python 3.6+
|
Python 3.6+
|
||||||
PostgreSQL 10.4+
|
PostgreSQL 10.4+
|
||||||
Redis 4.0.10+
|
Redis 4.0.10+
|
||||||
Python packages: `django psycopg2 channels channels_redis jinja2 argon2-cffi bleach`
|
Python packages: `django psycopg2 channels channels_redis jinja2 argon2-cffi bleach requests python-magic`
|
||||||
|
|
||||||
## Install
|
## Install
|
||||||
```
|
```
|
||||||
|
|
|
@ -29,12 +29,12 @@
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<ul id="header" class="header" style="{% if request.session.get("hide_header") == True %}display:none;{% else %}display:initial;{% endif %}">
|
<ul id="header" class="header" style="{% if request.session.get("hide_header") == True %}display:none;{% else %}display:initial;{% endif %}">
|
||||||
<li><a onclick="toggleHeader();" href="javascript:void(0);">⤒</a></li>
|
<li><a onclick="toggleHeader();" href="javascript:void(0);">⏫</a></li>
|
||||||
<li><a href="{{ url('homepage:index') }}">Home</a></li>
|
<li><a href="{{ url('homepage:index') }}">Home</a></li>
|
||||||
{% block header %}{% endblock %}
|
{% block header %}{% endblock %}
|
||||||
</ul>
|
</ul>
|
||||||
<ul id="headerHidden" class="header" style="{% if request.session.get("hide_header") == True %}display:initial;{% else %}display:none;{% endif %}">
|
<ul id="headerHidden" class="header" style="{% if request.session.get("hide_header") == True %}display:initial;{% else %}display:none;{% endif %}">
|
||||||
<li><a onclick="toggleHeader();" href="javascript:void(0);">⤓</a></li>
|
<li><a onclick="toggleHeader();" href="javascript:void(0);">⏬</a></li>
|
||||||
</ul>
|
</ul>
|
||||||
{#<br /> <!-- TODO: make this more exact. -->#}
|
{#<br /> <!-- TODO: make this more exact. -->#}
|
||||||
<div id="pageMessages">
|
<div id="pageMessages">
|
||||||
|
|
|
@ -15,6 +15,7 @@ from django.db import IntegrityError
|
||||||
from django.utils.timezone import localtime
|
from django.utils.timezone import localtime
|
||||||
|
|
||||||
from quest.models import *
|
from quest.models import *
|
||||||
|
from quest.tools import handle_img
|
||||||
from quest.forms import DiceCallForm, PollForm
|
from quest.forms import DiceCallForm, PollForm
|
||||||
|
|
||||||
def message(socket, data):
|
def message(socket, data):
|
||||||
|
@ -51,6 +52,7 @@ def message(socket, data):
|
||||||
message = message.replace(quote, msg)
|
message = message.replace(quote, msg)
|
||||||
|
|
||||||
# handle image
|
# handle image
|
||||||
|
message = handle_img(message)
|
||||||
|
|
||||||
# dice rolling
|
# dice rolling
|
||||||
if any(map(message.startswith, ["/dice", "/roll"])):
|
if any(map(message.startswith, ["/dice", "/roll"])):
|
||||||
|
|
|
@ -184,7 +184,7 @@ h3 {
|
||||||
}
|
}
|
||||||
|
|
||||||
#messageTextDiv {
|
#messageTextDiv {
|
||||||
padding-bottom: 10px;
|
padding-bottom: 1em;
|
||||||
width: 100%;
|
width: 100%;
|
||||||
display: flex;
|
display: flex;
|
||||||
flex-direction: column;
|
flex-direction: column;
|
||||||
|
@ -193,6 +193,7 @@ h3 {
|
||||||
#messageTextArea {
|
#messageTextArea {
|
||||||
resize: none;
|
resize: none;
|
||||||
box-sizing: border-box;
|
box-sizing: border-box;
|
||||||
|
height: 5em;
|
||||||
}
|
}
|
||||||
|
|
||||||
#preview {
|
#preview {
|
||||||
|
|
71
quest/tools.py
Normal file
71
quest/tools.py
Normal file
|
@ -0,0 +1,71 @@
|
||||||
|
#!/usr/bin/env python3
|
||||||
|
"""
|
||||||
|
Some miscellaneous tools and helper functions. Primarily for quests.
|
||||||
|
"""
|
||||||
|
import os
|
||||||
|
import re
|
||||||
|
import hashlib
|
||||||
|
|
||||||
|
import magic
|
||||||
|
import requests
|
||||||
|
from django.conf import settings
|
||||||
|
|
||||||
|
IMG_DIR = "/usr/local/www/html/img/"
|
||||||
|
ALLOWED_MIMES = [
|
||||||
|
"image/jpeg",
|
||||||
|
"image/png",
|
||||||
|
"image/gif",
|
||||||
|
"video/webm"
|
||||||
|
]
|
||||||
|
|
||||||
|
def download_img(url):
|
||||||
|
"""
|
||||||
|
Downloads the requested URL, ensures the mimetype is an acceptable
|
||||||
|
type, and saves it to file with the hash as filename. Returns a
|
||||||
|
URL to image.
|
||||||
|
"""
|
||||||
|
# TODO: file size limits
|
||||||
|
# https://stackoverflow.com/questions/22346158/
|
||||||
|
# TODO: prevent overwriting
|
||||||
|
try:
|
||||||
|
res = requests.get(url)
|
||||||
|
res.raise_for_status()
|
||||||
|
mime = magic.from_buffer(res.content, mime=True)
|
||||||
|
assert mime in ALLOWED_MIMES
|
||||||
|
h = hashlib.sha256()
|
||||||
|
h.update(res.content)
|
||||||
|
fname = h.hexdigest()
|
||||||
|
fname += "." + mime.partition("/")[2]
|
||||||
|
with open(os.path.join(IMG_DIR, fname), "wb") as file:
|
||||||
|
for chunk in res.iter_content(100000):
|
||||||
|
file.write(chunk)
|
||||||
|
return settings.IMG_SVR_URL + fname
|
||||||
|
except requests.exceptions.RequestException:
|
||||||
|
return "INVALID_URL"
|
||||||
|
except AssertionError:
|
||||||
|
return "INVALID_MIME_TYPE"
|
||||||
|
except Exception as e:
|
||||||
|
print(e)
|
||||||
|
return "UNKNOWN_ERROR"
|
||||||
|
|
||||||
|
|
||||||
|
def handle_img(text, limit=5):
|
||||||
|
"""
|
||||||
|
Finds all image urls in the given set of text and attempts to handle
|
||||||
|
them appropriately. `limit` will limit how many urls are processed.
|
||||||
|
The rest will be ignored. If an error occurs during handling, the raw
|
||||||
|
(unlinked) url will be inserted.
|
||||||
|
"""
|
||||||
|
# TODO: handle webms
|
||||||
|
urls = re.findall(r"\[img\](.*?)\[/img\]", text)
|
||||||
|
|
||||||
|
for ext_url in urls:
|
||||||
|
int_url = download_img(ext_url)
|
||||||
|
if int_url in ["INVALID_URL", "INVALID_MIME_TYPE", "UNKNOWN_ERROR"]:
|
||||||
|
text = text.replace("[img]" + ext_url + "[/img]", ext_url, 1)
|
||||||
|
alt_text = os.path.basename(ext_url)
|
||||||
|
img_tag = f'<img src="{int_url}" title="{alt_text}">'
|
||||||
|
|
||||||
|
text = text.replace("[img]" + ext_url + "[/img]", img_tag, 1)
|
||||||
|
|
||||||
|
return text
|
|
@ -156,3 +156,6 @@ CHANNEL_LAYERS = {
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# Image server url
|
||||||
|
IMG_SVR_URL = "https://img.steelbea.me/"
|
||||||
|
|
Loading…
Reference in New Issue
Block a user