[webinterface] add internet mimetype for mpeg2-ts, use proper error pages in Filestreamer
[enigma2-plugins.git] / webinterface / src / WebChilds / FileStreamer.py
1 from twisted.web import resource, http, server, static
2 from urllib import unquote
3 from os import path as os_path
4 from Tools.Directories import resolveFilename, SCOPE_HDD
5
6 class FileStreamer(resource.Resource):
7         addSlash = True
8
9         def render(self, request):
10                 if 'dir' in request.args:
11                         dir = unquote(request.args['dir'][0])
12                 elif 'root' in request.args:
13                         dir = unquote(request.args['root'][0])
14                 else:
15                         dir = ''
16
17                 if 'file' in request.args:
18                         filename = unquote(request.args["file"][0])
19                         path = dir + filename
20
21                         #dirty backwards compatibility hack
22                         if not os_path.exists(path):
23                                 path = resolveFilename(SCOPE_HDD, filename)
24
25                         print "[WebChilds.FileStreamer] path is %s" %path
26
27                         if os_path.exists(path):
28                                 basename = filename.decode('utf-8', 'ignore').encode('ascii', 'ignore')
29
30                                 if '/' in basename:
31                                         basename = basename.split('/')[-1]
32
33                                 request.setHeader("content-disposition", "attachment;filename=\"%s\"" % (basename))
34                                 file = static.File(path, defaultType = "application/octet-stream")
35                                 return file.render_GET(request)
36
37                         else:
38                                 return resource.NoResource(message="file '%s' was not found" %(dir + filename)).render(request)
39                 else:
40                         return resource.NoResource(message="no file given with file={filename}").render(request)
41
42                 return server.NOT_DONE_YET
43