2022-05-04 23:09:46 +00:00
|
|
|
from PIL import Image, ImageOps
|
|
|
|
from PIL.ImageSequence import Iterator
|
|
|
|
from webptools import gifwebp
|
|
|
|
import subprocess
|
2022-05-07 06:04:14 +00:00
|
|
|
import os
|
2022-06-18 20:41:00 +00:00
|
|
|
from flask import abort, g
|
2022-05-22 16:13:19 +00:00
|
|
|
import requests
|
|
|
|
import time
|
|
|
|
from .const import *
|
|
|
|
|
2022-05-22 22:15:29 +00:00
|
|
|
|
2022-06-18 15:53:34 +00:00
|
|
|
def process_files():
|
|
|
|
body = ''
|
|
|
|
if request.files.get("file") and request.headers.get("cf-ipcountry") != "T1":
|
|
|
|
files = request.files.getlist('file')[:4]
|
|
|
|
for file in files:
|
|
|
|
if file.content_type.startswith('image/'):
|
|
|
|
name = f'/images/{time.time()}'.replace('.','') + '.webp'
|
|
|
|
file.save(name)
|
2022-06-18 20:41:00 +00:00
|
|
|
url = process_image(name)
|
2022-06-18 15:53:34 +00:00
|
|
|
body += f"\n\n![]({url})"
|
|
|
|
elif file.content_type.startswith('video/'):
|
2022-06-18 16:23:10 +00:00
|
|
|
body += f"\n\n{process_video(file)}"
|
2022-06-18 15:53:34 +00:00
|
|
|
elif file.content_type.startswith('audio/'):
|
|
|
|
body += f"\n\n{process_audio(file)}"
|
|
|
|
else:
|
|
|
|
body += f"\n\n{process_other(file)}"
|
|
|
|
return body
|
|
|
|
|
|
|
|
|
|
|
|
def process_other(file):
|
|
|
|
req = requests.request("POST", "https://pomf2.lain.la/upload.php", files={'files[]': file}, timeout=20).json()
|
|
|
|
return req['files'][0]['url']
|
|
|
|
|
|
|
|
|
2022-05-23 19:00:14 +00:00
|
|
|
def process_audio(file):
|
2022-06-17 18:36:34 +00:00
|
|
|
name = f'/audio/{time.time()}'.replace('.','') + '.mp3'
|
2022-05-22 22:15:29 +00:00
|
|
|
file.save(name)
|
2022-05-23 18:03:59 +00:00
|
|
|
|
2022-05-27 17:19:12 +00:00
|
|
|
if os.stat(name).st_size > 8 * 1024 * 1024:
|
2022-05-23 18:03:59 +00:00
|
|
|
with open(name, 'rb') as f:
|
|
|
|
os.remove(name)
|
2022-06-17 20:15:42 +00:00
|
|
|
req = requests.request("POST", "https://pomf2.lain.la/upload.php",
|
|
|
|
files={'files[]': f}, timeout=20).json()
|
2022-05-23 18:03:59 +00:00
|
|
|
return req['files'][0]['url']
|
|
|
|
|
2022-05-22 22:15:29 +00:00
|
|
|
return f'{SITE_FULL}{name}'
|
|
|
|
|
|
|
|
|
2022-05-22 16:13:19 +00:00
|
|
|
def process_video(file):
|
2022-05-24 20:07:04 +00:00
|
|
|
old = f'/videos/{time.time()}'.replace('.','')
|
2022-06-17 18:36:34 +00:00
|
|
|
new = old + '.mp4'
|
2022-05-24 20:07:04 +00:00
|
|
|
|
2022-06-17 20:16:22 +00:00
|
|
|
if file.filename.split('.')[-1].lower() == 'webm':
|
2022-05-24 20:09:45 +00:00
|
|
|
file.save(new)
|
2022-05-24 20:07:04 +00:00
|
|
|
else:
|
2022-05-24 20:09:45 +00:00
|
|
|
file.save(old)
|
2022-06-17 20:23:04 +00:00
|
|
|
subprocess.run(["ffmpeg", "-y", "-loglevel", "warning", "-i", old, "-map_metadata", "-1", "-c:v", "copy", "-c:a", "copy", new], check=True)
|
2022-05-24 20:07:04 +00:00
|
|
|
os.remove(old)
|
|
|
|
|
|
|
|
size = os.stat(new).st_size
|
2022-05-27 17:19:12 +00:00
|
|
|
if os.stat(new).st_size > 8 * 1024 * 1024:
|
2022-05-24 20:07:04 +00:00
|
|
|
with open(new, 'rb') as f:
|
|
|
|
os.remove(new)
|
2022-06-17 20:15:42 +00:00
|
|
|
req = requests.request("POST", "https://pomf2.lain.la/upload.php",
|
|
|
|
files={'files[]': f}, timeout=20).json()
|
2022-05-23 19:00:14 +00:00
|
|
|
return req['files'][0]['url']
|
|
|
|
|
2022-05-24 20:07:04 +00:00
|
|
|
return f'{SITE_FULL}{new}'
|
2022-05-22 16:13:19 +00:00
|
|
|
|
2022-05-07 06:04:14 +00:00
|
|
|
|
2022-06-18 20:41:00 +00:00
|
|
|
def process_image(filename=None, resize=0):
|
2022-05-07 06:04:14 +00:00
|
|
|
size = os.stat(filename).st_size
|
|
|
|
|
2022-06-18 21:11:53 +00:00
|
|
|
if resize == 100: patron = False
|
|
|
|
else: patron = g.v.patron
|
|
|
|
|
|
|
|
if size > 16 * 1024 * 1024 or not patron and size > 8 * 1024 * 1024:
|
2022-05-07 06:04:14 +00:00
|
|
|
os.remove(filename)
|
|
|
|
abort(413)
|
2022-05-04 23:09:46 +00:00
|
|
|
|
|
|
|
i = Image.open(filename)
|
|
|
|
|
|
|
|
if resize and i.width > resize:
|
2022-06-17 20:16:22 +00:00
|
|
|
try: subprocess.run(["convert", filename, "-coalesce", "-resize", f"{resize}>", filename])
|
2022-05-04 23:09:46 +00:00
|
|
|
except: pass
|
|
|
|
elif i.format.lower() != "webp":
|
|
|
|
|
|
|
|
exif = i.getexif()
|
|
|
|
for k in exif.keys():
|
|
|
|
if k != 0x0112:
|
|
|
|
exif[k] = None
|
|
|
|
del exif[k]
|
|
|
|
i.info["exif"] = exif.tobytes()
|
|
|
|
|
|
|
|
if i.format.lower() == "gif":
|
2022-06-17 20:15:42 +00:00
|
|
|
gifwebp(input_image=filename, output_image=filename,
|
|
|
|
option="-mixed -metadata none -f 100 -mt -m 6")
|
2022-05-04 23:09:46 +00:00
|
|
|
else:
|
|
|
|
i = ImageOps.exif_transpose(i)
|
|
|
|
i.save(filename, format="WEBP", method=6)
|
|
|
|
|
2022-06-17 18:36:34 +00:00
|
|
|
return filename
|