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 math
import os import os
from tkinter.tix import IMAGE
from turtle import Turtle
from typing import List
import discord import discord
from idna import intranges_contain
TIPS_MESSAGE = ":bulb: TIPS: You can disable Discord's default preview for link by wrapping it around `<` `>`" 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): 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: for file in files:
joined = os.path.join(tmp_dir, file) joined = os.path.join(tmp_dir, file)
to_send_files.append(discord.File(joined, file)) to_send_files.append(discord.File(joined, file))
if len(to_send_files) > 10:
first = True upload_size_limit = (
while len(to_send_files) > 10: message.guild.filesize_limit
splited_file = to_send_files[:10] if message.guild is not None
to_send_files = to_send_files[10:] else DEFAULT_UPLOAD_SIZE_LIMIT
if first: )
await message.reply( first = True
files=splited_file, content=(TIPS_MESSAGE if not muted else None) while len(to_send_files) > 0:
) nbr_file_to_send = get_max_files_number(to_send_files, upload_size_limit)
first = False splited_file = to_send_files[:nbr_file_to_send]
else: to_send_files = to_send_files[nbr_file_to_send:]
await message.channel.send(files=splited_file) if first and nbr_file_to_send <= 0:
await message.channel.send(files=to_send_files) return
else: elif nbr_file_to_send <= 0:
await message.reply( # Next file is to big to be sent, skip it
files=to_send_files, content=(TIPS_MESSAGE if not muted else None) 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) # type: ignore
async def send_command_reply( async def send_command_reply(
@ -43,23 +55,48 @@ async def send_command_reply(
for file in files: for file in files:
joined = os.path.join(tmp_dir, file) joined = os.path.join(tmp_dir, file)
to_send_files.append(discord.File(joined, file)) to_send_files.append(discord.File(joined, file))
if len(to_send_files) > 10:
if interaction.channel is not None: upload_size_limit = (
first = True interaction.guild.filesize_limit
while len(to_send_files) > 10: if interaction.guild is not None
splited_file = to_send_files[:10] else DEFAULT_UPLOAD_SIZE_LIMIT
to_send_files = to_send_files[10:] )
if first:
await interaction.followup.send( first = True
files=splited_file, while len(to_send_files) > 0:
content=f"{title} - Submited by {author.mention} - <{url}> {chr(10) + TIPS_MESSAGE if not muted else ''}", nbr_file_to_send = get_max_files_number(to_send_files, upload_size_limit)
) splited_file = to_send_files[:nbr_file_to_send]
first = False to_send_files = to_send_files[nbr_file_to_send:]
else: if first and nbr_file_to_send <= 0:
await interaction.channel.send(files=splited_file) # type: ignore await interaction.followup.send(
await interaction.channel.send(files=to_send_files) # type: ignore content=f"This image exceeds upload file size limit", ephemeral=True
else: )
await interaction.followup.send( elif nbr_file_to_send <= 0:
files=to_send_files, # Next file is to big to be sent, skip it
content=f"{title} - Submited by {author.mention} - <{url}> {chr(10) + TIPS_MESSAGE if not muted else ''}", 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 ''}",
)
first = False
else:
await interaction.channel.send(files=splited_file) # 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:
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 import pixivpy3
cpu_count = os.cpu_count() 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( 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 (url, dest_folder, api) = data
logging.debug(f"[{thread_name}] - Downloading {url}...") logging.debug(f"[{thread_name}] - Downloading {url}...")
api.download(url=url, path=dest_folder) api.download(url=url, path=dest_folder)
logging.debug(f"[{thread_name}] - ... Done") print(f"[{thread_name}] - ... Done")
q.task_done() q.task_done()