-
Notifications
You must be signed in to change notification settings - Fork 4
/
mia.py
332 lines (303 loc) · 14.9 KB
/
mia.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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from bottle import route, run, template, static_file, error, request, redirect
from optparse import OptionParser
import os
import time
TEXT_TYPE = ['doc', 'docx', 'txt', 'rtf', 'odf', 'text', 'nfo']
AUDIO_TYPE = ['aac', 'mp3', 'wav', 'wma', 'm4p', 'flac']
IMAGE_TYPE = ['bmp', 'eps', 'gif', 'ico', 'jpg', 'jpeg', 'png', 'psd', 'psp', 'raw', 'tga', 'tif', 'tiff', 'svg']
VIDEO_TYPE = ['mv4', 'bup', 'mkv', 'ifo', 'flv', 'vob', '3g2', 'bik', 'xvid', 'divx', 'wmv', 'avi', '3gp', 'mp4', 'mov', '3gpp', '3gp2', 'swf', 'mpg', 'mpeg']
COMPRESS_TYPE = ['7z', 'dmg', 'rar', 'sit', 'zip', 'bzip', 'gz', 'tar', 'ace']
EXEC_TYPE = ['exe', 'msi', 'mse']
SCRIPT_TYPE = ['js', 'html', 'htm', 'xhtml', 'jsp', 'asp', 'aspx', 'php', 'xml', 'css', 'py', 'bat', 'sh', 'rb', 'java', 'sql']
path_local = "."
PAGE = u'''
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>{{path_dir}}</title>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<style>
body
{
margin:50px 0;
padding:0;
text-align:center;
font-family:"Lucida Sans Unicode","Lucida Grande",Sans-Serif;
}
.content
{
width:800px;
margin:0 auto;
}
.path
{
text-align:left;
padding:0 0 .3em .7em;
}
.line
{
border-bottom:2px solid #6b6b6b;
}
.footer
{
text-align:right;
font-size:10px;
}
.alert {
background-color: #FCF8E3;
border: 1px solid #FBEED5;
border-radius: 4px 4px 4px 4px;
color: #C09853;
margin-bottom: 18px;
padding: 8px 35px 8px 14px;
text-shadow: 0 1px 0 rgba(255, 255, 255, 0.5);
}
#fileTable
{
font-size:12px;
background:#fff;
width:800px;
border-collapse:collapse;
text-align:left;
}
#fileTable th
{
font-weight:normal;
text-align:left;
padding-left:1em;
border-bottom:1px solid #ff1405;
height:2.5em;
}
#fileTable td
{
border-bottom:1px solid #e0e0e0;
color:#3c3c3c;
height:25px;
padding-left:1em;
}
#fileTable td.data
{
background-color:#ededed;
width:12.5%;
}
#fileTable a
{
text-decoration:none;
color:#3c3c3c
}
#fileTable a:hover
{
color:#555
}
i.icon
{
background:url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAKAAAAAQCAYAAACRBXRYAA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PI5XK6uLhsto7CZcmSzZYHhMOFsMdgoMuSJZsxoBwOxQ+dVxPYWhg7KJ1XE5izfyU7rybwldMaHg7FD1bx88/PWr5+/dHs0lJevHaNhZWVvHrnDitra3m3uZlN7e1s7+qiqqeHvb8G8G+21z5//jxHg+XqgQIcSl6EfEcIFOAQgOfx67vS5lESM2cMDi51hnQ0zBnT/y/yEP4dH+XYjwPAuHHjgp588snE0TBu3LggABgLYNIkd/dttk+pzSRNZjONZjMNJhP1JhN1RiN7zWZOcnffBmBSn/ZxAJP2vY+flGVJ7CiJHxRlWRJPfLqMyrIk7nsfP/nPgxDA45gwYdYsL6/dbR0digfe9ST3HjlyEc8+6wPg3wD8FsCTEokkNzs7m44gkUhyATzZxzMApjjIM33asX2/haN6y+/n/BB65/8l/xyd/zN9Omt+HmL+mADg5bEvvBA5dubM5BHxwguRAF7u004A8HLIH3Bg57vIHQkhf8ABq97Z+RWnadOSRwomTfIH4Argub4A/BbAOABPOMi4Pu3/9/Kw/v1Ty/8A31YwU8b/R04AAAAASUVORK5CYII=) no-repeat scroll 0 0 transparent;
display:block;
float:left;
height:16px;
margin:0 10px 0 0;
width:16px;
}
i.icon.pdf
{
background-position:-0px -0px;
}
i.icon.folder
{
background-position:-16px -0px;
}
i.icon.script
{
background-position:-32px -0px;
}
i.icon.unknow
{
background-position:-48px -0px
}
i.icon.compress
{
background-position:-64px -0px;
}
i.icon.audio
{
background-position:-80px -0px;
}
i.icon.imagen
{
background-position:-96px -0px;
}
i.icon.text
{
background-position:-112px -0px;
}
i.icon.video
{
background-position:-128px -0px;
}
i.icon.exec
{
background-position:-144px -0px;
}
</style>
</head>
<body>
<div class="content">
%if error is None:
<div class="path">
PATH: {{path_dir}}
</div>
<div class="line"></div>
<table id="fileTable">
<thead>
<tr>
<th>Name</th>
<th>Date</th>
<th>Size</th>
</tr>
</thead>
<tbody>
%for file in to_html:
<tr>
<td>
<i class='icon {{file.iconType}}'></i>
%if file.iconType == 'folder':
<a href='{{file.folder}}/{{file.filename}}'>{{file.filename}}</a>
%else:
<a href='/download?file={{file.folder}}/{{file.filename}}'>{{file.filename}}</a>
%end
</td>
<td class='data'>
{{file.filesize}}
</td>
<td class='data'>
{{file.filedate}}
</td>
</tr>
%end
</tbody>
</table>
%else:
<div class="alert">
{{error}}
</div>
%end
<div class="footer">
Powered by <a href='https://github.com/herrerae/mia'>Mia</a>
</div>
</div>
</body>
</html>
'''
@route('/')
@route('/index/')
def index_2():
redirect('/index')
@route('/index')
@route('/index/:path#.+#')
def index(path=None):
if path is None:
return read_folder('')
else:
return read_folder(path)
@route('/download')
def download():
filename = request.GET.get('file')
return static_file(filename, root=path_local, download=filename)
@error(404)
def error404(error):
return 'Nothing here, sorry'
@error(500)
def error500(error):
return 'La carpeta no existe'
#------------------ UTILS -----------------------------
def read_folder(path):
to_html = []
if path == '':
path_dir = path_local
else:
path_dir = path_local + '/' + path
try:
file_list = os.listdir(path_dir)
except Exception:
return template(PAGE, error="Folder/Directory not found, Please check the name", path_dir=path)
for archivo in file_list:
if archivo == 'mia_main.py' or archivo == 'bottle.py' or archivo == 'bottle.pyc':
pass
else:
file_path = os.path.join(path_dir, archivo)
if os.path.isdir(file_path) == True:
cosa = HtmlFile('folder', request.fullpath, archivo, file_path)
to_html.append(cosa)
else:
extension = os.path.splitext(archivo)[1].replace('.', '')
if extension in TEXT_TYPE:
type_file = 'text'
elif extension in AUDIO_TYPE:
type_file = 'audio'
elif extension in IMAGE_TYPE:
type_file = 'imagen'
elif extension in VIDEO_TYPE:
type_file = 'video'
elif extension in COMPRESS_TYPE:
type_file = 'compress'
elif extension in EXEC_TYPE:
type_file = 'exec'
elif extension in SCRIPT_TYPE:
type_file = 'script'
elif extension == 'pdf':
type_file = 'pdf'
else:
type_file = 'unknow'
cosa = HtmlFile(type_file, path, archivo, file_path)
to_html.append(cosa)
if path == '':
path = 'index'
else:
path = 'index' + "/" + path
return template(PAGE, to_html=to_html, path_dir=path, error=None)
def convert_bytes(path):
'''
Copy from http://www.5dollarwhitebox.org/drupal/node/84
'''
bytes = float(os.path.getsize(path))
if bytes >= 1099511627776:
terabytes = bytes / 1099511627776
size = '%.2f Tb' % terabytes
elif bytes >= 1073741824:
gigabytes = bytes / 1073741824
size = '%.2f Gb' % gigabytes
elif bytes >= 1048576:
megabytes = bytes / 1048576
size = '%.2f Mb' % megabytes
elif bytes >= 1024:
kilobytes = bytes / 1024
size = '%.2f Kb' % kilobytes
else:
size = '%.2f Byte' % bytes
return size
def date_file(path):
tiempo = time.gmtime(os.path.getmtime(path))
return time.strftime("%d/%m/%Y-%H:%M:%S", tiempo)
def set_argument():
parser = OptionParser(usage="Usage: %prog [options]", version="%prog 1.5")
parser.add_option("-d", "--path", action="store", dest="path", default=".", help="Directory you want to share Ex. /home/hans/project/milleniun_falcon",)
parser.add_option("-o", "--host", action="store", dest="host", default="localhost", help="Server host. Defaul: localhost",)
parser.add_option("-p", "--port", action="store", dest="port", default=8080, help="Server port. Defaul: 8080",)
return parser
class HtmlFile(object):
def __init__(self, iconType, folder, filename, file_path):
self.iconType = iconType
self.folder = folder.decode('cp1252')
self.filename = filename.decode('cp1252')
self.filesize = convert_bytes(file_path)
self.filedate = date_file(file_path)
if __name__ == '__main__':
(options, args) = set_argument().parse_args()
debugMode = True
port = options.port
host = options.host
path_local = options.path
run(host=host, port=port, reloader=debugMode, debug=debugMode)