Commit 0faed4c3 authored by Philipp Hörist's avatar Philipp Hörist
Browse files

[httpupload] Add File object

parent 2cc6f200
......@@ -179,15 +179,15 @@ class Base(object):
return False
def on_file_dialog_ok(self, widget, jid, chat_control):
path_to_file = widget.get_filename()
path = widget.get_filename()
if not path_to_file or not os.path.exists(path_to_file):
if not path or not os.path.exists(path):
invalid_file = False
if os.path.isfile(path_to_file):
stat = os.stat(path_to_file)
if os.path.isfile(path):
stat = os.stat(path)
if stat[6] == 0:
invalid_file = True
msg = _('File is empty')
......@@ -200,20 +200,26 @@ class Base(object):
encrypted = self.encryption_activated(jid)
filesize = os.path.getsize(path_to_file)
size = os.path.getsize(path)
key, iv = None, None
if encrypted:
filesize += TAGSIZE
key = os.urandom(32)
iv = os.urandom(16)
size += TAGSIZE
mime_type = mimetypes.MimeTypes().guess_type(path_to_file)[0]
if not mime_type:
mime_type = 'application/octet-stream' # fallback mime type"Detected MIME type of file: ", mime_type)
mime = mimetypes.MimeTypes().guess_type(path)[0]
if not mime:
mime = 'application/octet-stream' # fallback mime type"Detected MIME type of file: ", mime)
progress_messages = Queue(8)
progress_window = ProgressWindow(_('HTTP Upload'), _('Requesting HTTP Upload Slot...'),
progress_messages, self.plugin, parent=chat_control.parent_win.window)
self.request_slot(path_to_file, filesize, mime_type, encrypted)
file = File(path=path, size=size, mime=mime, encrypted=encrypted,
key=key, iv=iv, control=chat_control,
def upload_file(stanza):
slot = stanza.getTag("slot")
......@@ -342,21 +348,29 @@ class Base(object):
def request_slot(self, path_to_file, filesize, mime_type, encrypted):
def request_slot(self, file):
iq = nbxmpp.Iq(typ='get', to=self.component)
id_ = gajim.get_an_id()
request = iq.setTag(name="request", namespace=NS_HTTPUPLOAD)
request.addChild('filename', payload=os.path.basename(path_to_file))
request.addChild('size', payload=filesize)
request.addChild('content-type', payload=mime_type)
request.addChild('filename', payload=os.path.basename(file.path))
request.addChild('size', payload=file.size)
request.addChild('content-type', payload=file.mime)"Sending request for slot")
IQ_CALLBACK[id_] = \
lambda stanza: self.upload_file(
stanza, path_to_file, filesize, mime_type, encrypted)
IQ_CALLBACK[id_] = lambda stanza: self.received_slot(stanza, file)
class File:
def __init__(self, **kwargs):
for k, v in kwargs.items():
setattr(self, k, v) = None
self.put = None
self.get = None
class StreamFileWithProgress:
def __init__(self, path, mode, callback=None,
encrypted_upload=False, key=None, iv=None, *args):
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment