30
from common import cgirequest
31
from common import studpath
32
from common import (cgirequest, studpath)
33
from common import zip as zipmod
33
35
req = cgirequest.CGIRequest()
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)
37
# Work out the parts of the URL
38
url = urlparse.urlparse(req.path)
41
filename = studpath.url_to_jailpaths(urlpath)[2]
43
default_mimetype = "application/octet-stream"
44
zip_mimetype = "application/zip"
51
# If any "path=" variables have been supplied, bring these into a list and
52
# make a zip file instead.
53
fields = req.get_fieldstorage()
54
paths = fields.getlist("path")
57
zipbasepath = filename
58
zipfilename = os.path.basename(zipbasepath)
59
#for i in range(0, len(paths)):
60
#paths[i] = paths[i].value
63
req.throw_error(req.HTTP_NOT_FOUND,
64
"The path specified is invalid.")
65
elif not os.access(filename, os.R_OK):
66
req.throw_error(req.HTTP_NOT_FOUND,
67
"The specified file (%s) does not exist." % path)
68
# If it's a directory, serve as a zip file
69
if os.path.isdir(filename):
71
# Zip it from the perspective of its own parent.
72
# That way it will be a directory in the top level of the zip
74
if filename[-1] == os.sep: filename = filename[:-1]
75
splitpath = filename.rsplit(os.sep, 1)
76
if len(splitpath) == 1:
80
zipbasepath = splitpath[0]
81
paths = [splitpath[1]]
82
zipfilename = paths[0]
85
req.content_type = zip_mimetype
86
# zipfilename is some filename. Strip trailing slash or extension,
90
elif zipfilename[-1] == '/':
91
zipfilename = zipfilename[:-1]
92
elif '.' in zipfilename:
93
zipfilename = zipfilename[:zipfilename.rindex('.')]
95
req.headers_out["Content-Disposition"] = ("attachment; filename=" +
97
zipfile = StringIO.StringIO()
98
zipmod.make_zip(zipbasepath, paths, zipfile)
100
req.write(zipfile.getvalue())
102
req.content_type = default_mimetype
103
req.sendfile(filename)