~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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#!/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

# Author: Thomas Conway, Will Grant

import mimetypes
import os
import sys
import StringIO
from optparse import OptionParser

import cjson

from ivle import zip as zipmod
import ivle.conf.app.server
import ivle.mimetypes

def determine_file_type(filename):
    filetype = mimetypes.guess_type(filename)[0]
    if filetype is None:
         filetype = ivle.mimetypes.DEFAULT_MIMETYPE
    return filetype

def throw_error(message, extra={}):
    error = {'error': message}
    error.update(extra)
    print cjson.encode(error)
    sys.exit(0)

parser = OptionParser()
parser.add_option('-d', '--download', dest='download', action='store_true',
                  help='force download, not execution, of the paths')
(options, args) = parser.parse_args()

# Detect download mode. Download mode zips any multiple selection,
# and does not execute CGI scripts.
if options.download:
    download = True
    # paths is filled later.
else:
    download = False
    assert len(args) == 1

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

zipmode = False
zipbasepath = None
zipfilename = None

# If multiple paths have been specified, zip them up.
if len(args) > 1:
    # Mangle the paths - we want the basename of their dirname in front.
    paths = []
    dir = os.path.dirname(args[0])
    for path in args:
        assert os.path.dirname(path) == dir
        paths.append(os.path.join(os.path.basename(dir),
                                  os.path.basename(path)))
    dir = os.path.dirname(dir)
    zipmode = True
    zipbasepath = dir
    zipfilename = os.path.basename(zipbasepath)
else:
    paths = args
    filename = paths[0]
    if not os.access(filename, os.F_OK):
        # The given path doesn't exist. CGI lets us backtrack and put the path
        # elements through which we pass into PATH_INFO, so we try that.
        while not os.access(filename, os.F_OK):
            filename, path_info_frag = os.path.split(filename)

        # We now have a file that exists, but is it something that we're allowed
        # to execute? If not, we should 404 anyway.
        if determine_file_type(filename) not in ivle.conf.app.server.interpreters:
            throw_error('not-found')

    # If it's a directory, serve as a zip file
    if os.path.isdir(filename):
        if not download:
            # Not giving a directory listing - this is visible to everyone.
            throw_error('is-directory')
        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 = filename
    else:
        if not download and \
           determine_file_type(filename) in ivle.conf.app.server.interpreters:
            throw_error('is-executable', {'path': filename})

        if not download and (
            (ivle.conf.app.server.blacklist_served_filetypes and \
                determine_file_type(filename) in \
                ivle.conf.app.server.served_filetypes_blacklist) or \
            (ivle.conf.app.server.served_filetypes_whitelist and \
                determine_file_type(filename) not in \
                ivle.conf.app.server.served_filetypes_whitelist)):
            throw_error('forbidden')

if zipmode:
    # 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) # TODO
    zipfile = StringIO.StringIO()
    zipmod.make_zip(zipbasepath, paths, zipfile)

    print cjson.encode({'type': zip_mimetype,
                        'name': zipfilename})

    stream = zipfile
    stream.seek(0)
else:

    print cjson.encode({'type': determine_file_type(filename),
                        'name': os.path.basename(filename)})
    stream = open(filename)
    
next = stream.read(1024)
while next:
    sys.stdout.write(next)
    next = stream.read(1024)