handle file size
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful

This commit is contained in:
SebClem 2023-11-02 16:15:41 +01:00
parent f23f6cc55f
commit 36591fda85
2 changed files with 76 additions and 39 deletions

View File

@ -1,9 +1,15 @@
import math
import os
from tkinter.tix import IMAGE
from turtle import Turtle
from typing import List
import discord
from idna import intranges_contain
TIPS_MESSAGE = ":bulb: TIPS: You can disable Discord's default preview for link by wrapping it around `<` `>`"
IMAGE_NBR_LIMIT = 10
DEFAULT_UPLOAD_SIZE_LIMIT = 209715200
async def send_message(message: discord.Message, files, tmp_dir: str, muted: bool):
@ -11,23 +17,29 @@ async def send_message(message: discord.Message, files, tmp_dir: str, muted: boo
for file in files:
joined = os.path.join(tmp_dir, file)
to_send_files.append(discord.File(joined, file))
if len(to_send_files) > 10:
upload_size_limit = (
message.guild.filesize_limit
if message.guild is not None
else DEFAULT_UPLOAD_SIZE_LIMIT
)
first = True
while len(to_send_files) > 10:
splited_file = to_send_files[:10]
to_send_files = to_send_files[10:]
if first:
while len(to_send_files) > 0:
nbr_file_to_send = get_max_files_number(to_send_files, upload_size_limit)
splited_file = to_send_files[:nbr_file_to_send]
to_send_files = to_send_files[nbr_file_to_send:]
if first and nbr_file_to_send <= 0:
return
elif nbr_file_to_send <= 0:
# Next file is to big to be sent, skip it
to_send_files = to_send_files[1:]
elif first:
await message.reply(
files=splited_file, content=(TIPS_MESSAGE if not muted else None)
)
first = False
else:
await message.channel.send(files=splited_file)
await message.channel.send(files=to_send_files)
else:
await message.reply(
files=to_send_files, content=(TIPS_MESSAGE if not muted else None)
)
await message.channel.send(files=splited_file) # type: ignore
async def send_command_reply(
@ -43,13 +55,26 @@ async def send_command_reply(
for file in files:
joined = os.path.join(tmp_dir, file)
to_send_files.append(discord.File(joined, file))
if len(to_send_files) > 10:
if interaction.channel is not None:
upload_size_limit = (
interaction.guild.filesize_limit
if interaction.guild is not None
else DEFAULT_UPLOAD_SIZE_LIMIT
)
first = True
while len(to_send_files) > 10:
splited_file = to_send_files[:10]
to_send_files = to_send_files[10:]
if first:
while len(to_send_files) > 0:
nbr_file_to_send = get_max_files_number(to_send_files, upload_size_limit)
splited_file = to_send_files[:nbr_file_to_send]
to_send_files = to_send_files[nbr_file_to_send:]
if first and nbr_file_to_send <= 0:
await interaction.followup.send(
content=f"This image exceeds upload file size limit", ephemeral=True
)
elif nbr_file_to_send <= 0:
# Next file is to big to be sent, skip it
to_send_files = to_send_files[1:]
elif first:
await interaction.followup.send(
files=splited_file,
content=f"{title} - Submited by {author.mention} - <{url}> {chr(10) + TIPS_MESSAGE if not muted else ''}",
@ -57,9 +82,21 @@ async def send_command_reply(
first = False
else:
await interaction.channel.send(files=splited_file) # type: ignore
await interaction.channel.send(files=to_send_files) # type: ignore
def get_max_files_number(files: List[discord.File], size_limit: int):
size = 0
nbr = 0
for file in files:
size = size + os.path.getsize(file.fp.name) # type: ignore
nbr = nbr + 1
if nbr >= IMAGE_NBR_LIMIT and size < size_limit:
return IMAGE_NBR_LIMIT
elif size > size_limit:
# We exceeds size limit, remove one image
if nbr >= IMAGE_NBR_LIMIT:
return IMAGE_NBR_LIMIT - 1
else:
await interaction.followup.send(
files=to_send_files,
content=f"{title} - Submited by {author.mention} - <{url}> {chr(10) + TIPS_MESSAGE if not muted else ''}",
)
return nbr - 1
# We didn't reach any limit
return nbr

View File

@ -8,7 +8,7 @@ from typing import List, Tuple, Union
import pixivpy3
cpu_count = os.cpu_count()
max_thread_num = cpu_count * 4 if cpu_count is not None else 4
max_thread_num = cpu_count * 2 if cpu_count is not None else 4
def dowload_pixiv_images(
@ -73,5 +73,5 @@ def download_thread(q: Queue[Union[Tuple[str, str, pixivpy3.AppPixivAPI], None]]
(url, dest_folder, api) = data
logging.debug(f"[{thread_name}] - Downloading {url}...")
api.download(url=url, path=dest_folder)
logging.debug(f"[{thread_name}] - ... Done")
print(f"[{thread_name}] - ... Done")
q.task_done()