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