set and retrieve information about the files transfered

This commit is contained in:
Jefry Lagrange 2012-08-29 15:06:04 -04:00
parent abd850e298
commit b20e054d55
5 changed files with 47 additions and 7 deletions

View File

@ -116,6 +116,7 @@ class FileProp(object):
# full file path
self.file_name = None
self.name = None
self.date = None
self.desc = None
self.offset = None
self.sender = None

View File

@ -55,6 +55,7 @@ class ConnectionJingle(object):
# dictionary: (jid, iq stanza id) => JingleSession object,
# one time callbacks
self.__iq_responses = {}
self.files = []
def delete_jingle_session(self, sid):
"""
@ -192,6 +193,20 @@ class ConnectionJingle(object):
else:
return sessions
def set_files_info(self, file_):
# Saves information about the files we have transfered in case they need
# to be requested again.
self.files.append(file_)
def get_files_info(self, hash_=None, name=None):
if hash_:
for f in self.files:
if f['hash'] == hash_:
return f
elif name:
for f in self.files:
if f['name'] == name:
return f
def get_jingle_session(self, jid, sid=None, media=None):
if sid:

View File

@ -165,12 +165,10 @@ class JingleContent(object):
def _fill_content(self, content):
description_node = xmpp.simplexml.Node(
tag=xmpp.NS_JINGLE_FILE_TRANSFER + ' description')
if self.session.werequest:
simode = xmpp.simplexml.Node(tag='request')
else:
simode = xmpp.simplexml.Node(tag='offer')
file_tag = simode.setTag('file', namespace=xmpp.NS_FILE)
if self.file_props.name:
node = xmpp.simplexml.Node(tag='name')
@ -188,15 +186,20 @@ class JingleContent(object):
else:
# if the file is less than 10 mb, then it is small
# lets calculate it right away
if int(self.file_props.size) < 10000000:
if int(self.file_props.size) < 10000000 and not \
self.file_props.hash_:
h = self._calcHash()
file_tag.addChild(node=h)
file_info = {'name' : self.file_props.name,
'hash' : self.file_props.hash_,
'size' : self.file_props.size,
'date' : self.file_props.date
}
self.session.connection.set_files_info(file_info)
desc = file_tag.setTag('desc')
if self.file_props.desc:
desc.setData(self.file_props.desc)
description_node.addChild(node=simode)
if self.use_security:
security = xmpp.simplexml.Node(
tag=xmpp.NS_JINGLE_XTLS + ' security')
@ -206,10 +209,8 @@ class JingleContent(object):
method.setAttr('name', m)
security.addChild(node=method)
content.addChild(node=security)
content.addChild(node=description_node)
def destroy(self):
self.callbacks = None
del self.session.contents[(self.creator, self.name)]

View File

@ -128,6 +128,13 @@ class JingleFileTransfer(JingleContent):
payload=[self._calcHash()])])
checksum.setNamespace(xmpp.NS_JINGLE_FILE_TRANSFER)
self.session.__session_info(checksum )
file_info = {'name' : self.file_props.name,
'hash' : self.file_props.hash_,
'size' : self.file_props.size,
'date' : self.file_props.date
}
self.session.connection.set_files_info(file_info)
def _calcHash(self):
# Caculates the hash and returns a xep-300 hash stanza

View File

@ -511,6 +511,22 @@ class JingleSession(object):
self.__ack(stanza, jingle, error, action)
self._session_terminate(reason)
raise xmpp.NodeProcessed
else:
# Stop if we don't have the requested file
request = \
jingle.getTag('content').getTag('description').getTag('request')
if request:
h = request.getTag('file').getTag('hash')
n = request.getTag('file').getTag('name')
if h:
file_info = self.connection.get_files_info(hash_=h)
elif n:
file_info = self.connection.get_files_info(name=n)
if not file_info:
# Send 404 error?
log.warning('The peer ' + self.peerjid + \
' is requesting a ' + \
'file that we dont have')
# If there's no content we understand...
if not contents:
# TODO: http://xmpp.org/extensions/xep-0166.html#session-terminate