Commit 2fbe40ed authored by Per Cederqvist's avatar Per Cederqvist
Browse files

Initial commit: navigation and download.

This code cannot yet convert to mp3, but works
if an mp3 file already exists.
parents
import os
import stat
import io
import urllib.parse
BASE = '/home/ceder/tmp/2014/sample'
def quote(s):
return urllib.parse.quote(s)
def application(environ, start_response):
if environ["REQUEST_METHOD"] != "GET":
start_response("405 Method Not Allowed",
[('Allow', 'GET')])
return []
path = environ['PATH_INFO']
# We want a trailing slash at the root.
if path == "" and environ.get("SCRIPT_NAME", "") != "":
start_response("301 Moved Permanently",
[("Location", environ["SCRIPT_NAME"] + "/")])
return []
subdir = os.path.dirname(path)
if len(subdir) > 0 and subdir[0] == "/":
subdir = subdir[1:]
filename = os.path.basename(path)
realfile = os.path.join(BASE, subdir, filename)
try:
status = os.stat(realfile)
except os.error:
start_response("404 Not found", [])
return []
if stat.S_ISDIR(status.st_mode):
return handle_directory(environ, start_response,
os.path.join(subdir, filename))
elif stat.S_ISREG(status.st_mode):
return handle_file(environ, start_response,
os.path.join(subdir, filename))
start_response("404 Not found", [])
return []
def sizeof_fmt(num):
for x in ['bytes','KB','MB','GB']:
if num < 1024.0 and num > -1024.0:
return "%.1f %s" % (num, x)
num /= 1024.0
return "%.1f %s" % (num, 'TB')
def handle_directory(environ, start_response, rel_path):
wavs = {}
mp3s = {}
dirs = set()
for fn in os.listdir(os.path.join(BASE, rel_path)):
realfile = os.path.join(BASE, rel_path, fn)
try:
status = os.stat(realfile)
except os.error:
continue
root, ext = os.path.splitext(fn)
if ext == ".wav":
wavs[root] = status
elif ext == ".mp3":
mp3s[root] = status
elif stat.S_ISDIR(status.st_mode):
dirs.add(fn)
res = io.StringIO()
res.write("<html>")
res.write("<head>")
res.write("</head>")
res.write("<body>")
parts = rel_path.split("/")
if len(parts) > 1:
res.write("\n<a href=\"%s\">(root)</a>" % ("../" * (len(parts) - 1)))
for n, part in enumerate(parts[:-1]):
res.write("\n / <a href=\"%s\">%s</a>" % (
"../" * (len(parts) - n - 2), part))
res.write("<ul>")
if rel_path != "":
res.write("<li><a href=\"..\">(up one level)</a></li>\n")
for d in sorted(dirs):
res.write("<li>")
res.write("<a href=\"%s/\">%s</a>" % (quote(d), d))
res.write("</li>\n")
for w in sorted(wavs):
res.write("<li>")
res.write("<a href=\"%s.wav\">%s.wav</a>" % (
quote(w), w))
res.write(" (" + sizeof_fmt(wavs[w].st_size) + ")")
if w in mp3s:
res.write(" <a href=\"%s.mp3\">.mp3</a>" % quote(w))
res.write(" (" + sizeof_fmt(mp3s[w].st_size) + ")")
else:
res.write(" (FIXME: add button to convert to mp3)")
res.write("</li>\n")
res.write("</ul>")
res.write("</body>")
res.write("</html>")
page = res.getvalue()
res.close()
start_response("200 OK", [("Content-Type", "text/html"),
("Content-Length", str(len(page)))])
return [page]
def handle_file(environ, start_response, rel_path):
root, ext = os.path.splitext(rel_path)
content_type = {".wav": "audio/vnd.wave",
".mp3": "audio/mpeg"}.get(ext)
if content_type is None:
start_response("403 Forbidden", [])
return []
file = open(os.path.join(BASE, rel_path), "r")
start_response("200 OK", [("Content-Type", content_type)])
if 'wsgi.file_wrapper' in environ:
return environ['wsgi.file_wrapper'](file, 1024 * 1024)
else:
return iter(lambda: file.read(1024 * 1024), '')
WSGIScriptAlias / /home/ceder/rgit/mp3wavweb/mp3wavweb.py
<Directory /home/ceder/rgit/mp3wavweb>
Order allow,deny
Allow from all
Require all granted
</Directory>
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment