summaryrefslogtreecommitdiffstats
path: root/xep_0363.py
blob: 0be9ce49a22276e4fd40887c46955a81df46858f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import logging
import requests

from sleekxmpp import register_stanza_plugin
from sleekxmpp.stanza import Iq
from sleekxmpp.plugins import BasePlugin, register_plugin
from sleekxmpp.xmlstream.handler import Callback
from sleekxmpp.xmlstream.matcher import StanzaPath
from sleekxmpp.plugins.xep_0363 import stanza

log = logging.getLogger(__name__)

class HTTPUploadServiceError(Exception):
    pass

class XEP_0363(BasePlugin):

    name = 'xep_0363'
    description = 'XEP-0363: HTTP File Upload'
    stanza = stanza

    def plugin_init(self):
        register_stanza_plugin(Iq, stanza.SlotRequest)
        register_stanza_plugin(Iq, stanza.UploadSlot)

        self.xmpp.register_handler(
                Callback('HTTP Upload Slot',
                    StanzaPath('iq/slot'), self._handle_upload_slot))

        self._waiting_files = {}

    def plugin_end(self):
        self.xmpp.unregister_handler('HTTP Upload Slot')

    def _handle_upload_slot(self, iq):
        fh = self._waiting_files.pop(iq['id'], None)
        if (iq['type'] == 'result'):
            log.debug("Got UploadSlot for {} ({}) - get: {} put: {}".format(
                self._waiting_files[iq['id']], iq['id'], iq['slot']['get'], iq['slot']['put']))

            if (fh):
                p = requests.put(url=iq['slot']['put'], data=fh)
                if (p.ok):
                    self.xmpp.event('http_upload::{}::finished'.format(iq['id']), iq['slot']['get'])

        elif (iq['type'] == 'error'):
            self.xmpp.event('http_upload::{}::failed'.format(iq['id']), iq)

    def _discover_upload_service(self):
        disco = self.xmpp['xep_0030']

        iq = disco.get_items(jid=self.xmpp.boundjid.host)
        items = iq['disco_items'].get_items()

        for (item, _, _) in items:
            if disco.supports(jid=item, feature=stanza.UploadSlot.namespace):
                return item

        return None

    def _check_file_size(self, upload_service, file_size):
        disco = self.xmpp['xep_0030']

        return True

    def start_upload(self, filename, handle, size, mime=None, upload_service=None):
        if (not upload_service):
            upload_service = self._discover_upload_service()

            if (not upload_service):
                raise HTTPUploadServiceError("Server ({}) does not support HTTP Upload".format(self.xmpp.boundjid.host))

        if (not self._check_file_size(upload_service, size)):
            raise HTTPUploadServiceError("HTTP Upload Service ({}) not suitable, file size to large".format(upload_service))

        req = self.xmpp.make_iq_get(ito=upload_service)

        req['slot_request']['filename'] = filename
        req['slot_request']['size'] = str(size)
        req['slot_request']['content-type'] = mime

        log.debug("Requesting UploadSlot for {} ({} Bytes, {})".format(
            req['slot_request']['filename'], req['slot_request']['size'], req['slot_request']['content-type']))

        self._waiting_files[req['id']] = handle

        req.send(block=False)

        return req['id']