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