Made Mosaic translateable and added german translation
[enigma2-plugins.git] / webinterface / src / WebChilds / Uploader.py
1 from os import statvfs, path as os_path, system as os_system, chmod as os_chmod
2 from twisted.web2 import resource, responsecode, http, http_headers
3
4 class UploadResource(resource.PostableResource):
5     default_uploaddir = "/tmp/"
6     
7     def render(self, req):
8         self.args = req.args
9         #############
10         uploaddir=self.default_uploaddir
11         if self.getArg("path"):
12             if os_path.isdir(self.getArg("path")):
13                 uploaddir= self.getArg("path")
14                 if uploaddir[-1] != "/":
15                     uploaddir += "/"
16             else:
17                 return http.Response(responsecode.OK,{'content-type': http_headers.MimeType('text', 'html')},"path '%s' to upload not existing!"%self.getArg("path"))
18         #########
19         if len(req.files):
20             #static.destination = uploaddir
21             return self.do_upload(req,uploaddir)
22         else:
23             return self.do_indexpage(req)
24     
25     def do_upload(self,req,uploaddir):
26         for file in req.files:
27             import tempfile
28             (filename,mimetype,filehandler) = req.files[file][0]
29 #            filehandler.name = tempfile.mktemp(suffix=os_path.splitext(filename)[1], dir=uploaddir)
30             print "filehandler.name: ",filehandler.name
31             filehandler.seek(0, 2)  # Seek to the end of the file.
32             filesize = filehandler.tell()  # Get the position of EOF.
33             filehandler.seek(0)  # Reset the file position to the beginning.
34             if filesize <=0:
35                 os_system("rm '%s'" %filehandler.name)
36                 return http.Response(responsecode.OK,{'content-type': http_headers.MimeType('text', 'html')},"filesize was 0, not uploaded")
37             else:
38                 os_system("mv '%s' '%s' " %(filehandler.name,uploaddir+filename))
39                 os_chmod(uploaddir+filename, 0755)
40                 return http.Response(responsecode.OK,{'content-type': http_headers.MimeType('text', 'html')},"uploaded to %s"%uploaddir+filename)
41     
42     def do_indexpage(self,req):
43         try:
44             stat = statvfs("/tmp/")
45         except OSError:
46             return -1
47         
48         freespace = stat.f_bfree / 1000 * stat.f_bsize / 1000
49         
50         return http.Response(responsecode.OK,
51                              {'content-type': http_headers.MimeType('text', 'html')},
52         """
53                 <form method="POST" enctype="multipart/form-data">
54                 <table>
55                 <tr><td>Path to save (default is '%s')</td><td><input name="path"></td></tr>
56                 <tr><td>File to upload</td><td><input name="file" type="file"></td></tr>
57                 <tr><td colspan="2">Filesize must not be greather than %dMB! /tmp/ has not more free space!</td></tr>
58                 <tr><td colspan="2"><input type="submit"></td><tr>
59                 </table>
60                 </form>
61                 
62         """%(self.default_uploaddir,freespace))
63
64     def getArg(self,key):
65         if self.args.has_key(key):
66             return self.args[key][0]
67         else:
68             return False