32
from common import (cgirequest, studpath)
33
from common import zip as zipmod
30
from common import cgirequest
31
from common import studpath
35
33
req = cgirequest.CGIRequest()
36
req.install_error_handler()
38
# Work out the parts of the URL
39
url = urlparse.urlparse(req.path)
42
filename = studpath.url_to_jailpaths(urlpath)[2]
44
default_mimetype = "application/octet-stream"
45
zip_mimetype = "application/zip"
52
# If any "path=" variables have been supplied, bring these into a list and
53
# make a zip file instead.
54
fields = req.get_fieldstorage()
55
paths = fields.getlist("path")
58
zipbasepath = filename
59
zipfilename = os.path.basename(zipbasepath)
60
#for i in range(0, len(paths)):
61
#paths[i] = paths[i].value
64
req.throw_error(req.HTTP_NOT_FOUND,
65
"The path specified is invalid.")
66
elif not os.access(filename, os.R_OK):
67
req.throw_error(req.HTTP_NOT_FOUND,
68
"The specified file (%s) does not exist." % urlpath)
69
# If it's a directory, serve as a zip file
70
if os.path.isdir(filename):
72
# Zip it from the perspective of its own parent.
73
# That way it will be a directory in the top level of the zip
75
if filename[-1] == os.sep: filename = filename[:-1]
76
splitpath = filename.rsplit(os.sep, 1)
77
if len(splitpath) == 1:
81
zipbasepath = splitpath[0]
82
paths = [splitpath[1]]
83
zipfilename = paths[0]
86
req.content_type = zip_mimetype
87
# zipfilename is some filename. Strip trailing slash or extension,
91
elif zipfilename[-1] == '/':
92
zipfilename = zipfilename[:-1]
93
elif '.' in zipfilename:
94
zipfilename = zipfilename[:zipfilename.rindex('.')]
96
req.headers_out["Content-Disposition"] = ("attachment; filename=" +
98
zipfile = StringIO.StringIO()
99
zipmod.make_zip(zipbasepath, paths, zipfile)
101
req.write(zipfile.getvalue())
103
#req.content_type = default_mimetype
104
req.sendfile(filename)
34
filename = studpath.url_to_jailpaths(req.path)[2]
36
if not os.access(filename, os.R_OK):
37
req.throw_error(req.HTTP_NOT_FOUND, "The specified file does not exist.")
39
(type, _) = mimetypes.guess_type(filename)
41
type = conf.mimetypes.default_mimetype
43
req.content_type = type
44
req.sendfile(filename)