Commit d0f2bd70 authored by Per Cederqvist's avatar Per Cederqvist
Browse files

Convert wav files via the web interface.

Still very ugly, and there are permission issues that
needs to be solved.
parent 73225a11
......@@ -3,6 +3,7 @@ import stat
import io
import urllib.parse
import sys
import multiprocessing.connection
sys.path.insert(0, os.path.dirname(__file__))
......@@ -11,6 +12,23 @@ import mp3wavcfg
def quote(s):
return urllib.parse.quote(s)
# Taken from
# http://stackoverflow.com/questions/4579908/cross-platform-splitting-of-path-in-python
# but with debug removed since Python 3 does not support that print
# syntax (and debug was pointless anyhow).
def os_path_split_asunder(path):
parts = []
while True:
newpath, tail = os.path.split(path)
if newpath == path:
assert not tail
if path: parts.append(path)
break
parts.append(tail)
path = newpath
parts.reverse()
return parts
def application(environ, start_response):
if environ["REQUEST_METHOD"] != "GET":
start_response("405 Method Not Allowed",
......@@ -25,31 +43,69 @@ def application(environ, start_response):
[("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)
parts = os_path_split_asunder(path)
if len(parts) > 0 and parts[0] == "/":
parts = parts[1:]
realfile = os.path.join(mp3wavcfg.BASE, subdir, filename)
realfile = os.path.join(mp3wavcfg.BASE, *parts)
try:
status = os.stat(realfile)
except os.error:
except (os.error, FileNotFoundError):
dirparts, basename, ops = locate_mp3(parts)
if ops is not None and len(ops) > 0 and ops[0] in MP3_OPS:
rel_base = os.path.join(*(dirparts + [basename]))
basefile = os.path.join(mp3wavcfg.BASE, rel_base)
try:
wavstat = os.stat(basefile + ".wav")
except os.error:
pass
else:
return MP3_OPS[ops[0]](start_response, rel_base, ops[1:])
start_response("404 Not found", [])
return []
if stat.S_ISDIR(status.st_mode):
return handle_directory(environ, start_response,
os.path.join(subdir, filename))
os.path.join(*parts))
elif stat.S_ISREG(status.st_mode):
return handle_file(environ, start_response,
os.path.join(subdir, filename))
os.path.join(*parts))
start_response("404 Not found", [])
return []
def convert(start_response, rel_base, ops):
if len(ops) > 0:
start_response("404 Not found", [])
return []
c = multiprocessing.connection.Client(mp3wavcfg.socketpath, 'AF_UNIX')
c.send(("encode", rel_base))
while True:
msg = c.recv()
if msg[0] != "progress":
break
start_response("301 Moved Permanently",
[("Content-Type", "text/plain"),
("Location", "..")])
return ["File converted to mp3"]
MP3_OPS = {
'convert': convert,
}
def locate_mp3(parts):
pos = len(parts) - 1
while pos >= 0:
fn, ext = os.path.splitext(parts[pos])
if ext == ".mp3":
return parts[:pos], fn, parts[pos+1:]
pos -= 1
return None, None, None
def sizeof_fmt(num):
for x in ['bytes','KB','MB','GB']:
......@@ -106,7 +162,10 @@ def handle_directory(environ, start_response, rel_path):
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("<form method=\"GET\" action=\"%s.mp3/convert\">\n" % (
quote(w)))
res.write("<button>Convert to mp3</button>")
res.write("</form>")
res.write("</li>\n")
res.write("</ul>")
res.write("</body>")
......
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