add self.default_uploaddir to form
[enigma2-plugins.git] / webinterface / src / WebChilds / MovieStreamer.py
1 from twisted.web2 import resource, stream, responsecode, http, http_headers
2 import os
3
4 class MovieStreamer(resource.Resource):
5     addSlash = True
6     
7     def render(self, req):
8         class myFileStream(stream.FileStream):
9             """
10                 because os.fstat(f.fileno()).st_size returns negative values on 
11                 large file, we set read() to a fix value
12             """
13             readsize = 10000    
14             
15             def read(self, sendfile=False):
16                 if self.f is None:
17                     return None
18         
19                 length = self.length
20                 if length == 0:
21                     self.f = None
22                     return None
23         
24                 if sendfile and length > SENDFILE_THRESHOLD:
25                     # XXX: Yay using non-existent sendfile support!
26                     # FIXME: if we return a SendfileBuffer, and then sendfile
27                     #        fails, then what? Or, what if file is too short?
28                     readSize = min(length, SENDFILE_LIMIT)
29                     res = SendfileBuffer(self.f, self.start, readSize)
30                     self.length -= readSize
31                     self.start += readSize
32                     return res
33         
34                 if self.useMMap and length > MMAP_THRESHOLD:
35                     readSize = min(length, MMAP_LIMIT)
36                     try:
37                         res = mmapwrapper(self.f.fileno(), readSize,
38                                           access=mmap.ACCESS_READ, offset=self.start)
39                         #madvise(res, MADV_SEQUENTIAL)
40                         self.length -= readSize
41                         self.start += readSize
42                         return res
43                     except mmap.error:
44                         pass
45                 # Fall back to standard read.
46                 readSize = self.readsize #this is the only changed line :} 3c5x9 #min(length, self.CHUNK_SIZE)
47                 
48                 self.f.seek(self.start)
49                 b = self.f.read(readSize)
50                 bytesRead = len(b)
51                 if not bytesRead:
52                     raise RuntimeError("Ran out of data reading file %r, expected %d more bytes" % (self.f, length))
53                 else:
54                     self.length -= bytesRead
55                     self.start += bytesRead
56                     return b
57         try:
58             w1 = req.uri.split("?")[1]
59             w2 = w1.split("&")
60             parts= {}
61             for i in w2:
62                 w3 = i.split("=")
63                 parts[w3[0]] = w3[1]
64         except:
65             return http.Response(responsecode.OK, stream="no file given with file=???")            
66         if parts.has_key("file"):
67             path = "/hdd/movie/"+parts["file"].replace("%20"," ")
68             if os.path.exists(path):
69                 self.filehandler = open(path,"r")
70                 s = myFileStream(self.filehandler)
71                 resp =  http.Response(responsecode.OK, {'Content-type': http_headers.MimeType('video', 'ts')},stream=s)
72                 resp.headers.addRawHeader('Content-Disposition','attachment; filename="%s"'%path)
73                 return resp
74             else:
75                 return http.Response(responsecode.OK, stream="file '%s' was not found"%path)            
76         else:
77             return http.Response(responsecode.OK, stream="no file given with file=???")            
78