diff --git a/files/helpers/images.py b/files/helpers/images.py index 63f737f96..37c1914be 100644 --- a/files/helpers/images.py +++ b/files/helpers/images.py @@ -8,9 +8,46 @@ from flask import g CF_KEY = environ.get("CLOUDFLARE_KEY", "").strip() CF_ZONE = environ.get("CLOUDFLARE_ZONE", "").strip() IMGUR_KEY = environ.get("IMGUR_KEY", "").strip() +IBB_KEY = environ.get("IBB_KEY", "").strip() + +def upload_ibb(file=None, resize=False, png=False): + + if file: file.save("image.gif") + + if resize: + i = IImage.open("image.gif") + size = 100, 100 + frames = ImageSequence.Iterator(i) + + def thumbnails(frames): + for frame in frames: + thumbnail = frame.copy() + thumbnail.thumbnail(size, IImage.ANTIALIAS) + yield thumbnail + + frames = thumbnails(frames) + + om = next(frames) + om.info = i.info + try: om.save("image.gif", save_all=True, append_images=list(frames), loop=0) + except: return + + if png: filedir = "image.png" + else: filedir = "image.gif" + try: + with open(filedir, 'rb') as f: + data={'image': base64.b64encode(f.read())} + req = requests.post(f'https://api.imgbb.com/1/upload?key={IBB_KEY}', data=data) + resp = req.json()['data'] + url = resp['url'] + except: + if req: print(req.json()) + return + + return(url) -def upload_file(file=None, resize=False, png=False): +def upload_imgur(file=None, resize=False, png=False): if file: file.save("image.gif") diff --git a/files/routes/admin.py b/files/routes/admin.py index 0df152700..d38ec8904 100644 --- a/files/routes/admin.py +++ b/files/routes/admin.py @@ -1189,4 +1189,4 @@ def multiple_plots(**kwargs): plt.savefig("image.png") plt.clf() - return upload_file(png=True) + return upload_imgur(png=True) \ No newline at end of file diff --git a/files/routes/comments.py b/files/routes/comments.py index 8fed20d1b..515429a87 100644 --- a/files/routes/comments.py +++ b/files/routes/comments.py @@ -276,7 +276,8 @@ def api_comment(v): file=request.files["file"] if not file.content_type.startswith('image/'): return {"error": "That wasn't an image!"}, 400 - url = upload_file(file) + if 'pcm' in request.host: url = upload_ibb(file) + else: url = upload_imgur(file) body = request.form.get("body") + f"\n![]({url})" body = body.replace("\n", "\n\n").replace("\n\n\n\n\n\n", "\n\n").replace("\n\n\n\n", "\n\n").replace("\n\n\n", "\n\n") @@ -651,8 +652,8 @@ def edit_comment(cid, v): file=request.files["file"] if not file.content_type.startswith('image/'): return {"error": "That wasn't an image!"}, 400 - name = f'comment/{c.id}/{secrets.token_urlsafe(8)}' - url = upload_file(file) + if 'pcm' in request.host: url = upload_ibb(file) + else: url = upload_imgur(file) body += f"\n![]({url})" body = body.replace("\n", "\n\n").replace("\n\n\n\n\n\n", "\n\n").replace("\n\n\n\n", "\n\n").replace("\n\n\n", "\n\n") diff --git a/files/routes/posts.py b/files/routes/posts.py index 91c8681c1..bdc088225 100644 --- a/files/routes/posts.py +++ b/files/routes/posts.py @@ -518,7 +518,9 @@ def thumbs(new_post): for chunk in image_req.iter_content(1024): file.write(chunk) - post.thumburl = upload_file(resize=True) + if 'pcm' in request.host: post.thumburl = upload_ibb(resize=True) + else: post.thumburl = upload_imgur(resize=True) + g.db.add(post) def archiveorg(url): @@ -843,7 +845,9 @@ def submit_post(v): else: return render_template("submit.html", v=v, error=f"Image files only.", title=title, body=request.form.get("body", "")), 400 - new_post.url = upload_file(file) + if 'pcm' in request.host: new_post.url = upload_ibb(file) + else: new_post.url = upload_imgur(file) + g.db.add(new_post) g.db.add(new_post.submission_aux) diff --git a/files/routes/settings.py b/files/routes/settings.py index b139d5646..ecc2a7142 100644 --- a/files/routes/settings.py +++ b/files/routes/settings.py @@ -464,10 +464,17 @@ def settings_images_profile(v): abort(413) if request.headers.get("cf-ipcountry") == "T1": return "Image uploads are not allowed through TOR.", 403 - highres = upload_file(request.files["profile"]) + + if 'pcm' in request.host: highres = upload_ibb(request.files["profile"]) + else: highres = upload_imgur(request.files["profile"]) + if not highres: abort(400) - imageurl = upload_file(resize=True) + + if 'pcm' in request.host: imageurl = upload_ibb(resize=True) + else: imageurl = upload_imgur(resize=True) + if not imageurl: abort(400) + v.highres = highres v.profileurl = imageurl g.db.add(v) @@ -484,7 +491,10 @@ def settings_images_banner(v): abort(413) if request.headers.get("cf-ipcountry") == "T1": return "Image uploads are not allowed through TOR.", 403 - imageurl = upload_file(request.files["banner"]) + + if 'pcm' in request.host: imageurl = upload_ibb(request.files["banner"]) + else: imageurl = upload_imgur(request.files["banner"]) + if imageurl: v.bannerurl = imageurl g.db.add(v)