remotes/1693045480750635534/spooky-22
parent
4a656c1454
commit
b8749ef949
|
@ -61,7 +61,7 @@ def upload_ibb(file=None, resize=False):
|
||||||
data={'image': base64.b64encode(f.read())}
|
data={'image': base64.b64encode(f.read())}
|
||||||
req = requests.post(f'https://api.imgbb.com/1/upload?key={IBB_KEY}', data=data)
|
req = requests.post(f'https://api.imgbb.com/1/upload?key={IBB_KEY}', data=data)
|
||||||
resp = req.json()['data']
|
resp = req.json()['data']
|
||||||
url = resp['url'].replace(".png", ".webp").replace(".jpg", ".webp").replace(".jpeg", ".webp")
|
url = resp['url']
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(e)
|
print(e)
|
||||||
print(req.text)
|
print(req.text)
|
||||||
|
@ -70,55 +70,6 @@ def upload_ibb(file=None, resize=False):
|
||||||
return url
|
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):
|
class UploadException(Exception):
|
||||||
"""Custom exception to raise if upload goes wrong"""
|
"""Custom exception to raise if upload goes wrong"""
|
||||||
pass
|
pass
|
||||||
|
|
|
@ -285,8 +285,7 @@ def api_comment(v):
|
||||||
g.db.rollback()
|
g.db.rollback()
|
||||||
abort(413)
|
abort(413)
|
||||||
|
|
||||||
if 'pcmemes.net' in request.host: url = upload_ibb(file=file)
|
upload_ibb(file=file)
|
||||||
else: url = upload_imgur(file=file)
|
|
||||||
|
|
||||||
body = request.form.get("body") + f"\n![]({url})"
|
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")
|
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()
|
g.db.rollback()
|
||||||
abort(413)
|
abort(413)
|
||||||
|
|
||||||
if 'pcmemes.net' in request.host: url = upload_ibb(file=file)
|
url = upload_ibb(file=file)
|
||||||
else: url = upload_imgur(file=file)
|
|
||||||
|
|
||||||
body += f"\n![]({url})"
|
body += f"\n![]({url})"
|
||||||
body_md = CustomRenderer().render(mistletoe.Document(body))
|
body_md = CustomRenderer().render(mistletoe.Document(body))
|
||||||
|
|
|
@ -801,8 +801,7 @@ def submit_post(v):
|
||||||
), 403
|
), 403
|
||||||
|
|
||||||
if file.content_type.startswith('image/'):
|
if file.content_type.startswith('image/'):
|
||||||
if 'pcmemes.net' in request.host: new_post.url = upload_ibb(file=file)
|
new_post.url = upload_ibb(file=file)
|
||||||
else: new_post.url = upload_imgur(file=file)
|
|
||||||
else:
|
else:
|
||||||
try:
|
try:
|
||||||
post_url = upload_video(file)
|
post_url = upload_video(file)
|
||||||
|
|
|
@ -117,8 +117,7 @@ def settings_profile_post(v):
|
||||||
if request.headers.get("Authorization"): return {"error": f"Image files only"}, 400
|
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
|
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)
|
upload_ibb(file=file)
|
||||||
else: url = upload_imgur(file=file)
|
|
||||||
|
|
||||||
bio += f"\n\n![]({url})"
|
bio += f"\n\n![]({url})"
|
||||||
|
|
||||||
|
@ -502,17 +501,11 @@ def settings_images_profile(v):
|
||||||
|
|
||||||
file = request.files["profile"]
|
file = request.files["profile"]
|
||||||
|
|
||||||
if 'pcmemes.net' in request.host:
|
file.save("image.webp")
|
||||||
file.save("image.webp")
|
highres = upload_ibb()
|
||||||
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)
|
|
||||||
if not highres: abort(400)
|
if not highres: abort(400)
|
||||||
|
|
||||||
if 'pcmemes.net' in request.host: imageurl = upload_ibb(resize=True)
|
imageurl = upload_ibb(resize=True)
|
||||||
else: imageurl = upload_imgur(filepath=filepath, resize=True)
|
|
||||||
if not imageurl: abort(400)
|
if not imageurl: abort(400)
|
||||||
|
|
||||||
v.highres = highres
|
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
|
if request.headers.get("cf-ipcountry") == "T1": return "Image uploads are not allowed through TOR.", 403
|
||||||
|
|
||||||
file = request.files["banner"]
|
file = request.files["banner"]
|
||||||
if 'pcmemes.net' in request.host: imageurl = upload_ibb(file=file)
|
imageurl = upload_ibb(file=file)
|
||||||
else: imageurl = upload_imgur(file=file)
|
|
||||||
|
|
||||||
if imageurl:
|
if imageurl:
|
||||||
v.bannerurl = imageurl
|
v.bannerurl = imageurl
|
||||||
|
|
Loading…
Reference in New Issue