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
|
#!/usr/bin/env python2
import os, mimetypes, json, cgi, recode, time, urllib, events, db, itertools
from config import config
from directory import Directory, File
from session import Session
class Application(object):
# Application handlers
def files(self, environ, start_response, path):
full_path = os.path.join(config.get('music_root'), *path[1:])
if not os.path.exists(full_path) or '..' in path:
start_response('404 Not Found', [])
return []
if os.path.isdir(full_path):
start_response('200 OK', [('Content-Type', 'text/html; charset=UTF-8')])
return (str(x) for x in Directory(full_path).listdir())
else:
return File(full_path).send(environ, start_response)
def static(self, environ, start_response, path):
filename = os.path.join('static', *path[1:])
if not os.path.exists(filename) or '..' in path:
start_response('404 Not Found', [])
return []
mime = mimetypes.guess_type(filename, strict = False)[0] or 'application/octet-stream'
start_response('200 OK', [('Content-Type', mime)])
return open(filename, 'rb')
def cache(self, environ, start_response, path):
args = cgi.FieldStorage(environ = environ)
path = os.path.join(*path[1:])
path = os.path.join(config.get('music_root'), path)
track = int(args.getvalue('track')) if 'track' in args else None
try:
session = db.Session()
t = db.Track.find(session, path, track)
except:
start_response('404 Not Fonud', [])
return []
finally:
session.close()
cache_path = File(path, track = track).get_cache_path()
if not os.path.exists(cache_path) or '..' in path:
start_response('404 Not Found', [])
return []
f = File(cache_path, True)
return f.send(environ, start_response)
# JSON handlers
def json_list(self, environ, start_response, path):
args = cgi.FieldStorage(environ = environ)
directory = args.getvalue('directory') if 'directory' in args else ''
if directory[-1:] == '/':
directory = directory[:-1]
directory = os.path.join(config.get('music_root'), directory)
if directory[-1:] == '/':
directory = directory[:-1]
contents = []
try:
session = db.Session()
directory = db.Directory.get(session, directory)
directories = (Directory(d.path) for d in directory.children)
tracks = (File(t.get_path(), track = t.file_index, metadata = t.get_metadata()) for t in directory.tracks)
contents = [x.json() for x in itertools.chain(directories, tracks)]
finally:
session.close()
start_response('200 OK', [('Content-Type', 'text/plain')])
return json.dumps(contents)
def json_recode(self, environ, start_response, path):
args = cgi.FieldStorage(environ = environ)
path = args.getvalue('path') if 'path' in args else ''
path = os.path.join(config.get('music_root'), path)
track = int(args.getvalue('track')) if 'track' in args else None
try:
session = db.Session()
t = db.Track.find(session, path, track)
except:
start_response('404 Not Fonud', [])
return []
finally:
session.close()
f = File(path, track = track)
# see json_play()
if not os.path.splitext(path)[1] in ('.mp3', '.ogg'):
decoder = 'ffmpeg'
encoder = config.get('encoder')
f.start_recode(decoder, encoder)
start_response('200 OK', [('Content-Type', 'text/plain')])
return []
def json_play(self, environ, start_response, path):
args = cgi.FieldStorage(environ = environ)
rel_path = args.getvalue('path')
path = os.path.join(config.get('music_root'), rel_path)
track = int(args.getvalue('track')) if 'track' in args else None
try:
session = db.Session()
t = db.Track.find(session, path, track)
except:
start_response('404 Not Fonud', [])
return []
finally:
session.close()
f = File(path, track = track)
# TODO: replace this with some sane logic
if not os.path.splitext(path)[1] in ('.mp3', '.ogg'):
cache_path = f.get_cache_path()
decoder = 'ffmpeg'
encoder = config.get('encoder')
if not os.path.exists(cache_path):
f.start_recode(decoder, encoder, environ['sessionid'])
else:
events.event_pub.play(environ['sessionid'], '/cache/{0}{1}'.format(rel_path, ('?track=' + str(track) if track else '')))
else:
events.event_pub.play(environ['sessionid'], '/files/{0}'.format(rel_path))
start_response('200 OK', [('Content-Type', 'text/plain')])
return []
handlers = {
'files': files,
'static': static,
'cache': cache,
'list': json_list,
'recode': json_recode,
'play': json_play,
'events': events.EventSubscriber,
}
# WSGI handler
def __call__(self, environ, start_response):
path = urllib.unquote(environ['PATH_INFO'])
path = path.split('/')[1:]
module = path[0] or None
if not module:
module = 'static'
path = ['static', 'index.html']
if module in self.handlers:
return Session(self.handlers[module])(self, environ, start_response, path)
start_response('404 Not Found', [('Content-Type', 'text/plain')])
return [str(path)]
if __name__ == '__main__':
import sys
if len(sys.argv) == 3:
from flup.server.fcgi import WSGIServer
WSGIServer(Application(), bindAddress = (sys.argv[1], int(sys.argv[2]))).run()
else:
from wsgiref.simple_server import make_server, WSGIServer
# enable IPv6
WSGIServer.address_family |= 10
httpd = make_server('', 8000, Application())
httpd.serve_forever()
|