comparison libervia.tac @ 300:4f221f34bdc7

browser_side (plugins radiocol, xep-0054): handle upload errors
author souliane <souliane@mailoo.org>
date Tue, 17 Dec 2013 19:37:47 +0100
parents e4f586fc6101
children 0ca441ba4317
comparison
equal deleted inserted replaced
299:e4f586fc6101 300:4f221f34bdc7
844 def _getFileName(self, request): 844 def _getFileName(self, request):
845 """Generate unique filename for a file""" 845 """Generate unique filename for a file"""
846 raise NotImplementedError 846 raise NotImplementedError
847 847
848 def _fileWritten(self, request, filepath): 848 def _fileWritten(self, request, filepath):
849 """Called once the file is actually written on disk""" 849 """Called once the file is actually written on disk
850 @param request: HTTP request object
851 @param filepath: full filepath on the server
852 @return: a tuple with the name of the async bridge method
853 to be called followed by its arguments.
854 """
850 raise NotImplementedError 855 raise NotImplementedError
851 856
852 def render(self, request): 857 def render(self, request):
853 """ 858 """
854 Render method with some hacks: 859 Render method with some hacks:
864 # be written in the futur. In addition, it is not yet possible to get progression informations 869 # be written in the futur. In addition, it is not yet possible to get progression informations
865 # (see http://twistedmatrix.com/trac/ticket/288) 870 # (see http://twistedmatrix.com/trac/ticket/288)
866 871
867 with open(filepath,'w') as f: 872 with open(filepath,'w') as f:
868 f.write(request.args[self.NAME][0]) 873 f.write(request.args[self.NAME][0])
869 self._fileWritten(request, filepath) 874
870 #end = time.time() 875 def finish(d):
871 #print "time spent in render: %fs" % (end - start) 876 #end = time.time()
872 return "OK" 877 #print "time spent in render: %fs" % (end - start)
878 error = isinstance(d, Exception) or isinstance (d, Failure)
879 request.write('KO' if error else 'OK')
880 # TODO: would be great to re-use the original Exception class and message
881 # but it is lost in the middle of the backtrace and encapsulated within
882 # a DBusException instance --> extract the data from the backtrace?
883 request.finish()
884
885 d = JSONRPCMethodManager(self.sat_host).asyncBridgeCall(*self._fileWritten(request, filepath))
886 d.addCallbacks(lambda d: finish(d), lambda failure: finish(failure))
887 return server.NOT_DONE_YET
888
873 889
874 class UploadManagerRadioCol(UploadManager): 890 class UploadManagerRadioCol(UploadManager):
875 NAME = 'song' 891 NAME = 'song'
876 892
877 def _getFileName(self, request): 893 def _getFileName(self, request):
878 return "%s.ogg" % str(uuid.uuid4()) #XXX: chromium doesn't seem to play song without the .ogg extension, even with audio/ogg mime-type 894 return "%s.ogg" % str(uuid.uuid4()) #XXX: chromium doesn't seem to play song without the .ogg extension, even with audio/ogg mime-type
879 895
880 def _fileWritten(self, request, filepath): 896 def _fileWritten(self, request, filepath):
881 """Called once the file is actually written on disk""" 897 """Called once the file is actually written on disk
898 @param request: HTTP request object
899 @param filepath: full filepath on the server
900 @return: a tuple with the name of the async bridge method
901 to be called followed by its arguments.
902 """
882 profile = ISATSession(request.getSession()).profile 903 profile = ISATSession(request.getSession()).profile
883 self.sat_host.bridge.radiocolSongAdded(request.args['referee'][0], filepath, profile) 904 return ("radiocolSongAdded", request.args['referee'][0], filepath, profile)
905
884 906
885 class UploadManagerAvatar(UploadManager): 907 class UploadManagerAvatar(UploadManager):
886 NAME = 'avatar_path' 908 NAME = 'avatar_path'
887 909
888 def _getFileName(self, request): 910 def _getFileName(self, request):
889 return str(uuid.uuid4()) 911 return str(uuid.uuid4())
890 912
891 def _fileWritten(self, request, filepath): 913 def _fileWritten(self, request, filepath):
892 """Called once the file is actually written on disk""" 914 """Called once the file is actually written on disk
915 @param request: HTTP request object
916 @param filepath: full filepath on the server
917 @return: a tuple with the name of the async bridge method
918 to be called followed by its arguments.
919 """
893 profile = ISATSession(request.getSession()).profile 920 profile = ISATSession(request.getSession()).profile
894 print u"fichier écrit:", filepath 921 debug("fichier écrit:", filepath) # /!\ unicode output raises UnicodeDecodeError
895 self.sat_host.bridge.setAvatar(filepath, profile) 922 return ("setAvatar", filepath, profile)
923
896 924
897 class Libervia(service.Service): 925 class Libervia(service.Service):
898 926
899 def __init__(self): 927 def __init__(self):
900 self._cleanup = [] 928 self._cleanup = []