add webbouqueteditor (dmm plugin contest winner plugin)
[enigma2-plugins.git] / webbouqueteditor / src / WebChilds / WebUploadResource.py
1 from os import write as os_write, close as os_close, O_WRONLY as os_O_WRONLY, O_CREAT as os_O_CREAT, open as os_open, remove as os_remove
2 from twisted.web import resource, http
3
4 class WebUploadResource(resource.Resource):
5         FILENAME = "/tmp/webbouqueteditor_backup.tar"
6         def __init__(self, session):
7                 self.session = session
8                 resource.Resource.__init__(self)
9
10         def render_POST(self, req):
11                 req.setResponseCode(http.OK)
12                 req.setHeader('Content-type', 'application; xhtml+xml;' )
13                 req.setHeader('charset', 'UTF-8')       
14                 data = req.args['file'][0]
15                 if not data:
16                         result = """<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n
17                                 <e2simplexmlresult>\n
18                                         <e2state>False</e2state>
19                                         <e2statetext>Filesize was 0, not uploaded</e2statetext>
20                                 </e2simplexmlresult>\n"""
21                         return result
22                 fd = os_open( self.FILENAME, os_O_WRONLY|os_O_CREAT )
23                 if fd:
24                         cnt = os_write(fd, data)
25                         os_close(fd)
26                 if cnt <= 0:
27                         try:
28                                 os_remove(FILENAME)
29                         except OSError, oe:
30                                 pass
31                         result = """<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n
32                                 <e2simplexmlresult>\n
33                                         <e2state>False</e2state>
34                                         <e2statetext>Error writing to disk, not uploaded</e2statetext>
35                                 </e2simplexmlresult>\n"""
36                 else:
37                         result = """<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n
38                                 <e2simplexmlresult>\n
39                                         <e2state>True</e2state>
40                                         <e2statetext>%s</e2statetext>
41                                 </e2simplexmlresult>\n""" % self.FILENAME
42                 return result
43