2024-01-03 11:37:33 +01:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
"""
|
|
|
|
Based on http_upload.py example from project slixmpp
|
|
|
|
https://codeberg.org/poezio/slixmpp/src/branch/master/examples/http_upload.py
|
|
|
|
"""
|
|
|
|
|
2024-06-13 17:53:53 +02:00
|
|
|
from slixfeed.log import Logger
|
2024-01-17 15:36:28 +01:00
|
|
|
from slixmpp.exceptions import IqTimeout, IqError
|
|
|
|
from slixmpp.plugins.xep_0363.http_upload import HTTPError
|
2024-06-13 17:53:53 +02:00
|
|
|
|
|
|
|
logger = Logger(__name__)
|
2024-01-03 11:37:33 +01:00
|
|
|
# import sys
|
|
|
|
|
2024-02-07 01:26:42 +01:00
|
|
|
class XmppUpload:
|
2024-01-03 11:37:33 +01:00
|
|
|
|
2024-02-07 01:26:42 +01:00
|
|
|
async def start(self, jid, filename, domain=None):
|
2024-06-13 17:53:53 +02:00
|
|
|
logger.info('Uploading file %s...', filename)
|
2024-01-17 15:36:28 +01:00
|
|
|
try:
|
2024-02-07 01:26:42 +01:00
|
|
|
upload_file = self['xep_0363'].upload_file
|
|
|
|
# if self.encrypted and not self['xep_0454']:
|
|
|
|
# print(
|
|
|
|
# 'The xep_0454 module isn\'t available. '
|
|
|
|
# 'Ensure you have \'cryptography\' '
|
|
|
|
# 'from extras_require installed.',
|
|
|
|
# file=sys.stderr,
|
2024-01-17 15:36:28 +01:00
|
|
|
# )
|
2024-02-07 01:26:42 +01:00
|
|
|
# return
|
|
|
|
# elif self.encrypted:
|
|
|
|
# upload_file = self['xep_0454'].upload_file
|
|
|
|
try:
|
|
|
|
url = await upload_file(
|
|
|
|
filename, domain, timeout=10,
|
|
|
|
)
|
2024-06-13 17:53:53 +02:00
|
|
|
logger.info('Upload successful!')
|
|
|
|
logger.info('Sending file to %s', jid)
|
2024-02-07 01:26:42 +01:00
|
|
|
except HTTPError:
|
2024-06-13 17:53:53 +02:00
|
|
|
url = ('Error: It appears that this server does not support '
|
|
|
|
'HTTP File Upload.')
|
|
|
|
logger.error('It appears that this server does not support '
|
|
|
|
'HTTP File Upload.')
|
2024-02-07 01:26:42 +01:00
|
|
|
# raise HTTPError(
|
|
|
|
# "This server doesn't appear to support HTTP File Upload"
|
|
|
|
# )
|
2024-06-13 17:53:53 +02:00
|
|
|
except IqError as e:
|
|
|
|
logger.error('Could not send message')
|
|
|
|
logger.error(e)
|
|
|
|
except IqTimeout as e:
|
|
|
|
# raise TimeoutError('Could not send message in time')
|
|
|
|
logger.error('Could not send message in time')
|
|
|
|
logger.error(e)
|
2024-02-07 01:26:42 +01:00
|
|
|
return url
|