~azzar1/unity/add-show-desktop-key

« back to all changes in this revision

Viewing changes to www/apps/server/__init__.py

  • Committer: dcoles
  • Date: 2008-03-31 01:10:43 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:720
download: Fixing the download button.
    We now run download in the jail so that we don't encounter permission 
    problems.  Since this is very similar to "serve" we now extend serveservice 
    to do the work of download and use the serve code to do download.

    w/a/s/__init__.py: Overloaded servefile to support downloads
    w/a/d/__init__.py: Use servefile from server rather than old implementaion
    s/serveservice: Added support for download of directories (as zip files)
    l/c/zip.py: Now uses paths not ivle_urls

Show diffs side-by-side

added added

removed removed

Lines of Context:
33
33
 
34
34
serveservice_path = "/opt/ivle/scripts/serveservice"
35
35
 
 
36
# Serve all files as application/octet-stream so the browser presents them as
 
37
# a download.
 
38
default_mimetype = "application/octet-stream"
 
39
zip_mimetype = "application/zip"
 
40
 
36
41
def handle(req):
37
42
    """Handler for the Server application which serves pages."""
38
43
    req.write_html_head_foot = False
62
67
        # their own files, and can access all of them).
63
68
        studpath.authorize(req)
64
69
 
65
 
def serve_file(req, owner, filename):
 
70
def serve_file(req, owner, filename, download=False):
66
71
    """Serves a file, using one of three possibilities: interpreting the file,
67
72
    serving it directly, or denying it and returning a 403 Forbidden error.
68
73
    No return value. Writes to req (possibly throwing a server error exception
71
76
    req: An IVLE request object.
72
77
    owner: Username of the user who owns the file being served.
73
78
    filename: Filename in the local file system.
 
79
    download:  Should the file be viewed in browser or downloaded
74
80
    """
75
81
    # Authorize access. If failure, this throws a HTTP_FORBIDDEN error.
76
82
    authorize(req)
77
83
 
78
84
    # First get the mime type of this file
 
85
    # If download then use "application/octet-stream" type to force the browser 
 
86
    # to download the file.
79
87
    # (Note that importing common.util has already initialised mime types)
80
 
    (type, _) = mimetypes.guess_type(filename)
81
 
    if type is None:
82
 
        type = conf.mimetypes.default_mimetype
 
88
    if download:
 
89
        if os.path.isdir(filename):
 
90
            type = zip_mimetype
 
91
        else:
 
92
            type = default_mimetype
 
93
        req.headers_out["Content-Disposition"] = "attachment"
 
94
    else:
 
95
        (type, _) = mimetypes.guess_type(filename)
 
96
        if type is None:
 
97
            type = conf.mimetypes.default_mimetype
83
98
 
84
99
    # If this type is to be interpreted
85
 
    if os.path.isdir(filename):
 
100
    if os.path.isdir(filename) and not download:
86
101
        # 403 Forbidden error for visiting a directory
87
102
        # (Not giving a directory listing, since this can be seen by
88
103
        # the world at large. Directory contents are private).