gmyth-stream/server/0.3/lib/request_handler.py
branchtrunk
changeset 801 e51af4d0caf5
parent 795 2b4e9d183146
child 802 d0e8c542c38e
     1.1 --- a/gmyth-stream/server/0.3/lib/request_handler.py	Fri Aug 03 21:43:42 2007 +0100
     1.2 +++ b/gmyth-stream/server/0.3/lib/request_handler.py	Mon Aug 13 22:54:55 2007 +0100
     1.3 @@ -13,6 +13,7 @@
     1.4  import threading
     1.5  import SocketServer
     1.6  import BaseHTTPServer
     1.7 +import mimetypes
     1.8  
     1.9  import lib.utils as utils
    1.10  import lib.file_handler as files
    1.11 @@ -64,6 +65,7 @@
    1.12              "/get_log.do": self.serve_get_log,
    1.13              "/get_all_log.do": self.serve_get_all_log,
    1.14              "/stream.do": self.serve_stream,
    1.15 +            "/transcode.do": self.serve_transcode,
    1.16              "/list.do": self.serve_list,
    1.17              "/get_file_info.do": self.serve_file_info,
    1.18              }
    1.19 @@ -353,6 +355,46 @@
    1.20      # serve_file_info()
    1.21  
    1.22      def serve_stream(self, body):
    1.23 +        filename = self.query.get("uri", None)[0]
    1.24 +
    1.25 +        if not os.path.exists (filename):
    1.26 +            self.send_error(404, "File not found")
    1.27 +            return
    1.28 +
    1.29 +        size = int(os.path.getsize(filename))
    1.30 +        self.send_response(200)
    1.31 +        self.send_header("Content-Type", mimetypes.guess_type(filename)[0])
    1.32 +        self.send_header("Cache-Control","no-cache")
    1.33 +        self.send_header("Content-Length", size)
    1.34 +        #self.send_header("Connection", "close")
    1.35 +        self.end_headers()
    1.36 +
    1.37 +        media = open(filename)
    1.38 +        data_in = " "
    1.39 +        total_read = 0
    1.40 +
    1.41 +        test_tid = int(self.query.get("tid", "0")[0])
    1.42 +        if test_tid == 0 or test_tid not in self.tid_queue:
    1.43 +            test_tid = self._get_new_id(self.server.last_tid)
    1.44 +
    1.45 +        self.transcoders_log.insert(test_tid, "gms.Stream: %s" % filename)
    1.46 +
    1.47 +        try:
    1.48 +            while data_in != "":
    1.49 +                data_in = media.read(4096)
    1.50 +                total_read += 4096
    1.51 +                self.wfile.write(data_in)
    1.52 +                status = utils.progress_bar(total_read, size, 50)
    1.53 +                msg_status = "Status:%s:%s%%" % (test_tid, status)
    1.54 +                self.transcoders_log._update_status(test_tid, msg_status)
    1.55 +                self.transcoders_log._update_status(test_tid, "OK: Done")
    1.56 +
    1.57 +        except Exception, e:
    1.58 +            self.log.error("Stream error: %s" %e)
    1.59 +            self.transcoders_log._update_status(test_tid, "Error: %s" % e)
    1.60 +    # serve_stream()
    1.61 +
    1.62 +    def serve_transcode(self, body):
    1.63          transcoder = self._get_transcoder()
    1.64          try:
    1.65              obj = transcoder(self.query)
    1.66 @@ -362,7 +404,7 @@
    1.67  
    1.68          self.send_response(200)
    1.69          self.send_header("Content-Type", obj.get_mimetype())
    1.70 -	self.send_header("Cache-Control","no-cache")
    1.71 +        self.send_header("Cache-Control","no-cache")
    1.72  
    1.73          if (obj.name == "gmencoder"):
    1.74              self.send_header("Transfer-Encoding", "chunked")
    1.75 @@ -371,12 +413,10 @@
    1.76          self.end_headers()
    1.77  
    1.78          if body:
    1.79 -
    1.80              test_tid = int(self.query.get("tid", "0")[0])
    1.81              if test_tid == 0 or test_tid not in self.tid_queue:
    1.82                  test_tid = self._get_new_id(self.server.last_tid)
    1.83  
    1.84 -
    1.85              if self.query.get("transcoder", None):
    1.86                  self.transcoders_log.insert(test_tid, "gms.%s" % obj.name)
    1.87                  obj.tid = test_tid
    1.88 @@ -387,33 +427,6 @@
    1.89                  self.server.del_transcoders(self, obj)
    1.90                  files.TranscodedFile("", self.query)
    1.91  
    1.92 -            elif self.query.get("type", "")[0] == "file" and \
    1.93 -                     self.query.get("uri", None):
    1.94 -
    1.95 -                filename = self.query.get("uri", None)[0]
    1.96 -                self.transcoders_log.insert(test_tid, "gms.%s" % filename)
    1.97 -
    1.98 -                try:
    1.99 -                    media = open(filename)
   1.100 -                    data_in = " "
   1.101 -                    total_read = 0
   1.102 -                    size = int(os.path.getsize(filename))
   1.103 -
   1.104 -                    while data_in != "":
   1.105 -                        data_in = media.read(4096)
   1.106 -                        total_read += 4096
   1.107 -                        self.wfile.write(data_in)
   1.108 -                        status = utils.progress_bar(total_read, size, 50)
   1.109 -                        msg_status = "Status:%s:%s%%" % (test_tid, status)
   1.110 -                        self.transcoders_log._update_status(test_tid,
   1.111 -                                                            msg_status)
   1.112 -
   1.113 -                    self.transcoders_log._update_status(test_tid, "OK: Done")
   1.114 -
   1.115 -                except Exception, e:
   1.116 -                    self.log.error("Stream error: %s" %e)
   1.117 -                    self.transcoders_log._update_status(test_tid,
   1.118 -                                                        "Error: %s" % e)
   1.119      # serve_stream()
   1.120  
   1.121