-
Notifications
You must be signed in to change notification settings - Fork 0
/
storage.py
executable file
·75 lines (57 loc) · 1.69 KB
/
storage.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
#!/usr/bin/env python2.7
import web
import hashlib
import os, sys
import errno
#sys.path.append(os.path.dirname(__file__))
web.config.debug = True
urls = (
'/', 'index',
'/stor', 'stor',
)
namespace = "data"
class index:
def GET(self):
return "Hello, world"
class stor:
def GET(self):
return """<html><head></head><body>
<form method="POST" enctype="multipart/form-data" action="">
<input type="text" name="folder" /><br/>
<input type="file" name="fileinfo" />
<br/>
<input type="submit" />
</form>
</body></html>"""
def POST(self):
try:
data = web.input(fileinfo={})
filename = data['fileinfo'].filename
folder = data['folder']
if len(filename) == 0:
raise AttributeError("missing filename")
m = hashlib.md5()
if len(folder) > 1:
m.update(folder)
folder = '/'+ folder
else:
m.update(filename)
h = m.hexdigest()
path = "%s/%s/%s/%s%s" % (namespace, h[0:1], h[1:3], h[3:4], folder)
try:
os.makedirs(path)
except OSError, e:
if e.errno == errno.EEXIST:
pass
f = open("%s/%s" % (path, filename), "w")
f.write(data['fileinfo'].file.read())
f.close()
return "%s/%s" % (path, filename)
except AttributeError, e:
return 'AttributeError', str(e)
except Exception, e:
return 'Exception', str(e)
#app = web.application(urls, globals())
#app.run() '''
app = web.application(urls, globals())
application = app.wsgifunc()