code style reformatting using black
This commit is contained in:
parent
4127bcffd0
commit
744cb0c503
1 changed files with 21394 additions and 10488 deletions
|
|
@ -20,6 +20,7 @@
|
|||
from sat.core.i18n import _
|
||||
from sat.core.constants import Const as C
|
||||
from sat.core.log import getLogger
|
||||
|
||||
log = getLogger(__name__)
|
||||
from sat.core import exceptions
|
||||
from wokkel import disco, iwokkel
|
||||
|
|
@ -49,18 +50,17 @@ PLUGIN_INFO = {
|
|||
C.PI_DEPENDENCIES: ["FILE", "UPLOAD"],
|
||||
C.PI_MAIN: "XEP_0363",
|
||||
C.PI_HANDLER: "yes",
|
||||
C.PI_DESCRIPTION: _("""Implementation of HTTP File Upload""")
|
||||
C.PI_DESCRIPTION: _("""Implementation of HTTP File Upload"""),
|
||||
}
|
||||
|
||||
NS_HTTP_UPLOAD = 'urn:xmpp:http:upload'
|
||||
NS_HTTP_UPLOAD = "urn:xmpp:http:upload"
|
||||
|
||||
|
||||
Slot = namedtuple('Slot', ['put', 'get'])
|
||||
Slot = namedtuple("Slot", ["put", "get"])
|
||||
|
||||
|
||||
@implementer(IOpenSSLClientConnectionCreator)
|
||||
class NoCheckConnectionCreator(object):
|
||||
|
||||
def __init__(self, hostname, ctx):
|
||||
self._ctx = ctx
|
||||
|
||||
|
|
@ -80,53 +80,91 @@ class NoCheckContextFactory(ssl.ClientContextFactory):
|
|||
"""
|
||||
|
||||
def creatorForNetloc(self, hostname, port):
|
||||
log.warning(u"TLS check disabled for {host} on port {port}".format(host=hostname, port=port))
|
||||
log.warning(
|
||||
u"TLS check disabled for {host} on port {port}".format(
|
||||
host=hostname, port=port
|
||||
)
|
||||
)
|
||||
certificateOptions = ssl.CertificateOptions(trustRoot=None)
|
||||
return NoCheckConnectionCreator(hostname, certificateOptions.getContext())
|
||||
|
||||
|
||||
class XEP_0363(object):
|
||||
|
||||
def __init__(self, host):
|
||||
log.info(_("plugin HTTP File Upload initialization"))
|
||||
self.host = host
|
||||
host.bridge.addMethod("fileHTTPUpload", ".plugin", in_sign='sssbs', out_sign='', method=self._fileHTTPUpload)
|
||||
host.bridge.addMethod("fileHTTPUploadGetSlot", ".plugin", in_sign='sisss', out_sign='(ss)', method=self._getSlot, async=True)
|
||||
host.plugins['UPLOAD'].register(u"HTTP Upload", self.getHTTPUploadEntity, self.fileHTTPUpload)
|
||||
host.bridge.addMethod(
|
||||
"fileHTTPUpload",
|
||||
".plugin",
|
||||
in_sign="sssbs",
|
||||
out_sign="",
|
||||
method=self._fileHTTPUpload,
|
||||
)
|
||||
host.bridge.addMethod(
|
||||
"fileHTTPUploadGetSlot",
|
||||
".plugin",
|
||||
in_sign="sisss",
|
||||
out_sign="(ss)",
|
||||
method=self._getSlot,
|
||||
async=True,
|
||||
)
|
||||
host.plugins["UPLOAD"].register(
|
||||
u"HTTP Upload", self.getHTTPUploadEntity, self.fileHTTPUpload
|
||||
)
|
||||
|
||||
def getHandler(self, client):
|
||||
return XEP_0363_handler()
|
||||
|
||||
@defer.inlineCallbacks
|
||||
def getHTTPUploadEntity(self, upload_jid=None, profile=C.PROF_KEY_NONE):
|
||||
"""Get HTTP upload capable entity
|
||||
"""Get HTTP upload capable entity
|
||||
|
||||
upload_jid is checked, then its components
|
||||
@param upload_jid(None, jid.JID): entity to check
|
||||
@return(D(jid.JID)): first HTTP upload capable entity
|
||||
@raise exceptions.NotFound: no entity found
|
||||
"""
|
||||
client = self.host.getClient(profile)
|
||||
try:
|
||||
entity = client.http_upload_service
|
||||
except AttributeError:
|
||||
found_entities = yield self.host.findFeaturesSet(client, (NS_HTTP_UPLOAD,))
|
||||
try:
|
||||
entity = client.http_upload_service = iter(found_entities).next()
|
||||
except StopIteration:
|
||||
entity = client.http_upload_service = None
|
||||
client = self.host.getClient(profile)
|
||||
try:
|
||||
entity = client.http_upload_service
|
||||
except AttributeError:
|
||||
found_entities = yield self.host.findFeaturesSet(client, (NS_HTTP_UPLOAD,))
|
||||
try:
|
||||
entity = client.http_upload_service = iter(found_entities).next()
|
||||
except StopIteration:
|
||||
entity = client.http_upload_service = None
|
||||
|
||||
if entity is None:
|
||||
raise failure.Failure(exceptions.NotFound(u'No HTTP upload entity found'))
|
||||
if entity is None:
|
||||
raise failure.Failure(exceptions.NotFound(u"No HTTP upload entity found"))
|
||||
|
||||
defer.returnValue(entity)
|
||||
defer.returnValue(entity)
|
||||
|
||||
def _fileHTTPUpload(self, filepath, filename='', upload_jid='', ignore_tls_errors=False, profile=C.PROF_KEY_NONE):
|
||||
def _fileHTTPUpload(
|
||||
self,
|
||||
filepath,
|
||||
filename="",
|
||||
upload_jid="",
|
||||
ignore_tls_errors=False,
|
||||
profile=C.PROF_KEY_NONE,
|
||||
):
|
||||
assert os.path.isabs(filepath) and os.path.isfile(filepath)
|
||||
progress_id_d, dummy = self.fileHTTPUpload(filepath, filename or None, jid.JID(upload_jid) if upload_jid else None, {'ignore_tls_errors': ignore_tls_errors}, profile)
|
||||
progress_id_d, dummy = self.fileHTTPUpload(
|
||||
filepath,
|
||||
filename or None,
|
||||
jid.JID(upload_jid) if upload_jid else None,
|
||||
{"ignore_tls_errors": ignore_tls_errors},
|
||||
profile,
|
||||
)
|
||||
return progress_id_d
|
||||
|
||||
def fileHTTPUpload(self, filepath, filename=None, upload_jid=None, options=None, profile=C.PROF_KEY_NONE):
|
||||
def fileHTTPUpload(
|
||||
self,
|
||||
filepath,
|
||||
filename=None,
|
||||
upload_jid=None,
|
||||
options=None,
|
||||
profile=C.PROF_KEY_NONE,
|
||||
):
|
||||
"""upload a file through HTTP
|
||||
|
||||
@param filepath(str): absolute path of the file
|
||||
|
|
@ -141,14 +179,20 @@ class XEP_0363(object):
|
|||
"""
|
||||
if options is None:
|
||||
options = {}
|
||||
ignore_tls_errors = options.get('ignore_tls_errors', False)
|
||||
ignore_tls_errors = options.get("ignore_tls_errors", False)
|
||||
client = self.host.getClient(profile)
|
||||
filename = filename or os.path.basename(filepath)
|
||||
size = os.path.getsize(filepath)
|
||||
progress_id_d = defer.Deferred()
|
||||
download_d = defer.Deferred()
|
||||
d = self.getSlot(client, filename, size, upload_jid=upload_jid)
|
||||
d.addCallbacks(self._getSlotCb, self._getSlotEb, (client, progress_id_d, download_d, filepath, size, ignore_tls_errors), None, (client, progress_id_d, download_d))
|
||||
d.addCallbacks(
|
||||
self._getSlotCb,
|
||||
self._getSlotEb,
|
||||
(client, progress_id_d, download_d, filepath, size, ignore_tls_errors),
|
||||
None,
|
||||
(client, progress_id_d, download_d),
|
||||
)
|
||||
return progress_id_d, download_d
|
||||
|
||||
def _getSlotEb(self, fail, client, progress_id_d, download_d):
|
||||
|
|
@ -157,7 +201,9 @@ class XEP_0363(object):
|
|||
progress_id_d.errback(fail)
|
||||
download_d.errback(fail)
|
||||
|
||||
def _getSlotCb(self, slot, client, progress_id_d, download_d, path, size, ignore_tls_errors=False):
|
||||
def _getSlotCb(
|
||||
self, slot, client, progress_id_d, download_d, path, size, ignore_tls_errors=False
|
||||
):
|
||||
"""Called when slot is received, try to do the upload
|
||||
|
||||
@param slot(Slot): slot instance with the get and put urls
|
||||
|
|
@ -169,15 +215,28 @@ class XEP_0363(object):
|
|||
@return (tuple
|
||||
"""
|
||||
log.debug(u"Got upload slot: {}".format(slot))
|
||||
sat_file = self.host.plugins['FILE'].File(self.host, client, path, size=size, auto_end_signals=False)
|
||||
sat_file = self.host.plugins["FILE"].File(
|
||||
self.host, client, path, size=size, auto_end_signals=False
|
||||
)
|
||||
progress_id_d.callback(sat_file.uid)
|
||||
file_producer = http_client.FileBodyProducer(sat_file)
|
||||
if ignore_tls_errors:
|
||||
agent = http_client.Agent(reactor, NoCheckContextFactory())
|
||||
else:
|
||||
agent = http_client.Agent(reactor)
|
||||
d = agent.request('PUT', slot.put.encode('utf-8'), http_headers.Headers({'User-Agent': [C.APP_NAME.encode('utf-8')]}), file_producer)
|
||||
d.addCallbacks(self._uploadCb, self._uploadEb, (sat_file, slot, download_d), None, (sat_file, download_d))
|
||||
d = agent.request(
|
||||
"PUT",
|
||||
slot.put.encode("utf-8"),
|
||||
http_headers.Headers({"User-Agent": [C.APP_NAME.encode("utf-8")]}),
|
||||
file_producer,
|
||||
)
|
||||
d.addCallbacks(
|
||||
self._uploadCb,
|
||||
self._uploadEb,
|
||||
(sat_file, slot, download_d),
|
||||
None,
|
||||
(sat_file, download_d),
|
||||
)
|
||||
return d
|
||||
|
||||
def _uploadCb(self, dummy, sat_file, slot, download_d):
|
||||
|
|
@ -188,7 +247,7 @@ class XEP_0363(object):
|
|||
@param slot(Slot): put/get urls
|
||||
"""
|
||||
log.info(u"HTTP upload finished")
|
||||
sat_file.progressFinished({'url': slot.get})
|
||||
sat_file.progressFinished({"url": slot.get})
|
||||
download_d.callback(slot.get)
|
||||
|
||||
def _uploadEb(self, fail, sat_file, download_d):
|
||||
|
|
@ -216,15 +275,17 @@ class XEP_0363(object):
|
|||
@param iq_elt(domish.Element): <IQ/> result as specified in XEP-0363
|
||||
"""
|
||||
try:
|
||||
slot_elt = iq_elt.elements(NS_HTTP_UPLOAD, 'slot').next()
|
||||
put_url = unicode(slot_elt.elements(NS_HTTP_UPLOAD, 'put').next())
|
||||
get_url = unicode(slot_elt.elements(NS_HTTP_UPLOAD, 'get').next())
|
||||
slot_elt = iq_elt.elements(NS_HTTP_UPLOAD, "slot").next()
|
||||
put_url = unicode(slot_elt.elements(NS_HTTP_UPLOAD, "put").next())
|
||||
get_url = unicode(slot_elt.elements(NS_HTTP_UPLOAD, "get").next())
|
||||
except StopIteration:
|
||||
raise exceptions.DataError(u"Incorrect stanza received from server")
|
||||
slot = Slot(put=put_url, get=get_url)
|
||||
return slot
|
||||
|
||||
def _getSlot(self, filename, size, content_type, upload_jid, profile_key=C.PROF_KEY_NONE):
|
||||
def _getSlot(
|
||||
self, filename, size, content_type, upload_jid, profile_key=C.PROF_KEY_NONE
|
||||
):
|
||||
"""Get a upload slot
|
||||
|
||||
This method can be used when uploading is done by the frontend
|
||||
|
|
@ -234,9 +295,11 @@ class XEP_0363(object):
|
|||
@param content_type(unicode, None): MIME type of the content
|
||||
empty string or None to guess automatically
|
||||
"""
|
||||
filename = filename.replace('/', '_')
|
||||
filename = filename.replace("/", "_")
|
||||
client = self.host.getClient(profile_key)
|
||||
return self.getSlot(client, filename, size, content_type or None, upload_jid or None)
|
||||
return self.getSlot(
|
||||
client, filename, size, content_type or None, upload_jid or None
|
||||
)
|
||||
|
||||
def getSlot(self, client, filename, size, content_type=None, upload_jid=None):
|
||||
"""Get a slot (i.e. download/upload links)
|
||||
|
|
@ -261,19 +324,25 @@ class XEP_0363(object):
|
|||
upload_jid = client.http_upload_service
|
||||
except AttributeError:
|
||||
d = self.getHTTPUploadEntity(profile=client.profile)
|
||||
d.addCallback(lambda found_entity: self.getSlot(client, filename, size, content_type, found_entity))
|
||||
d.addCallback(
|
||||
lambda found_entity: self.getSlot(
|
||||
client, filename, size, content_type, found_entity
|
||||
)
|
||||
)
|
||||
return d
|
||||
else:
|
||||
if upload_jid is None:
|
||||
raise failure.Failure(exceptions.NotFound(u'No HTTP upload entity found'))
|
||||
raise failure.Failure(
|
||||
exceptions.NotFound(u"No HTTP upload entity found")
|
||||
)
|
||||
|
||||
iq_elt = client.IQ('get')
|
||||
iq_elt['to'] = upload_jid.full()
|
||||
request_elt = iq_elt.addElement((NS_HTTP_UPLOAD, 'request'))
|
||||
request_elt.addElement('filename', content=filename)
|
||||
request_elt.addElement('size', content=unicode(size))
|
||||
iq_elt = client.IQ("get")
|
||||
iq_elt["to"] = upload_jid.full()
|
||||
request_elt = iq_elt.addElement((NS_HTTP_UPLOAD, "request"))
|
||||
request_elt.addElement("filename", content=filename)
|
||||
request_elt.addElement("size", content=unicode(size))
|
||||
if content_type is not None:
|
||||
request_elt.addElement('content-type', content=content_type)
|
||||
request_elt.addElement("content-type", content=content_type)
|
||||
|
||||
d = iq_elt.send()
|
||||
d.addCallback(self._gotSlot, client)
|
||||
|
|
@ -284,8 +353,8 @@ class XEP_0363(object):
|
|||
class XEP_0363_handler(XMPPHandler):
|
||||
implements(iwokkel.IDisco)
|
||||
|
||||
def getDiscoInfo(self, requestor, target, nodeIdentifier=''):
|
||||
def getDiscoInfo(self, requestor, target, nodeIdentifier=""):
|
||||
return [disco.DiscoFeature(NS_HTTP_UPLOAD)]
|
||||
|
||||
def getDiscoItems(self, requestor, target, nodeIdentifier=''):
|
||||
def getDiscoItems(self, requestor, target, nodeIdentifier=""):
|
||||
return []
|
||||
|
|
|
|||
Loading…
Reference in a new issue