python-large-file support is now avaiable in OE, so we dont need to work around the...
[enigma2-plugins.git] / webinterface / src / WebChilds / FileStreamer.py
1 from twisted.web2 import resource, stream, responsecode, http, http_headers
2 import os
3
4 class FileStreamer(resource.Resource):
5     addSlash = True
6     
7     def render(self, req):
8         try:
9             w1 = req.uri.split("?")[1]
10             w2 = w1.split("&")
11             parts= {}
12             for i in w2:
13                 w3 = i.split("=")
14                 parts[w3[0]] = w3[1]
15         except:
16             return http.Response(responsecode.OK, stream="no file given with file=???")
17         root = "/hdd/movie/"
18         if parts.has_key("root"):
19             root = parts["root"].replace("%20"," ")
20         if parts.has_key("file"):
21             filename = parts["file"].replace("%20"," ")
22             path = root+filename
23             if os.path.exists(path):
24                 s = stream.FileStream(open(path,"r"))
25                 type = path.split(".")[-1]
26                 header = http_headers.MimeType('video', 'ts')
27                 if type == "mp3" or type == "ogg" or type == "wav":
28                     header = http_headers.MimeType('audio', 'x-mpeg')
29                 elif type == "avi" or type == "mpg":
30                     header = http_headers.MimeType('video', 'x-msvideo')
31                 elif type == "jpg" or type == "jpeg" or type == "jpe":
32                     header = http_headers.MimeType('image', 'jpeg')
33                 
34                 resp =  http.Response(responsecode.OK, {'Content-type': header},stream=s)
35                 resp.headers.addRawHeader('Content-Disposition','attachment; filename="%s"'%filename)
36                 return resp
37             else:
38                 return http.Response(responsecode.OK, stream="file '%s' was not found"%path)            
39         else:
40             return http.Response(responsecode.OK, stream="no file given with file=???")            
41