diff 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
line wrap: on
line diff
--- a/libervia.tac	Tue Dec 24 02:00:30 2013 +0100
+++ b/libervia.tac	Tue Dec 17 19:37:47 2013 +0100
@@ -846,7 +846,12 @@
         raise NotImplementedError
 
     def _fileWritten(self, request, filepath):
-        """Called once the file is actually written on disk"""
+        """Called once the file is actually written on disk
+        @param request: HTTP request object
+        @param filepath: full filepath on the server
+        @return: a tuple with the name of the async bridge method
+        to be called followed by its arguments.
+        """
         raise NotImplementedError
 
     def render(self, request):
@@ -866,10 +871,21 @@
 
         with open(filepath,'w') as f:
             f.write(request.args[self.NAME][0])
-        self._fileWritten(request, filepath)
-        #end = time.time()
-        #print "time spent in render: %fs" % (end - start)
-        return "OK"
+
+        def finish(d):
+            #end = time.time()
+            #print "time spent in render: %fs" % (end - start)
+            error = isinstance(d, Exception) or isinstance (d, Failure)
+            request.write('KO' if error else 'OK')
+            # TODO: would be great to re-use the original Exception class and message
+            # but it is lost in the middle of the backtrace and encapsulated within
+            # a DBusException instance --> extract the data from the backtrace?
+            request.finish()
+
+        d = JSONRPCMethodManager(self.sat_host).asyncBridgeCall(*self._fileWritten(request, filepath))
+        d.addCallbacks(lambda d: finish(d), lambda failure: finish(failure))
+        return server.NOT_DONE_YET
+
 
 class UploadManagerRadioCol(UploadManager):
     NAME = 'song'
@@ -878,9 +894,15 @@
         return "%s.ogg" % str(uuid.uuid4()) #XXX: chromium doesn't seem to play song without the .ogg extension, even with audio/ogg mime-type
 
     def _fileWritten(self, request, filepath):
-        """Called once the file is actually written on disk"""
+        """Called once the file is actually written on disk
+        @param request: HTTP request object
+        @param filepath: full filepath on the server
+        @return: a tuple with the name of the async bridge method
+        to be called followed by its arguments.
+        """
         profile = ISATSession(request.getSession()).profile
-        self.sat_host.bridge.radiocolSongAdded(request.args['referee'][0], filepath, profile)
+        return ("radiocolSongAdded", request.args['referee'][0], filepath, profile)
+
 
 class UploadManagerAvatar(UploadManager):
     NAME = 'avatar_path'
@@ -889,10 +911,16 @@
         return str(uuid.uuid4())
 
     def _fileWritten(self, request, filepath):
-        """Called once the file is actually written on disk"""
+        """Called once the file is actually written on disk
+        @param request: HTTP request object
+        @param filepath: full filepath on the server
+        @return: a tuple with the name of the async bridge method
+        to be called followed by its arguments.
+        """
         profile = ISATSession(request.getSession()).profile
-        print u"fichier écrit:", filepath
-        self.sat_host.bridge.setAvatar(filepath, profile)
+        debug("fichier écrit:", filepath)  # /!\ unicode output raises UnicodeDecodeError
+        return ("setAvatar", filepath, profile)
+
 
 class Libervia(service.Service):