remotes/1693045480750635534/spooky-22
Aevann1 2021-09-13 18:35:26 +02:00
parent 4e18f2ead3
commit bb5fa9f328
4 changed files with 57 additions and 19 deletions

View File

@ -11,7 +11,46 @@ CF_ZONE = environ.get("CLOUDFLARE_ZONE", "").strip()
IMGUR_KEY = environ.get("IMGUR_KEY", "").strip()
IBB_KEY = environ.get("IBB_KEY", "").strip()
def upload(ibb=False, filepath=None, file=None, resize=False):
def upload_ibb(file=None, resize=False):
if file: file.save("image.webp")
i = IImage.open("image.webp")
if resize:
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.webp", save_all=True, append_images=list(frames), loop=0, optimize=True, quality=30)
except Exception as e:
print(e)
return
i.save("image.webp", optimize=True, quality=30)
try:
with open("image.webp", '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 Exception as e:
print(e)
print(req.text)
return
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')
@ -47,20 +86,19 @@ def upload(ibb=False, filepath=None, file=None, resize=False):
try:
with open(filepath, 'rb') as f:
data={'image': base64.b64encode(f.read())}
if ibb: req = requests.post(f'https://api.imgbb.com/1/upload?key={IBB_KEY}', data=data)
else: req = requests.post('https://api.imgur.com/3/upload.json', headers = {"Authorization": f"Client-ID {IMGUR_KEY}"}, data=data)
req = requests.post('https://api.imgur.com/3/upload.json', headers = {"Authorization": f"Client-ID {IMGUR_KEY}"}, data=data)
resp = req.json()['data']
if ibb: url = resp['url']
else: url = resp['link']
url = url.replace(".png", ".webp").replace(".jpg", ".webp").replace(".jpeg", ".webp")
url = resp['link']
if not "_d." in url:
url = url.replace(".png", "_d.png").replace(".jpg", "_d.jpg").replace(".jpeg", "_d.jpeg")
if "_d." in url: url += "?maxwidth=9999"
except Exception as e:
print(e)
print(req.text)
return
if not ibb:
new_image = Image(text=url, deletehash=resp["deletehash"])
g.db.add(new_image)
new_image = Image(text=url, deletehash=resp["deletehash"])
g.db.add(new_image)
return url

View File

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

View File

@ -523,12 +523,12 @@ def thumbs(new_post):
print(f'Unknown content type {x.headers.get("Content-Type")}')
return False, f'Unknown content type {x.headers.get("Content-Type")} for submitted content'
with open("image.png", "wb") as file:
with open("image.webp", "wb") as file:
for chunk in image_req.iter_content(1024):
file.write(chunk)
if 'pcmemes.net' in request.host: post.thumburl = upload_ibb(filepath="image.png", resize=True)
else: post.thumburl = upload(filepath="image.png", resize=True)
if 'pcmemes.net' in request.host: post.thumburl = upload_ibb(filepath="image.webp", resize=True)
else: post.thumburl = upload_imgur(filepath="image.webp", resize=True)
g.db.add(post)
@ -915,7 +915,7 @@ def submit_post(v):
if file.content_type.startswith('image/'):
if 'pcmemes.net' in request.host: new_post.url = upload_ibb(file=file)
else: new_post.url = upload(file=file)
else: new_post.url = upload_imgur(file=file)
else:
try:
post_url = upload_video(file)

View File

@ -118,7 +118,7 @@ def settings_profile_post(v):
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(file=file)
else: url = upload_imgur(file=file)
bio += f"\n\n![]({url})"
@ -506,11 +506,11 @@ def settings_images_profile(v):
file.save(filepath)
if 'pcmemes.net' in request.host: highres = upload_ibb(filepath=filepath)
else: highres = upload(filepath=filepath)
else: highres = upload_imgur(filepath=filepath)
if not highres: abort(400)
if 'pcmemes.net' in request.host: imageurl = upload_ibb(filepath=filepath, resize=True)
else: imageurl = upload(filepath=filepath, resize=True)
else: imageurl = upload_imgur(filepath=filepath, resize=True)
if not imageurl: abort(400)
v.highres = highres
@ -532,7 +532,7 @@ def settings_images_banner(v):
file = request.files["banner"]
if 'pcmemes.net' in request.host: imageurl = upload_ibb(file=file)
else: imageurl = upload(file=file)
else: imageurl = upload_imgur(file=file)
if imageurl:
v.bannerurl = imageurl