Add config option to set maximum image pixels before sending as document
Fixes #552
This commit is contained in:
@@ -138,6 +138,7 @@ class Config(BaseBridgeConfig):
|
|||||||
copy("bridge.invite_link_resolve")
|
copy("bridge.invite_link_resolve")
|
||||||
copy("bridge.inline_images")
|
copy("bridge.inline_images")
|
||||||
copy("bridge.image_as_file_size")
|
copy("bridge.image_as_file_size")
|
||||||
|
copy("bridge.image_as_file_pixels")
|
||||||
copy("bridge.max_document_size")
|
copy("bridge.max_document_size")
|
||||||
copy("bridge.parallel_file_transfer")
|
copy("bridge.parallel_file_transfer")
|
||||||
copy("bridge.federate_rooms")
|
copy("bridge.federate_rooms")
|
||||||
|
|||||||
@@ -214,6 +214,8 @@ bridge:
|
|||||||
inline_images: false
|
inline_images: false
|
||||||
# Maximum size of image in megabytes before sending to Telegram as a document.
|
# Maximum size of image in megabytes before sending to Telegram as a document.
|
||||||
image_as_file_size: 10
|
image_as_file_size: 10
|
||||||
|
# Maximum number of pixels in an image before sending to Telegram as a document. Defaults to 1280x1280 = 1638400.
|
||||||
|
image_as_file_pixels: 1638400
|
||||||
# Maximum size of Telegram documents in megabytes to bridge.
|
# Maximum size of Telegram documents in megabytes to bridge.
|
||||||
max_document_size: 100
|
max_document_size: 100
|
||||||
# Enable experimental parallel file transfer, which makes uploads/downloads much faster by
|
# Enable experimental parallel file transfer, which makes uploads/downloads much faster by
|
||||||
|
|||||||
@@ -1516,6 +1516,7 @@ class Portal(DBPortal, BasePortal):
|
|||||||
w = h = None
|
w = h = None
|
||||||
file_name = content["net.maunium.telegram.internal.filename"]
|
file_name = content["net.maunium.telegram.internal.filename"]
|
||||||
max_image_size = self.config["bridge.image_as_file_size"] * 1000**2
|
max_image_size = self.config["bridge.image_as_file_size"] * 1000**2
|
||||||
|
max_image_pixels = self.config["bridge.image_as_file_pixels"]
|
||||||
|
|
||||||
if self.config["bridge.parallel_file_transfer"] and content.url:
|
if self.config["bridge.parallel_file_transfer"] and content.url:
|
||||||
file_handle, file_size = await util.parallel_transfer_to_telegram(
|
file_handle, file_size = await util.parallel_transfer_to_telegram(
|
||||||
@@ -1548,12 +1549,14 @@ class Portal(DBPortal, BasePortal):
|
|||||||
file_size = len(file)
|
file_size = len(file)
|
||||||
|
|
||||||
file_handle.name = file_name
|
file_handle.name = file_name
|
||||||
|
force_document = file_size >= max_image_size
|
||||||
|
|
||||||
attributes = [DocumentAttributeFilename(file_name=file_name)]
|
attributes = [DocumentAttributeFilename(file_name=file_name)]
|
||||||
if w and h:
|
if w and h:
|
||||||
attributes.append(DocumentAttributeImageSize(w, h))
|
attributes.append(DocumentAttributeImageSize(w, h))
|
||||||
|
force_document = force_document or w * h >= max_image_pixels
|
||||||
|
|
||||||
if (mime == "image/png" or mime == "image/jpeg") and file_size < max_image_size:
|
if (mime == "image/png" or mime == "image/jpeg") and not force_document:
|
||||||
media = InputMediaUploadedPhoto(file_handle)
|
media = InputMediaUploadedPhoto(file_handle)
|
||||||
else:
|
else:
|
||||||
media = InputMediaUploadedDocument(
|
media = InputMediaUploadedDocument(
|
||||||
|
|||||||
Reference in New Issue
Block a user