2018-03-08 14:04:59 +00:00
|
|
|
"""
|
|
|
|
Upload a file and auto-complete the input with its URL.
|
|
|
|
|
|
|
|
Usage
|
|
|
|
-----
|
|
|
|
|
|
|
|
This plugin adds a command to the chat tabs.
|
|
|
|
|
|
|
|
.. glossary::
|
|
|
|
|
|
|
|
/upload
|
|
|
|
**Usage:** ``/upload <filename>``
|
|
|
|
|
|
|
|
Uploads the <filename> file to the preferred HTTP File Upload
|
|
|
|
service (see XEP-0363) and fill the input with its URL.
|
|
|
|
|
|
|
|
|
|
|
|
"""
|
2020-04-12 14:45:21 +00:00
|
|
|
|
|
|
|
from typing import Optional
|
|
|
|
|
2018-03-08 14:04:59 +00:00
|
|
|
import asyncio
|
2018-03-11 18:42:11 +00:00
|
|
|
import traceback
|
2018-03-08 14:04:59 +00:00
|
|
|
from os.path import expanduser
|
|
|
|
from glob import glob
|
|
|
|
|
2020-05-11 23:58:37 +00:00
|
|
|
from slixmpp.plugins.xep_0363.http_upload import FileTooBig, HTTPError, UploadServiceNotFound
|
2018-10-13 11:12:58 +00:00
|
|
|
|
2018-03-08 14:04:59 +00:00
|
|
|
from poezio.plugin import BasePlugin
|
|
|
|
from poezio.core.structs import Completion
|
|
|
|
from poezio.decorators import command_args_parser
|
|
|
|
from poezio import tabs
|
|
|
|
|
|
|
|
|
2018-08-15 11:13:17 +00:00
|
|
|
class Plugin(BasePlugin):
|
2020-04-12 14:45:21 +00:00
|
|
|
dependencies = {'embed'}
|
|
|
|
|
2018-03-08 14:04:59 +00:00
|
|
|
def init(self):
|
2020-04-12 14:45:21 +00:00
|
|
|
self.embed = self.refs['embed']
|
|
|
|
|
2018-03-09 11:44:29 +00:00
|
|
|
if not self.core.xmpp['xep_0363']:
|
|
|
|
raise Exception('slixmpp XEP-0363 plugin failed to load')
|
2018-11-27 12:38:04 +00:00
|
|
|
for _class in (tabs.PrivateTab, tabs.StaticConversationTab, tabs.DynamicConversationTab, tabs.MucTab):
|
2018-08-15 11:13:17 +00:00
|
|
|
self.api.add_tab_command(
|
|
|
|
_class,
|
|
|
|
'upload',
|
|
|
|
self.command_upload,
|
|
|
|
usage='<filename>',
|
|
|
|
help='Upload a file and auto-complete the input with its URL.',
|
|
|
|
short='Upload a file',
|
|
|
|
completion=self.completion_filename)
|
2018-03-08 14:04:59 +00:00
|
|
|
|
2020-04-12 14:45:21 +00:00
|
|
|
async def upload(self, filename) -> Optional[str]:
|
2018-03-08 14:04:59 +00:00
|
|
|
try:
|
2018-07-28 11:31:25 +00:00
|
|
|
url = await self.core.xmpp['xep_0363'].upload_file(filename)
|
2018-10-13 11:12:58 +00:00
|
|
|
except UploadServiceNotFound:
|
|
|
|
self.api.information('HTTP Upload service not found.', 'Error')
|
2020-04-12 14:45:21 +00:00
|
|
|
return None
|
2020-05-11 23:58:37 +00:00
|
|
|
except (FileTooBig, HTTPError) as exn:
|
|
|
|
self.api.information(str(exn), 'Error')
|
|
|
|
return None
|
2018-03-11 18:42:11 +00:00
|
|
|
except Exception:
|
|
|
|
exception = traceback.format_exc()
|
2018-08-15 11:13:17 +00:00
|
|
|
self.api.information('Failed to upload file: %s' % exception,
|
|
|
|
'Error')
|
2020-04-12 14:45:21 +00:00
|
|
|
return None
|
|
|
|
return url
|
|
|
|
|
|
|
|
async def send_upload(self, filename):
|
|
|
|
url = await self.upload(filename)
|
|
|
|
if url is not None:
|
|
|
|
self.embed.embed_image_url(url)
|
2018-03-08 14:04:59 +00:00
|
|
|
|
|
|
|
@command_args_parser.quoted(1)
|
|
|
|
def command_upload(self, args):
|
|
|
|
if args is None:
|
|
|
|
self.core.command.help('upload')
|
|
|
|
return
|
|
|
|
filename, = args
|
|
|
|
filename = expanduser(filename)
|
2020-04-12 14:45:21 +00:00
|
|
|
asyncio.ensure_future(self.send_upload(filename))
|
2018-03-08 14:04:59 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def completion_filename(the_input):
|
|
|
|
txt = expanduser(the_input.get_text()[8:])
|
|
|
|
files = glob(txt + '*')
|
|
|
|
return Completion(the_input.auto_completion, files, quotify=False)
|