jp (file): new file/upload command

This commit is contained in:
Goffi 2015-11-22 17:37:47 +01:00
commit 45d6db1063

View file

@ -38,9 +38,9 @@ class Send(base.CommandBase):
super(Send, self).__init__(host, 'send', use_progress=True, use_verbose=True, help=_('Send a file to a contact'))
def add_parser_options(self):
self.parser.add_argument("files", type=str, nargs = '+', help=_("A list of file"))
self.parser.add_argument("jid", type=base.unicode_decoder, help=_("The destination jid"))
self.parser.add_argument("-b", "--bz2", action="store_true", help=_("Make a bzip2 tarball"))
self.parser.add_argument("files", type=str, nargs='+', metavar='file', help=_("a list of file"))
self.parser.add_argument("jid", type=base.unicode_decoder, help=_("the destination jid"))
self.parser.add_argument("-b", "--bz2", action="store_true", help=_("make a bzip2 tarball"))
def connected(self):
"""Send files to jabber contact"""
@ -215,8 +215,75 @@ class Receive(base.CommandAnswering):
self.disp(_(u"waiting for incoming file request"),2)
class Upload(base.CommandBase):
def __init__(self, host):
super(Upload, self).__init__(host, 'upload', use_progress=True, use_verbose=True, help=_('Upload a file'))
def add_parser_options(self):
self.parser.add_argument("file", type=str, help=_("file to upload"))
self.parser.add_argument("jid", type=base.unicode_decoder, nargs='?', help=_("jid of upload component (nothing to autodetect)"))
self.parser.add_argument("--ignore-tls-errors", action="store_true", help=_("ignore invalide TLS certificate"))
def connected(self):
"""Send files to jabber contact"""
self.need_loop=True
super(Upload, self).connected()
self.uploadFile()
def onProgressStarted(self, metadata):
self.disp(_(u'File upload started'),2)
def onProgressFinished(self, metadata):
self.disp(_(u'File uploaded successfully'),2)
try:
url = metadata['url']
except KeyError:
self.disp(u'download URL not found in metadata')
else:
self.disp(_(u'URL to retrieve the file:'),1)
# XXX: url is display alone on a line to make parsing easier
self.disp(url)
def onProgressError(self, error_msg):
self.disp(_(u'Error while uploading file: {}').format(error_msg),error=True)
def gotId(self, data, file_):
"""Called when a progress id has been received
@param pid(unicode): progress id
@param file_(str): file path
"""
try:
self.progress_id = data['progress']
except KeyError:
# TODO: if 'xmlui' key is present, manage xmlui message display
self.disp(_(u"Can't upload file"), error=True)
self.host.quit(2)
def error(self, failure):
self.disp(_("Error while trying to upload a file: {reason}").format(reason=failure), error=True)
self.host.quit(1)
def uploadFile(self):
file_ = self.args.file
if not os.path.exists(file_):
self.disp(_(u"file [{}] doesn't exist !").format(file_), error=True)
self.host.quit(1)
if os.path.isdir(file_):
self.disp(_(u"[{}] is a dir! Can't upload a dir").format(file_))
self.host.quit(1)
self.full_dest_jid = self.host.get_full_jid(self.args.jid) if self.args.jid is not None else ''
options = {}
if self.args.ignore_tls_errors:
options['ignore-tls-errors'] = C.BOOL_TRUE
path = os.path.abspath(file_)
self.host.bridge.fileUpload(path, '', self.full_dest_jid, options, self.profile, callback=lambda pid, file_=file_: self.gotId(pid, file_), errback=self.error)
class File(base.CommandBase):
subcommands = (Send, Receive)
subcommands = (Send, Receive, Upload)
def __init__(self, host):
super(File, self).__init__(host, 'file', use_profile=False, help=_('File sending/receiving'))