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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#!/usr/bin/python

# IVLE - Informatics Virtual Learning Environment
# Copyright (C) 2007-2008 The University of Melbourne
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

# Script: serveservice
# Author: Thomas Conway
# Date:   6/3/2007

# A CGI script for serving files.

import mimetypes
import os
import conf
import StringIO
import urlparse

from common import (cgirequest, studpath)
from common import zip as zipmod

req = cgirequest.CGIRequest()
req.install_error_handler()

# Work out the parts of the URL
url = urlparse.urlparse(req.path)
querystr = url[4]
urlpath = url[2]
filename = studpath.url_to_jailpaths(urlpath)[2]

default_mimetype = "application/octet-stream"
zip_mimetype = "application/zip"

zipmode = False
zipbasepath = None
zipfilename = None
path = None

# If any "path=" variables have been supplied, bring these into a list and
# make a zip file instead.
fields = req.get_fieldstorage()
paths = fields.getlist("path")
if len(paths) > 0:
    zipmode = True
    zipbasepath = filename
    zipfilename = os.path.basename(zipbasepath)
    #for i in range(0, len(paths)):
        #paths[i] = paths[i].value
else:
    if filename is None:
        req.throw_error(req.HTTP_NOT_FOUND,
            "The path specified is invalid.")
    elif not os.access(filename, os.R_OK):
        req.throw_error(req.HTTP_NOT_FOUND,
            "The specified file (%s) does not exist." % urlpath)
     # If it's a directory, serve as a zip file
    if os.path.isdir(filename):
        zipmode = True
        # Zip it from the perspective of its own parent.
        # That way it will be a directory in the top level of the zip
        # file.
        if filename[-1] == os.sep: filename = filename[:-1]
        splitpath = filename.rsplit(os.sep, 1)
        if len(splitpath) == 1:
            zipbasepath = ''
            paths = [filename]
        else:
            zipbasepath = splitpath[0]
            paths = [splitpath[1]]
        zipfilename = paths[0]

if zipmode:
    req.content_type = zip_mimetype
    # zipfilename is some filename. Strip trailing slash or extension,
    # and add ".zip".
    if zipfilename == '':
        zipfilename = "files"
    elif zipfilename[-1] == '/':
        zipfilename = zipfilename[:-1]
    elif '.' in zipfilename:
        zipfilename = zipfilename[:zipfilename.rindex('.')]
    zipfilename += ".zip"
    req.headers_out["Content-Disposition"] = ("attachment; filename=" +   
        zipfilename)
    zipfile = StringIO.StringIO()
    zipmod.make_zip(zipbasepath, paths, zipfile)
        
    req.write(zipfile.getvalue())
else:
    #req.content_type = default_mimetype
    req.sendfile(filename)