Compare commits

..

No commits in common. "65703cfe2c317753ab838848503912a90d2c9263" and "c99a6ba24ec65208cd5dca356e164062183fccc0" have entirely different histories.

4 changed files with 18 additions and 47 deletions

15
LICENSE
View File

@ -1,15 +0,0 @@
ISC License
Copyright (c) 2019, iou1name <iou1name@steelbea.me>
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

View File

@ -18,9 +18,6 @@ h3 {
margin-right: auto; margin-right: auto;
} }
#content {
padding-left: 0;
}
#questPane { #questPane {
float: left; float: left;
box-sizing: border-box; box-sizing: border-box;

View File

@ -5,7 +5,6 @@ Some miscellaneous tools and helper functions. Primarily for quests.
import os import os
import re import re
import json import json
import time
import hashlib import hashlib
import magic import magic
@ -28,40 +27,32 @@ def download_img(url):
type, and saves it to file with the hash as filename. Returns a type, and saves it to file with the hash as filename. Returns a
URL to image. URL to image.
""" """
# TODO: external server # TODO: file size limits
timeout = 10 # TODO: put in settings # https://stackoverflow.com/questions/22346158/
url = url.replace('..', '') # TODO: why is this here? # TODO: prevent overwriting
url = url.replace('..', '')
if url.startswith(settings.IMG_SVR_URL): if url.startswith(settings.IMG_SVR_URL):
if '/' not in url.replace(settings.IMG_SVR_URL, ''): if '/' not in url.replace(settings.IMG_SVR_URL, ''):
return url return url
try: try:
with requests.get(url, stream=True) as r: res = requests.get(url)
r.raise_for_status() res.raise_for_status()
data = b'' mime = magic.from_buffer(res.content, mime=True)
start_time = time.time()
for chunk in r.iter_content(102400):
if time.time() - start_time > timeout:
raise ValueError('TIMEOUT_REACHED')
data += chunk
if len(data) > 4*1024*1024: # TODO: put in settings
raise ValueError('RESPONSE_TOO_LARGE')
mime = magic.from_buffer(data, mime=True)
assert mime in ALLOWED_MIMES assert mime in ALLOWED_MIMES
h = hashlib.sha256() h = hashlib.sha256()
h.update(data) h.update(res.content)
fname = h.hexdigest() fname = h.hexdigest()
fname += "." + mime.partition("/")[2] fname += "." + mime.partition("/")[2]
with open(os.path.join(IMG_DIR, fname), "wb") as file: with open(os.path.join(IMG_DIR, fname), "wb") as file:
file.write(data) for chunk in res.iter_content(100000):
file.write(chunk)
return settings.IMG_SVR_URL + fname return settings.IMG_SVR_URL + fname
except requests.exceptions.RequestException: except requests.exceptions.RequestException:
return "INVALID_URL" return "INVALID_URL"
except AssertionError: except AssertionError:
return "INVALID_MIME_TYPE" return "INVALID_MIME_TYPE"
except ValueError as e:
return str(e)
except Exception as e: except Exception as e:
print(e) # TODO: log this print(e)
return "UNKNOWN_ERROR" return "UNKNOWN_ERROR"
@ -75,19 +66,18 @@ def handle_img(text, limit=5):
# TODO: handle webms # TODO: handle webms
urls = re.findall( urls = re.findall(
r"""\[img(?: title=['"](.*)['"])?\](.*)\[\/img\]""", r"""\[img(?: title=['"](.*)['"])?\](.*)\[\/img\]""",
text.replace('<br>', '\n') text.replace('<br', '\n')
) )
urls = urls[:limit] urls = urls[:limit]
for match_pair in urls: for match_pair in urls:
title, external_url = match_pair title, ext_url = match_pair
internal_url = download_img(external_url) int_url = download_img(ext_url)
if not internal_url.startswith("http"): # download errored if int_url in ["INVALID_URL", "INVALID_MIME_TYPE", "UNKNOWN_ERROR"]:
# TODO: error message? text = re.sub(r"\[img.*?\[\/img\]", ext_url, text, 1)
text = re.sub(r"\[img.*?\[\/img\]", external_url, text, 1)
if not title: if not title:
title = os.path.basename(external_url) title = os.path.basename(ext_url)
img_tag = f'<img src="{internal_url}" title="{title}">' img_tag = f'<img src="{int_url}" title="{title}">'
text = re.sub(r"\[img.*?\[\/img\]", img_tag, text, 1) text = re.sub(r"\[img.*?\[\/img\]", img_tag, text, 1)

View File

@ -100,5 +100,4 @@ a:hover {
flex: auto; flex: auto;
overflow: auto; overflow: auto;
overflow-wrap: break-word; overflow-wrap: break-word;
padding-left: 5%;
} }