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-06-19 14:49:44 +00:00
|
|
|
import gevent
|
2022-09-27 01:24:20 +00:00
|
|
|
import imagehash
|
2022-10-06 05:23:05 +00:00
|
|
|
from shutil import copyfile
|
2022-10-06 05:54:04 +00:00
|
|
|
from files.classes.media import *
|
2022-10-13 07:27:56 +00:00
|
|
|
from files.helpers.cloudflare import purge_files_in_cache
|
2022-10-06 06:00:41 +00:00
|
|
|
from files.__main__ import db_session
|
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-10-06 07:30:38 +00:00
|
|
|
url = process_image(name, patron=g.v.patron)
|
2022-06-18 15:53:34 +00:00
|
|
|
body += f"\n\n![]({url})"
|
|
|
|
elif file.content_type.startswith('video/'):
|
2022-10-18 11:09:53 +00:00
|
|
|
body += f"\n\n{SITE_FULL}{process_video(file)}"
|
2022-06-18 15:53:34 +00:00
|
|
|
elif file.content_type.startswith('audio/'):
|
2022-10-18 11:09:53 +00:00
|
|
|
body += f"\n\n{SITE_FULL}{process_audio(file)}"
|
2022-06-18 15:53:34 +00:00
|
|
|
else:
|
2022-06-19 16:56:45 +00:00
|
|
|
abort(415)
|
2022-06-18 15:53:34 +00:00
|
|
|
return body
|
|
|
|
|
|
|
|
|
2022-05-23 19:00:14 +00:00
|
|
|
def process_audio(file):
|
2022-06-22 17:44:43 +00:00
|
|
|
name = f'/audio/{time.time()}'.replace('.','')
|
|
|
|
|
|
|
|
extension = file.filename.split('.')[-1].lower()
|
|
|
|
name = name + '.' + extension
|
|
|
|
|
2022-05-22 22:15:29 +00:00
|
|
|
file.save(name)
|
2022-05-23 18:03:59 +00:00
|
|
|
|
2022-10-06 04:31:08 +00:00
|
|
|
size = os.stat(name).st_size
|
2022-10-12 08:52:08 +00:00
|
|
|
if size > MAX_IMAGE_AUDIO_SIZE_MB_PATRON * 1024 * 1024 or not g.v.patron and size > MAX_IMAGE_AUDIO_SIZE_MB * 1024 * 1024:
|
2022-10-06 04:31:08 +00:00
|
|
|
os.remove(name)
|
2022-10-12 10:45:45 +00:00
|
|
|
abort(413, f"Max image/audio size is {MAX_IMAGE_AUDIO_SIZE_MB} MB ({MAX_IMAGE_AUDIO_SIZE_MB_PATRON} MB for {patron.lower()}s)")
|
2022-05-23 18:03:59 +00:00
|
|
|
|
2022-10-11 17:26:38 +00:00
|
|
|
media = g.db.query(Media).filter_by(filename=name, kind='audio').one_or_none()
|
|
|
|
if media: g.db.delete(media)
|
|
|
|
|
2022-10-06 05:54:04 +00:00
|
|
|
media = Media(
|
|
|
|
kind='audio',
|
2022-10-11 17:20:15 +00:00
|
|
|
filename=name,
|
2022-10-06 05:54:04 +00:00
|
|
|
user_id=g.v.id,
|
|
|
|
size=size
|
|
|
|
)
|
|
|
|
g.db.add(media)
|
|
|
|
|
2022-10-18 11:09:53 +00:00
|
|
|
return name
|
2022-05-22 22:15:29 +00:00
|
|
|
|
|
|
|
|
2022-10-06 06:00:41 +00:00
|
|
|
def webm_to_mp4(old, new, vid):
|
2022-10-06 05:16:24 +00:00
|
|
|
tmp = new.replace('.mp4', '-t.mp4')
|
2022-10-06 19:17:36 +00:00
|
|
|
subprocess.run(["ffmpeg", "-y", "-loglevel", "warning", "-nostats", "-threads:v", "1", "-i", old, "-map_metadata", "-1", tmp], check=True, stderr=subprocess.STDOUT)
|
2022-10-06 05:07:24 +00:00
|
|
|
os.replace(tmp, new)
|
2022-10-06 04:54:05 +00:00
|
|
|
os.remove(old)
|
2022-10-13 07:27:56 +00:00
|
|
|
purge_files_in_cache(f"{SITE_FULL}{new}")
|
2022-10-06 06:00:41 +00:00
|
|
|
db = db_session()
|
2022-10-11 17:26:38 +00:00
|
|
|
|
2022-10-11 17:28:08 +00:00
|
|
|
media = db.query(Media).filter_by(filename=new, kind='video').one_or_none()
|
2022-10-11 17:26:38 +00:00
|
|
|
if media: db.delete(media)
|
|
|
|
|
2022-10-06 06:00:41 +00:00
|
|
|
media = Media(
|
|
|
|
kind='video',
|
2022-10-11 17:20:15 +00:00
|
|
|
filename=new,
|
2022-10-06 06:00:41 +00:00
|
|
|
user_id=vid,
|
|
|
|
size=os.stat(new).st_size
|
|
|
|
)
|
|
|
|
db.add(media)
|
|
|
|
db.commit()
|
|
|
|
db.close()
|
|
|
|
|
|
|
|
|
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-19 13:03:14 +00:00
|
|
|
file.save(old)
|
2022-06-22 17:44:43 +00:00
|
|
|
|
2022-10-06 04:31:08 +00:00
|
|
|
size = os.stat(old).st_size
|
2022-10-19 02:15:57 +00:00
|
|
|
if (SITE_NAME != 'WPD' and
|
|
|
|
(size > MAX_VIDEO_SIZE_MB_PATRON * 1024 * 1024
|
|
|
|
or not g.v.patron and size > MAX_VIDEO_SIZE_MB * 1024 * 1024)):
|
2022-10-06 04:31:08 +00:00
|
|
|
os.remove(old)
|
2022-10-12 08:53:23 +00:00
|
|
|
abort(413, f"Max video size is {MAX_VIDEO_SIZE_MB} MB ({MAX_VIDEO_SIZE_MB_PATRON} MB for paypigs)")
|
2022-10-06 04:31:08 +00:00
|
|
|
|
2022-06-22 17:44:43 +00:00
|
|
|
extension = file.filename.split('.')[-1].lower()
|
|
|
|
new = old + '.' + extension
|
2022-05-24 20:07:04 +00:00
|
|
|
|
2022-10-06 04:54:05 +00:00
|
|
|
if extension == 'webm':
|
|
|
|
new = new.replace('.webm', '.mp4')
|
2022-10-06 05:23:05 +00:00
|
|
|
copyfile(old, new)
|
2022-10-06 06:00:41 +00:00
|
|
|
gevent.spawn(webm_to_mp4, old, new, g.v.id)
|
2022-10-06 04:54:05 +00:00
|
|
|
else:
|
2022-10-06 19:17:36 +00:00
|
|
|
subprocess.run(["ffmpeg", "-y", "-loglevel", "warning", "-nostats", "-i", old, "-map_metadata", "-1", "-c:v", "copy", "-c:a", "copy", new], check=True)
|
2022-10-06 04:54:05 +00:00
|
|
|
os.remove(old)
|
|
|
|
|
2022-10-11 17:28:08 +00:00
|
|
|
media = g.db.query(Media).filter_by(filename=new, kind='video').one_or_none()
|
2022-10-11 17:26:38 +00:00
|
|
|
if media: g.db.delete(media)
|
|
|
|
|
2022-10-06 06:00:41 +00:00
|
|
|
media = Media(
|
|
|
|
kind='video',
|
2022-10-11 17:20:15 +00:00
|
|
|
filename=new,
|
2022-10-06 06:00:41 +00:00
|
|
|
user_id=g.v.id,
|
|
|
|
size=os.stat(new).st_size
|
|
|
|
)
|
|
|
|
g.db.add(media)
|
2022-10-06 05:54:04 +00:00
|
|
|
|
2022-10-18 11:09:53 +00:00
|
|
|
return new
|
2022-05-23 19:00:14 +00:00
|
|
|
|
2022-05-22 16:13:19 +00:00
|
|
|
|
2022-05-07 06:04:14 +00:00
|
|
|
|
2022-10-06 19:07:45 +00:00
|
|
|
def process_image(filename=None, resize=0, trim=False, uploader=None, patron=False, db=None):
|
2022-05-07 06:04:14 +00:00
|
|
|
size = os.stat(filename).st_size
|
|
|
|
|
2022-10-12 08:52:08 +00:00
|
|
|
if size > MAX_IMAGE_AUDIO_SIZE_MB_PATRON * 1024 * 1024 or not patron and size > MAX_IMAGE_AUDIO_SIZE_MB * 1024 * 1024:
|
2022-09-10 00:38:06 +00:00
|
|
|
os.remove(filename)
|
2022-10-12 08:52:08 +00:00
|
|
|
abort(413, f"Max image/audio size is {MAX_IMAGE_AUDIO_SIZE_MB} MB ({MAX_IMAGE_AUDIO_SIZE_MB_PATRON} MB for paypigs)")
|
2022-05-04 23:09:46 +00:00
|
|
|
|
2022-10-25 15:41:18 +00:00
|
|
|
with Image.open(filename) as i:
|
2022-10-25 20:19:16 +00:00
|
|
|
params = ["convert", "-coalesce", filename, "-quality", "88", "-define", "webp:method=6", "-strip", "-auto-orient"]
|
2022-10-25 18:45:32 +00:00
|
|
|
if trim and len(list(Iterator(i))) == 1:
|
|
|
|
params.append("-trim")
|
|
|
|
if resize and i.width > resize:
|
|
|
|
params.extend(["-resize", f"{resize}>"])
|
2022-10-25 15:41:18 +00:00
|
|
|
|
|
|
|
params.append(filename)
|
|
|
|
subprocess.run(params)
|
2022-05-04 23:09:46 +00:00
|
|
|
|
2022-09-27 01:24:20 +00:00
|
|
|
|
2022-10-22 15:24:07 +00:00
|
|
|
if resize:
|
2022-10-15 10:37:55 +00:00
|
|
|
if os.stat(filename).st_size > MAX_IMAGE_SIZE_BANNER_RESIZED_KB * 1024:
|
2022-09-27 01:24:20 +00:00
|
|
|
os.remove(filename)
|
2022-10-23 09:27:40 +00:00
|
|
|
abort(413, f"Max size for site assets is {MAX_IMAGE_SIZE_BANNER_RESIZED_KB} KB")
|
2022-09-27 01:24:20 +00:00
|
|
|
|
2022-10-22 15:24:07 +00:00
|
|
|
if filename.startswith('files/assets/images/'):
|
|
|
|
path = filename.rsplit('/', 1)[0]
|
2022-10-23 09:27:40 +00:00
|
|
|
kind = path.split('/')[-1]
|
|
|
|
|
|
|
|
if kind in ('banners','sidebar','badges'):
|
|
|
|
hashes = {}
|
|
|
|
|
|
|
|
for img in os.listdir(path):
|
|
|
|
if resize == 400 and img in ('256.webp','585.webp'): continue
|
|
|
|
img_path = f'{path}/{img}'
|
|
|
|
if img_path == filename: continue
|
2022-10-25 15:39:57 +00:00
|
|
|
|
|
|
|
with Image.open(img_path) as i:
|
|
|
|
i_hash = str(imagehash.phash(i))
|
|
|
|
|
2022-10-23 09:27:40 +00:00
|
|
|
if i_hash in hashes.keys():
|
|
|
|
print(hashes[i_hash], flush=True)
|
|
|
|
print(img_path, flush=True)
|
|
|
|
else: hashes[i_hash] = img_path
|
|
|
|
|
2022-10-25 15:39:57 +00:00
|
|
|
with Image.open(filename) as i:
|
|
|
|
i_hash = str(imagehash.phash(i))
|
|
|
|
|
2022-10-22 15:24:07 +00:00
|
|
|
if i_hash in hashes.keys():
|
2022-10-23 09:27:40 +00:00
|
|
|
os.remove(filename)
|
|
|
|
abort(409, "Image already exists!")
|
2022-09-26 01:27:31 +00:00
|
|
|
|
2022-10-11 17:26:38 +00:00
|
|
|
db = db or g.db
|
|
|
|
|
2022-10-11 17:28:08 +00:00
|
|
|
media = db.query(Media).filter_by(filename=filename, kind='image').one_or_none()
|
2022-10-11 17:26:38 +00:00
|
|
|
if media: db.delete(media)
|
|
|
|
|
2022-10-06 05:54:04 +00:00
|
|
|
media = Media(
|
|
|
|
kind='image',
|
2022-10-11 17:20:15 +00:00
|
|
|
filename=filename,
|
2022-10-06 09:15:21 +00:00
|
|
|
user_id=uploader or g.v.id,
|
2022-10-06 05:54:04 +00:00
|
|
|
size=os.stat(filename).st_size
|
|
|
|
)
|
2022-10-06 19:07:45 +00:00
|
|
|
db.add(media)
|
2022-10-06 05:54:04 +00:00
|
|
|
|
2022-09-29 05:43:29 +00:00
|
|
|
return filename
|