master
Aevann1 2021-09-15 02:53:45 +02:00
parent 4a656c1454
commit b8749ef949
4 changed files with 9 additions and 69 deletions

View File

@ -61,7 +61,7 @@ def upload_ibb(file=None, resize=False):
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'].replace(".png", ".webp").replace(".jpg", ".webp").replace(".jpeg", ".webp")
url = resp['url']
except Exception as e:
print(e)
print(req.text)
@ -70,55 +70,6 @@ def upload_ibb(file=None, resize=False):
return url
def upload_imgur(filepath=None, file=None, resize=False):
if file:
format = file.filename.split('.')[-1].lower().replace('jpg','png').replace('jpeg','png')
filepath = f"image.{format}"
file.save(filepath)
else: format = filepath.split('.')[-1].lower().replace('jpg','png').replace('jpeg','png')
if resize:
i = IImage.open(filepath)
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
filepath = f"image.{i.format}".lower().replace('jpg','png').replace('jpeg','png')
try: om.save(filepath, save_all=True, append_images=list(frames), loop=0, optimize=True, quality=30)
except Exception as e:
print(e)
return
elif format != "gif":
i = IImage.open(filepath)
filepath = f"image.{i.format}".lower().replace('jpg','png').replace('jpeg','png')
i.save(filepath, optimize=True, quality=30)
try:
with open(filepath, 'rb') as f:
data={'image': base64.b64encode(f.read())}
req = requests.post('https://api.imgur.com/3/upload.json', headers = {"Authorization": f"Client-ID {IMGUR_KEY}"}, data=data)
resp = req.json()['data']
url = resp['link'].replace(".png", ".webp").replace(".jpg", ".webp").replace(".jpeg", ".webp")
except Exception as e:
print(e)
print(req.text)
return
new_image = Image(text=url, deletehash=resp["deletehash"])
g.db.add(new_image)
return url
class UploadException(Exception):
"""Custom exception to raise if upload goes wrong"""
pass

View File

@ -285,8 +285,7 @@ def api_comment(v):
g.db.rollback()
abort(413)
if 'pcmemes.net' in request.host: url = upload_ibb(file=file)
else: url = upload_imgur(file=file)
upload_ibb(file=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")
@ -698,8 +697,7 @@ def edit_comment(cid, v):
g.db.rollback()
abort(413)
if 'pcmemes.net' in request.host: url = upload_ibb(file=file)
else: url = upload_imgur(file=file)
url = upload_ibb(file=file)
body += f"\n![]({url})"
body_md = CustomRenderer().render(mistletoe.Document(body))

View File

@ -801,8 +801,7 @@ def submit_post(v):
), 403
if file.content_type.startswith('image/'):
if 'pcmemes.net' in request.host: new_post.url = upload_ibb(file=file)
else: new_post.url = upload_imgur(file=file)
new_post.url = upload_ibb(file=file)
else:
try:
post_url = upload_video(file)

View File

@ -117,8 +117,7 @@ def settings_profile_post(v):
if request.headers.get("Authorization"): return {"error": f"Image files only"}, 400
else: return render_template("settings_profile.html", v=v, error=f"Image files only."), 400
if 'pcmemes.net' in request.host: url = upload_ibb(file=file)
else: url = upload_imgur(file=file)
upload_ibb(file=file)
bio += f"\n\n![]({url})"
@ -502,17 +501,11 @@ def settings_images_profile(v):
file = request.files["profile"]
if 'pcmemes.net' in request.host:
file.save("image.webp")
highres = upload_ibb()
else:
filepath = f"image." + file.filename.split('.')[-1].lower().replace('jpg','png').replace('jpeg','png')
file.save(filepath)
highres = upload_imgur(filepath=filepath)
file.save("image.webp")
highres = upload_ibb()
if not highres: abort(400)
if 'pcmemes.net' in request.host: imageurl = upload_ibb(resize=True)
else: imageurl = upload_imgur(filepath=filepath, resize=True)
imageurl = upload_ibb(resize=True)
if not imageurl: abort(400)
v.highres = highres
@ -533,8 +526,7 @@ def settings_images_banner(v):
if request.headers.get("cf-ipcountry") == "T1": return "Image uploads are not allowed through TOR.", 403
file = request.files["banner"]
if 'pcmemes.net' in request.host: imageurl = upload_ibb(file=file)
else: imageurl = upload_imgur(file=file)
imageurl = upload_ibb(file=file)
if imageurl:
v.bannerurl = imageurl