plugins radiocol, xep-0054: better handling of upload errors:

- radiocolSongAdded is now asynchronous
- _buildAvatar return a Failure if the image is not readable
This commit is contained in:
souliane 2013-12-17 18:44:53 +01:00
commit b97f78ca93
2 changed files with 8 additions and 16 deletions

View file

@ -21,7 +21,8 @@ from logging import debug, info, warning, error
from twisted.words.xish import domish
from twisted.internet import reactor
from twisted.words.protocols.jabber import jid
from twisted.internet import threads, defer
from sat.core import exceptions
import os.path
import copy
import time
@ -65,7 +66,7 @@ class Radiocol(object):
self.host = host
host.bridge.addMethod("radiocolLaunch", ".plugin", in_sign='asss', out_sign='', method=self.prepareRoom)
host.bridge.addMethod("radiocolCreate", ".plugin", in_sign='sass', out_sign='', method=self.createGame)
host.bridge.addMethod("radiocolSongAdded", ".plugin", in_sign='sss', out_sign='', method=self.radiocolSongAdded)
host.bridge.addMethod("radiocolSongAdded", ".plugin", in_sign='sss', out_sign='', method=self.radiocolSongAdded, async=True)
host.bridge.addSignal("radiocolPlayers", ".plugin", signature='ssass') # room_jid, referee, players, profile
host.bridge.addSignal("radiocolStarted", ".plugin", signature='ssasais') # room_jid, referee, players, [QUEUE_TO_START, QUEUE_LIMIT], profile
host.bridge.addSignal("radiocolSongRejected", ".plugin", signature='sss') # room_jid, reason, profile
@ -94,32 +95,22 @@ class Radiocol(object):
# check data. Referee will have to parse the song himself to check it
client = self.host.getClient(profile)
if not client:
error(_("Can't access profile's data"))
return
raise exceptions.NotConnectedProfileError(_("Can't access profile's data"))
try:
song = OggVorbis(song_path)
except OggVorbisHeaderError:
#this file is not ogg vorbis, we reject it
self.deleteFile(song_path) # FIXME: same host trick (see note above)
self.host.bridge.radiocolSongRejected(jid.JID(referee).userhost(),
"Uploaded file is not Ogg Vorbis song, only Ogg Vorbis songs are acceptable", profile)
"""mess = self.createGameElt(jid.JID(referee))
reject_elt = mess.firstChildElement().addElement(('','song_rejected'))
reject_elt['sender'] = client.jid
reject_elt['reason'] = "Uploaded file is not Ogg Vorbis song, only Ogg Vorbis songs are acceptable"
#FIXME: add an error code
self.host.profiles[profile].xmlstream.send(mess)"""
return
return defer.fail(exceptions.DataError("Uploaded file is not Ogg Vorbis song, only Ogg Vorbis songs are acceptable"))
attrs = {'filename': os.path.basename(song_path),
'title': song.get("title", ["Unknown"])[0],
'artist': song.get("artist", ["Unknown"])[0],
'album': song.get("album", ["Unknown"])[0],
'length': str(song.info.length)
}
self.send(jid.JID(referee), ('', 'song_added'), attrs, profile=profile)
radio_data = self.games[jid.JID(referee).userhost()] # FIXME: referee comes from Libervia's client side, it's unsecure
radio_data['to_delete'][attrs['filename']] = song_path # FIXME: works only because of the same host trick, see the note under the docstring
return threads.deferToThread(self.send, jid.JID(referee), ('', 'song_added'), attrs, profile=profile)
def playNext(self, room_jid, profile):
""""Play next song in queue if exists, and put a timer

View file

@ -23,6 +23,7 @@ from twisted.internet.defer import inlineCallbacks, returnValue
from twisted.words.protocols.jabber import jid
from twisted.words.protocols.jabber.xmlstream import IQ
from twisted.words.xish import domish
from twisted.python.failure import Failure
import os.path
from zope.interface import implements
@ -236,7 +237,7 @@ class XEP_0054(object):
try:
img = Image.open(filepath)
except IOError:
raise exceptions.DataError("Can't open image")
return Failure(exceptions.DataError("Can't open image"))
if img.size != (64, 64):
img = img.resize((64, 64))