-
Notifications
You must be signed in to change notification settings - Fork 0
/
ms.py
executable file
·128 lines (111 loc) · 4.03 KB
/
ms.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#!/usr/bin/python
import cgitb
cgitb.enable()
import cgi
from datetime import datetime
import os
import ptree
import simplejson as json
#print json.dumps({'Hello': 'world!'})
#for param in os.environ.keys():
# print '%20s: %s' % (param, os.environ[param])
#print '\n' + os.environ['REQUEST_METHOD']
# Generator to buffer file chunks
# from http://webpython.codepoint.net/cgi_big_file_upload
def fbuffer(f, chunk_size=10000):
while True:
chunk = f.read(chunk_size)
if not chunk: break
yield chunk
def log(message):
try:
log_handle = open('../log', 'a')
log_handle.write(message + '\n')
finally:
log_handle.close()
def html_response(content):
return """\
Content-Type: text/html\n
<!doctype html>
<html>
<head><title>up</title></head>
<body>%s</body>
</html>
""" % content
def get_response():
accept_formats = os.environ.get('HTTP_ACCEPT', [])
if accept_formats:
accept_formats = accept_formats.split(',')
form = cgi.FieldStorage()
#print 'Content-Type: text/plain\n'
#print form
#print 'Content-Type: text/html\n'
#cgi.print_environ()
if os.environ['REQUEST_METHOD'] == 'GET':
identity = None
filename = 'meta.json'
if 'id' in form:
identity = form['id'].value
if 'f' in form:
filename = form['f'].value
if identity:
ppath = ptree.id2ptree(identity)
name = os.path.basename(filename)
url = 'http://data.free103point9.org/r%s%s' % (ppath, name)
return '''Content-Type: text/plain
Location: %(url)s
%(url)s''' % {'url': url}
if os.environ['REQUEST_METHOD'] == 'POST':
if 'id' in form and 'file' in form:
identity = form['id'].value
# A nested FieldStorage instance holds the file
fileitem = form['file']
if identity and fileitem.filename:
ppath = ptree.id2ptree(identity)
home = '../r%s' % ppath
try:
os.makedirs(home)
except OSError:
pass
# strip leading path from file name to avoid directory traversal attacks
name = os.path.basename(fileitem.filename)
f = open('../r%s%s' % (ppath, name), 'wb', 10000)
# Read the file in chunks
for chunk in fbuffer(fileitem.file):
f.write(chunk)
f.close()
log(datetime.now().isoformat() + ' POST ' + identity + ' ' + name)
message = "The file %s was uploaded successfully" % name
if 'text/html' in accept_formats:
return html_response('<p>%s</p>' % message)
else:
return 'Content-Type: text/plain\n\n%s' % message
if os.environ['REQUEST_METHOD'] == 'DELETE':
if 'id' in form and 'filename' in form:
identity = form['id'].value
filename = form['filename'].value
if identity and filename:
ppath = ptree.id2ptree(identity)
dir = '../r%s' % ppath
name = os.path.basename(filename)
os.remove(dir + name)
try:
os.removedirs(dir) # remove parent directories if empty
except OSError:
pass
log(datetime.now().isoformat() + ' DELETE ' + identity + ' ' + name)
message = "The file %s was deleted successfully" % name
if 'text/html' in accept_formats:
return html_response('<p></p>')
else:
return 'Content-Type: text/plain\n\n%s' % message
if 'text/html' in accept_formats:
return html_response("""
<form enctype="multipart/form-data" method="post">
<input type="text" name="id">
<input type="file" name="file">
<input type="submit">
</form>""")
else:
return 'Content-Type: text/plain\n\ndata.free103point9.org'
print get_response()