diff --git a/files/helpers/const.py b/files/helpers/const.py index b8769399c7..4a348723d2 100644 --- a/files/helpers/const.py +++ b/files/helpers/const.py @@ -797,7 +797,4 @@ NOTIFIED_USERS = { 'landlet': LLM_ID, 'dong': DONGER_ID, 'kippy': KIPPY_ID, -} - -num_banners = len(listdir('files/assets/images/Drama/banners')) + 1 -num_sidebar = len(listdir('files/assets/images/Drama/sidebar')) + 1 \ No newline at end of file +} \ No newline at end of file diff --git a/files/helpers/images.py b/files/helpers/images.py index 1caa0969ea..f99485331b 100644 --- a/files/helpers/images.py +++ b/files/helpers/images.py @@ -1,12 +1,14 @@ from PIL import Image as IImage, ImageSequence from webptools import gifwebp +import time -def process_image(filename=None, resize=False): +def process_image(file=None, filename=None, resize=False): - i = IImage.open(filename) + i = IImage.open(file) + if not filename: filename = f'/images/{time.time()}'.replace('.','')[:-5] + '.webp' if resize: - size = 100, 100 + size = resize, resize frames = ImageSequence.Iterator(i) def thumbnails(frames): @@ -21,7 +23,9 @@ def process_image(filename=None, resize=False): om.info = i.info om.save(filename, format="WEBP", save_all=True, append_images=list(frames), loop=0, method=6, allow_mixed=True) elif i.format.lower() != "webp": - if i.format.lower() == "gif": gifwebp(input_image=filename, output_image=filename, option="-mixed -metadata none -f 100 -mt -m 6") + if i.format.lower() == "gif": + i.save(filename) + gifwebp(input_image=filename, output_image=filename, option="-mixed -metadata none -f 100 -mt -m 6") else: i.save(filename, format="WEBP", method=6) return f'/static{filename}' \ No newline at end of file diff --git a/files/helpers/jinja2.py b/files/helpers/jinja2.py index 1a12539821..a8e58a4706 100644 --- a/files/helpers/jinja2.py +++ b/files/helpers/jinja2.py @@ -24,4 +24,4 @@ def post_embed(id, v): @app.context_processor def inject_constants(): - return {"num_banners":num_banners, "num_sidebar":num_sidebar, "environ":environ, "SITE_NAME":SITE_NAME, "AUTOJANNY_ID":AUTOJANNY_ID, "NOTIFICATIONS_ID":NOTIFICATIONS_ID, "PUSHER_ID":PUSHER_ID, "CC":CC, "CC_TITLE":CC_TITLE} + return {"environ":environ, "SITE_NAME":SITE_NAME, "AUTOJANNY_ID":AUTOJANNY_ID, "NOTIFICATIONS_ID":NOTIFICATIONS_ID, "PUSHER_ID":PUSHER_ID, "CC":CC, "CC_TITLE":CC_TITLE, "listdir":listdir} diff --git a/files/routes/comments.py b/files/routes/comments.py index e2bb0e51b2..4c4db6de6a 100644 --- a/files/routes/comments.py +++ b/files/routes/comments.py @@ -174,10 +174,7 @@ def api_comment(v): if request.files.get("file") and request.headers.get("cf-ipcountry") != "T1": file=request.files["file"] if file.content_type.startswith('image/'): - name = f'/images/{time.time()}'.replace('.','')[:-5] + '.webp' - file.save(name) - url = process_image(name) - body += f"\n\n![]({url})" + body += f"\n\n![]({process_image(file.stream)})" elif file.content_type.startswith('video/'): file.save("video.mp4") with open("video.mp4", 'rb') as f: @@ -640,10 +637,15 @@ def edit_comment(cid, v): if request.files.get("file") and request.headers.get("cf-ipcountry") != "T1": file=request.files["file"] if file.content_type.startswith('image/'): - name = f'/images/{time.time()}'.replace('.','')[:-5] + '.webp' - file.save(name) - url = process_image(name) - body += f"\n\n![]({url})" + body += f"\n\n![]({process_image(file.stream)})" + if v.admin_level == 3: + if parent_post.id == 48: + filename = 'files/assets/images/Drama/sidebar/' + str(len(listdir('files/assets/images/Drama/sidebar'))+1) + '.webp' + process_image(file.stream, filename, 400) + elif parent_post.id == 49: + filename = 'files/assets/images/Drama/sidebar/' + str(len(listdir('files/assets/images/Drama/banners'))+1) + '.webp' + process_image(file.stream, filename, 400) + elif file.content_type.startswith('video/'): file.save("video.mp4") with open("video.mp4", 'rb') as f: diff --git a/files/routes/posts.py b/files/routes/posts.py index b50cdc83b7..ae4e3e49ef 100644 --- a/files/routes/posts.py +++ b/files/routes/posts.py @@ -16,8 +16,6 @@ from .front import frontlist, changeloglist from urllib.parse import ParseResult, urlunparse, urlparse, quote, unquote from os import path import requests -from shutil import copyfile -from psutil import cpu_percent IMGUR_KEY = environ.get("IMGUR_KEY").strip() @@ -421,10 +419,7 @@ def edit_post(pid, v): if request.files.get("file") and request.headers.get("cf-ipcountry") != "T1": file=request.files["file"] if file.content_type.startswith('image/'): - name = f'/images/{time.time()}'.replace('.','')[:-5] + '.webp' - file.save(name) - url = process_image(name) - body += f"\n\n![]({url})" + body += f"\n\n![]({process_image(file.stream)})" elif file.content_type.startswith('video/'): file.save("video.mp4") with open("video.mp4", 'rb') as f: @@ -530,29 +525,6 @@ def thumbnail_thread(pid): db = db_session() - # cpu = cpu_percent() - - # time.sleep(0.1) - - # cpu = cpu_percent() - - # if cpu > 70: - # print(f'Cpu usage at {cpu}%, Enabling under attack mode!', flush=True) - # with open('under_attack', 'r') as f: content = f.read() - - # with open('under_attack', 'w') as f: - # if content == "no": - # f.write("yes") - # ma = ModAction( - # kind="enable_under_attack", - # user_id=AUTOJANNY_ID, - # ) - # db.add(ma) - # db.commit() - # data='{"value":"under_attack"}' - # response = requests.patch(f'https://api.cloudflare.com/client/v4/zones/{CF_ZONE}/settings/security_level', headers=CF_HEADERS, data=data) - # print(response.text, flush=True) - def expand_url(post_url, fragment_url): if fragment_url.startswith("https://"): @@ -646,10 +618,11 @@ def thumbnail_thread(pid): if image_req.headers.get("Content-Type","").startswith("image/svg"): continue - image = PILimage.open(BytesIO(image_req.content)) - if image.width < 30 or image.height < 30: + image = BytesIO(image_req.content) + image2 = PILimage.open(image) + if image2.width < 30 or image2.height < 30: continue - + break else: @@ -660,7 +633,7 @@ def thumbnail_thread(pid): elif x.headers.get("Content-Type","").startswith("image/"): image_req=x - image = PILimage.open(BytesIO(x.content)) + image = BytesIO(x.content) else: db.close() @@ -668,11 +641,7 @@ def thumbnail_thread(pid): name = f'/images/{time.time()}'.replace('.','')[:-5] + '.webp' - with open(name, "wb") as file: - for chunk in image_req.iter_content(1024): - file.write(chunk) - - post.thumburl = process_image(name, resize=True) + post.thumburl = process_image(image, resize=100) db.add(post) db.commit() db.close() @@ -879,9 +848,7 @@ def submit_post(v): if request.files.get("file2") and request.headers.get("cf-ipcountry") != "T1": file=request.files["file2"] if file.content_type.startswith('image/'): - name = f'/images/{time.time()}'.replace('.','')[:-5] + '.webp' - file.save(name) - body += f"\n\n![]({process_image(name)})" + body += f"\n\n![]({process_image(file.stream)})" elif file.content_type.startswith('video/'): file.save("video.mp4") with open("video.mp4", 'rb') as f: @@ -968,13 +935,8 @@ def submit_post(v): file = request.files['file'] if file.content_type.startswith('image/'): - name = f'/images/{time.time()}'.replace('.','')[:-5] + '.webp' - file.save(name) - new_post.url = process_image(name) - - name2 = name.replace('.webp', 'r.webp') - copyfile(name, name2) - new_post.thumburl = process_image(name2, resize=True) + new_post.url = process_image(file.stream) + new_post.thumburl = process_image(file.stream, resize=100) elif file.content_type.startswith('video/'): file.save("video.mp4") with open("video.mp4", 'rb') as f: diff --git a/files/routes/settings.py b/files/routes/settings.py index d81b5b9551..6983290a90 100644 --- a/files/routes/settings.py +++ b/files/routes/settings.py @@ -11,7 +11,6 @@ from .front import frontlist import os from files.helpers.sanitize import filter_emojis_only from files.helpers.discord import add_role -from shutil import copyfile import requests valid_username_regex = re.compile("^[a-zA-Z0-9_\-]{3,25}$") @@ -256,10 +255,7 @@ def settings_profile_post(v): if request.files.get('file'): file = request.files['file'] if file.content_type.startswith('image/'): - name = f'/images/{time.time()}'.replace('.','')[:-5] + '.webp' - file.save(name) - url = process_image(name) - bio += f"\n\n![]({url})" + bio += f"\n\n![]({process_image(file.stream)})" elif file.content_type.startswith('video/'): file.save("video.mp4") with open("video.mp4", 'rb') as f: @@ -625,15 +621,11 @@ def settings_images_profile(v): file = request.files["profile"] - name = f'/images/{time.time()}'.replace('.','')[:-5] + '.webp' - file.save(name) - highres = process_image(name) + highres = process_image(file.stream) if not highres: abort(400) - name2 = name.replace('.webp', 'r.webp') - copyfile(name, name2) - imageurl = process_image(name2, resize=True) + imageurl = process_image(file.stream, resize=100) if not imageurl: abort(400) @@ -660,9 +652,7 @@ def settings_images_banner(v): file = request.files["banner"] - name = f'/images/{time.time()}'.replace('.','')[:-5] + '.webp' - file.save(name) - bannerurl = process_image(name) + bannerurl = process_image(file.stream) if bannerurl: if v.bannerurl and '/images/' in v.bannerurl : os.remove('/images/' + v.bannerurl.split('/images/')[1]) diff --git a/files/routes/static.py b/files/routes/static.py index 2f78698fab..c50e022b09 100644 --- a/files/routes/static.py +++ b/files/routes/static.py @@ -271,10 +271,7 @@ def submit_contact(v): if request.files.get("file") and request.headers.get("cf-ipcountry") != "T1": file=request.files["file"] if file.content_type.startswith('image/'): - name = f'/images/{time.time()}'.replace('.','')[:-5] + '.webp' - file.save(name) - url = process_image(name) - body_html += f'' + body_html += f'' elif file.content_type.startswith('video/'): file.save("video.mp4") with open("video.mp4", 'rb') as f: diff --git a/files/templates/default.html b/files/templates/default.html index 7fbb43ff3f..b3ab015c4e 100644 --- a/files/templates/default.html +++ b/files/templates/default.html @@ -217,8 +217,11 @@ {% if v %} {% if SITE_NAME == 'Drama' %} + {% set path = "assets/images/" + SITE_NAME + "/banners" %} + {% set image = "/static/" + path + "/" + listdir('files/' + path)|random() %} + - site banner + site banner {% else %} diff --git a/files/templates/sidebar_Drama.html b/files/templates/sidebar_Drama.html index cbab732b8e..16735ed51d 100644 --- a/files/templates/sidebar_Drama.html +++ b/files/templates/sidebar_Drama.html @@ -1,6 +1,7 @@