1.1 --- a/scripts/redirect.py Sun Aug 10 18:44:07 2014 +0200
1.2 +++ b/scripts/redirect.py Sun Aug 10 19:34:36 2014 +0200
1.3 @@ -16,17 +16,32 @@
1.4 # An empty string means that the wiki is anchored at the site root.
1.5
1.6 URL_PREFIX = ""
1.7 +DOWNLOAD_SCRIPT = "/download.py"
1.8 EXPORT_SCRIPT = "/export.py"
1.9 EXPORT_PDF_SCRIPT = "/exportpdf.py"
1.10
1.11 -# See the scripts/tiny.py program for similar code in a stand-alone program.
1.12 +# Conversion of tiny identifiers to verbose identifiers.
1.13
1.14 from base64 import b64decode
1.15 from struct import unpack
1.16
1.17 def identifier(s):
1.18 +
1.19 + "Extract an identifier from the given string 's'."
1.20 +
1.21 + # Isolate numeric identifiers for things like downloads:
1.22 + # /download/attachment/<pageid>/<filename>?<args>
1.23 +
1.24 + if s.split("/")[0].isdigit():
1.25 + return s.split("/")[0]
1.26 +
1.27 + # Reject strings that are too long for tiny identifiers.
1.28 +
1.29 if len(s) > 6:
1.30 return None
1.31 +
1.32 + # Attempt to unpack tiny identifiers.
1.33 +
1.34 bytes = b64decode(s.replace("-", "/").replace("_", "+") + "=" * (6 - len(s)))
1.35 return str(unpack("<I", bytes + "\x00" * (4 - len(bytes)))[0])
1.36
1.37 @@ -47,8 +62,13 @@
1.38 """ % (pageid and " (%s)" % pageid or "")
1.39 sys.exit(0)
1.40
1.41 -def redirect(pagename, export=False):
1.42 - location = "%s/%s%s" % (URL_PREFIX, quote(pagename), export and "?action=ExportPDF" or "")
1.43 +def redirect(pagename, download=None, export=False):
1.44 + location = "%s/%s%s" % (
1.45 + URL_PREFIX, quote(pagename),
1.46 + download and "?action=AttachFile&do=get&target=%s" % download
1.47 + or export and "?action=ExportPDF"
1.48 + or ""
1.49 + )
1.50
1.51 print """\
1.52 Status: 302 Redirect to page
1.53 @@ -81,6 +101,10 @@
1.54 if pageid is None:
1.55 fail(pageid)
1.56
1.57 + download = (script.endswith(DOWNLOAD_SCRIPT) or
1.58 + script.endswith("/download/attachment")
1.59 + ) and path.rsplit("/", 1)[1] or None
1.60 +
1.61 export = (script.endswith(EXPORT_SCRIPT) or
1.62 script.endswith("/pages/doexportpage.action")
1.63 ) and args.get("type", [""])[0] == "TYPE_PDF" or \
1.64 @@ -101,7 +125,7 @@
1.65
1.66 # Redirect to the page.
1.67
1.68 - redirect(pagename, export)
1.69 + redirect(pagename, download, export)
1.70
1.71 finally:
1.72 f.close()