-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcode.py
executable file
·262 lines (215 loc) · 6.04 KB
/
code.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
#!/usr/bin/python
import web
import os
import markdown
import commands
import time
from web.contrib.template import render_jinja
import json
import random
import string
from settings import *
#Define URLS
urls = (
'/sendfile/(.*)','sendfile',
'/reload/(.*)','reload',
'/(.*)','home'
)
app = web.application(urls, globals())
#don't touch from here on out
site_base = os.path.abspath("code.py").replace("code.py","").replace(site_base_base,"")
http_base += site_base
def header(adict):
if adict.has_key("title"): title = " - " + adict["title"]
else: title = ""
return """
<head>
<title>"""+site_title+title+"""</title>
<LINK REL=StyleSheet HREF='"""+site_base+"""static/style.css' TYPE="text/css" MEDIA=screen>
<LINK REL=StyleSheet HREF='"""+site_base+"""static/styles/idea.css' TYPE="text/css" MEDIA=screen>
<LINK REL=StyleSheet HREF='"""+site_base+"""static/vfu/client/fileuploader.css' TYPE="text/css" MEDIA=screen>
<script src='"""+site_base+"""static/vfu/client/fileuploader.js'></script>
<script src='"""+site_base+"""static/jquery.js'></script>
<style>
body
{
width: 800;
font-size: 10 px;
}
h1
{
font-size: 32px;
border-style:solid;
border-top:thick #000000;
border-right:thick #000000;
border-left:thick #000000;
border-bottom:thin solid #000000;
}
.start
{
font-size: 25px;
font-weight: normal;
}
.start-small
{
font-size: 10px;
font-weight: normal
}
.alt
{
background: rgb(200,200,200);
border-width: 0px;
}
td
{
font-size: 12px;
padding-left: 10px;
padding-right: 10px;
padding-top: 3px;
padding-bottom: 3px;
}
tr
{
border-width: 1px;
border-color: red;
}
.sidebar
{
width: 150px;
float: right;
text-align: right;
}
</style>
</head>
"""
filer = ""
def upload_stuff(fils):
return """
<div class="sidebar">
click on this <br>or<br> drag file(s) here
<div id="file-uploader">
<noscript>
<p>Please enable JavaScript to use file uploader.</p>
<!-- or put a simple form for upload here -->
</noscript>
<script>
var uploader = new qq.FileUploader({
// pass the dom node (ex. $(selector)[0] for jQuery users)
element: document.getElementById('file-uploader'),
// path to server-side upload script
params: {
filer:'"""+fils+"""'
},
action: '"""+site_base+"""sendfile/',
onSubmit: function(id,fileName){
},
onComplete: function(id,fileName,responseJSON){
$.get('"""+site_base+"""reload/"""+fils+"""',
function(data) {
document.getElementById("files").innerHTML = data;
console.log(responseJSON);
});
}
});
</script>
</div>
</div>
"""
def pre(stuff):
return """
<h1><a href='"""+site_base+""" '>"""+site_title+"""</a></h1>
<h2>"""+stuff+"""</h2>
"""
pre2 = """<div id="files">"""
post = """
</div>
"""
def sorter():
dirpath = "files"
# get all entries in the directory w/ stats
entries = (os.path.join(dirpath, fn) for fn in os.listdir(dirpath))
entries = ((os.stat(path), path) for path in entries)
# leave only regular files, insert creation date
entries = ((stat[ST_CTIME], path)
for stat, path in entries if S_ISREG(stat[ST_MODE]))
#NOTE: on Windows `ST_CTIME` is a creation date
# but on Unix it could be something else
#NOTE: use `ST_MTIME` to sort by a modification date
return entries
def converter(thing):
number = os.path.getsize(thing)
out = "<td>"
if number > 1000000000:
out += str(number/1000000000.0)[0:5] + "GB"
elif number > 1000000:
out += str(number/1000000.0)[0:5] + "MB"
elif number > 1000:
out += str(number/1000.0)[0:5] + "kB"
else:
out += str(number)[0:5] + "B"
out = out + "</td><td>" + time.asctime(time.localtime(os.path.getctime(thing)))+"</td>"
return out
def marker(link):
return "<td> <a href='"+link+"'>"+link.split("/")[len(link.split("/"))-1]+"</a> </td>"+converter(link)
def getfiles(generic):
if generic=="drops/": generic = "files"
else: generic = "realfiles/"+generic
try:
files = os.listdir(generic)
except OSError:
web.debug("testing!")
web.debug(commands.getoutput("mkdir "+generic))
files = os.listdir(generic)
files.sort(key=lambda x: os.path.getctime(generic+"/"+x))
files.reverse()
out = "<table>\n"
style = "alt"
for f in files:
if f != ".htaccess":
if style.find("alt")==0: style = ""
else: style ="alt"
out += "<tr class='"+style+"'> "+marker(generic+"/"+f)+" </tr>"
out = out + "</table>"
return out
class reload:
def GET(self,generic):
web.debug(generic)
return getfiles(generic)
class home:
def GET(self,generic):
web.header('Content-Type','text/html; charset=utf-8', unique=True)
if generic=="drops/": generic = "files"
filer = generic
adict = {}
if generic.find("files") == -1:
out = getfiles(generic)
adict["title"] = generic
return header(adict) + pre("You are in the '"+generic+"' drop")+ upload_stuff(filer) +pre2+ markdown.markdown(out) + post
else:
guess = ''.join(random.choice(string.letters) for i in xrange(5))
out = """Name your drop or use the handy/dandy pre-generated random string<br><span class='start'>"""+http_base+"""</span><input style="width: 200px;" type="text" class="start" id="field1" value='"""+guess+"""'><button class="start" onclick="location.href='"""+site_base+"""'+document.getElementById('field1').value">add drop</button></div>
<div class="start-small">only letters/numbers<br>
Safari 5+, Chrome 5+, Firefox 3.5+ (no IE)</div>
"""
generic = ""
return header(adict) + pre("")+ pre2+markdown.markdown(out) + post
class sendfile:
def GET(self,foo):
web.debug(foo)
def POST(self,foo):
web.debug("foo="+foo)
x = web.input()['qqfile']
web.debug(x)
foo = web.input()['filer'];
web.debug()
if foo == "": foo = "files"
else: foo = "realfiles/"+foo
for f in os.listdir(foo):
if f.find(x) > -1:
x = x.split(".")[0]+str(int(time.time()))+"."+x.split(".")[1]
form = web.data()
#web.debug(form)
open(foo+ "/" + str(x), 'w').write(form)
return json.dumps({"success":"true"})
if __name__ == "__main__":
app.run()