[qrcode] Allow binary content for qrcode.generate.

This commit is contained in:
Fabio Manganiello 2024-07-21 17:15:05 +02:00
parent 8329de15ba
commit 2ccf00508d
Signed by untrusted user: blacklight
GPG key ID: D90FBA7F76362774
2 changed files with 22 additions and 5 deletions

View file

@ -3,7 +3,7 @@ import io
import os import os
import threading import threading
import time import time
from typing import Optional, List from typing import Optional, List, Union
import qrcode import qrcode
from pyzbar import pyzbar from pyzbar import pyzbar
@ -56,7 +56,8 @@ class QrcodePlugin(Plugin):
@action @action
def generate( def generate(
self, self,
content: str, content: Union[str, bytes],
binary: bool = False,
output_file: Optional[str] = None, output_file: Optional[str] = None,
show: bool = False, show: bool = False,
format: str = 'png', format: str = 'png',
@ -69,6 +70,9 @@ class QrcodePlugin(Plugin):
``http://<host>:<port>/qrcode?content=...``. ``http://<host>:<port>/qrcode?content=...``.
:param content: Text, URL or content of the QR code. :param content: Text, URL or content of the QR code.
:param binary: If True then the content will be treated as binary data.
Content needs to be either a bytes object or a base64-encoded
string.
:param output_file: If set then the QR code will be exported in the :param output_file: If set then the QR code will be exported in the
specified image file. Otherwise, a base64-encoded representation of specified image file. Otherwise, a base64-encoded representation of
its binary content will be returned in the response as ``data``. its binary content will be returned in the response as ``data``.
@ -78,10 +82,23 @@ class QrcodePlugin(Plugin):
:param format: Output image format (default: ``png``). :param format: Output image format (default: ``png``).
:return: .. schema:: qrcode.QrcodeGeneratedSchema :return: .. schema:: qrcode.QrcodeGeneratedSchema
""" """
if binary:
if isinstance(content, str):
try:
content = base64.b64decode(content)
except ValueError as e:
raise AssertionError(f'Invalid base64-encoded binary content: {e}')
assert isinstance(content, bytes), 'Invalid binary content'
qr = qrcode.make(content) qr = qrcode.make(content)
img = qr.get_image() img = qr.get_image()
ret = { ret = {
'content': content, 'content': (
content
if isinstance(content, str)
else base64.b64encode(content).decode()
),
'format': format, 'format': format,
} }
@ -95,7 +112,7 @@ class QrcodePlugin(Plugin):
else: else:
f = io.BytesIO() f = io.BytesIO()
img.save(f, format=format) img.save(f, format=format)
ret['data'] = base64.encodebytes(f.getvalue()).decode() ret['data'] = base64.b64encode(f.getvalue()).decode()
return dict(QrcodeGeneratedSchema().dump(ret)) return dict(QrcodeGeneratedSchema().dump(ret))

View file

@ -19,7 +19,7 @@ class QrcodeGeneratedSchema(Schema):
text = fields.String( text = fields.String(
required=True, required=True,
metadata={ metadata={
'description': 'Text content of the QR code', 'description': 'Text content of the QR code, or base64-encoded binary data',
'example': 'https://platypush.tech', 'example': 'https://platypush.tech',
}, },
) )