plugin XEP-0363, upload: compute hash on file upload:

- Hash (SHA-256) is now automacally computed when a file is uploaded via XEP-0363, and
  used if no hash has been specified by frontend already.
- Use named tuples (`UploadTracking` and `UploadResult`) to handle callback values.
- upload: fix a bug (missing variable) in `unregister` (which is not used yet anyway).
- usual docstring/type hints improvements
This commit is contained in:
Goffi 2026-07-09 13:07:01 +02:00
commit 92cb52dd04

View file

@ -21,7 +21,9 @@ import mimetypes
from pathlib import Path
import shutil
import tempfile
from typing import Callable, Optional
from typing import Callable, Optional, cast
from libervia.backend.plugins.plugin_misc_upload import UploadPlugin, UploadResult
from twisted.internet import defer
@ -30,6 +32,7 @@ from libervia.backend.core.constants import Const as C
from libervia.backend.core.core_types import SatXMPPEntity
from libervia.backend.core.i18n import _
from libervia.backend.core.log import getLogger
from libervia.backend.plugins.plugin_misc_upload import UploadPlugin, UploadResult
from libervia.backend.tools import utils
from libervia.backend.tools import image
@ -56,7 +59,7 @@ class AttachPlugin:
def __init__(self, host):
log.info(_("plugin Attach initialization"))
self.host = host
self._u = host.plugins["UPLOAD"]
self._u = cast(UploadPlugin, host.plugins["UPLOAD"])
host.trigger.add("sendMessage", self._send_message_trigger)
host.trigger.add("sendMessageComponent", self._send_message_trigger)
self._attachments_handlers = {"clear": [], "encrypted": []}
@ -220,6 +223,7 @@ class AttachPlugin:
attachment["size"] = path.stat().st_size
extra = {"attachment": attachment}
extra["hash_algo"] = "sha-256"
progress_id = attachment.pop("progress_id", None)
if progress_id:
extra["progress_id"] = progress_id
@ -232,27 +236,30 @@ class AttachPlugin:
_("certificate check disabled for upload, this is dangerous!")
)
__, upload_d = await upload_cb(
upload_result = await upload_cb(
client=client,
filepath=path,
filename=name,
extra=extra,
)
uploads_d.append(upload_d)
uploads_d.append(upload_result.download_d)
for attachment in to_delete:
attachments.remove(attachment)
upload_results = await defer.DeferredList(uploads_d)
for idx, (success, ret) in enumerate(upload_results):
for idx, (success, upload_result) in enumerate(upload_results):
attachment = attachments[idx]
if not success:
# ret is a failure here
log.warning(f"error while uploading {attachment}: {ret}")
# upload_result is a failure here
log.warning(f"error while uploading {attachment}: {upload_result}")
continue
attachment["url"] = ret
attachment["url"] = upload_result.url
if upload_result.hash and "hash" not in attachment:
attachment["hash"] = upload_result.hash
attachment["hash_algo"] = upload_result.hash_algo
return data