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

« back to all changes in this revision

Viewing changes to services/serveservice

  • Committer: William Grant
  • Date: 2009-02-17 00:01:10 UTC
  • mto: (1099.1.143 new-dispatch)
  • mto: This revision was merged to the branch mainline in revision 1100.
  • Revision ID: grantw@unimelb.edu.au-20090217000110-mysp1vl5n29uwm2v
Change serveservice's interface - it now uses a JSON-based non-CGI one.
This breaks serving things.

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
# along with this program; if not, write to the Free Software
18
18
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19
19
 
20
 
# Script: serveservice
21
 
# Author: Thomas Conway
22
 
# Date:   6/3/2007
23
 
 
24
 
# A CGI script for serving files.
 
20
# Author: Thomas Conway, Will Grant
25
21
 
26
22
import mimetypes
27
23
import os
 
24
import sys
28
25
import StringIO
29
26
import urlparse
 
27
from optparse import OptionParser
 
28
 
 
29
import cjson
30
30
 
31
31
from ivle import (cgirequest, studpath)
32
32
from ivle import zip as zipmod
33
33
 
34
 
req = cgirequest.CGIRequest()
35
 
req.install_error_handler()
36
 
 
37
 
# Work out the parts of the URL
38
 
url = urlparse.urlparse(req.path)
39
 
querystr = url[4]
40
 
urlpath = url[2]
41
 
filename = studpath.url_to_jailpaths(urlpath)[2]
 
34
def throw_error(message):
 
35
    print cjson.encode({'error': message})
 
36
    sys.exit(0)
 
37
 
 
38
parser = OptionParser()
 
39
parser.add_option('-d', '--download', dest='download', action='store_true',
 
40
                  help='force download, not execution, of the paths')
 
41
(options, args) = parser.parse_args()
 
42
 
 
43
# Detect download mode. Download mode zips any multiple selection,
 
44
# and does not execute CGI scripts.
 
45
if options.download:
 
46
    download = True
 
47
 
 
48
    dir = os.path.dirname(args[0])
 
49
 
 
50
    # Mangle the paths - we want the basename of their dirname in front.
 
51
    paths = []
 
52
    for path in args:
 
53
        assert os.path.dirname(path) == dir
 
54
        paths.append(os.path.join(os.path.basename(dir),
 
55
                                  os.path.basename(path)))
 
56
    dir = os.path.dirname(dir)
 
57
else:
 
58
    download = False
 
59
    paths = args
42
60
 
43
61
default_mimetype = "application/octet-stream"
44
62
zip_mimetype = "application/zip"
46
64
zipmode = False
47
65
zipbasepath = None
48
66
zipfilename = None
49
 
path = None
50
67
 
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")
55
 
if len(paths) > 0:
 
68
# If multiple paths have been specified, zip them up.
 
69
if len(paths) > 1:
56
70
    zipmode = True
57
 
    zipbasepath = filename
 
71
    zipbasepath = dir
58
72
    zipfilename = os.path.basename(zipbasepath)
59
 
    #for i in range(0, len(paths)):
60
 
        #paths[i] = paths[i].value
61
73
else:
62
 
    if filename is None:
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." % urlpath)
68
 
     # If it's a directory, serve as a zip file
 
74
    filename = paths[0]
 
75
    if not os.access(filename, os.R_OK):
 
76
        throw_error('not-found')
 
77
 
 
78
    # If it's a directory, serve as a zip file
69
79
    if os.path.isdir(filename):
70
80
        zipmode = True
71
81
        # Zip it from the perspective of its own parent.
79
89
        else:
80
90
            zipbasepath = splitpath[0]
81
91
            paths = [splitpath[1]]
82
 
        zipfilename = paths[0]
 
92
        zipfilename = filename
83
93
 
84
94
if zipmode:
85
 
    req.content_type = zip_mimetype
86
95
    # zipfilename is some filename. Strip trailing slash or extension,
87
96
    # and add ".zip".
88
97
    if zipfilename == '':
92
101
    elif '.' in zipfilename:
93
102
        zipfilename = zipfilename[:zipfilename.rindex('.')]
94
103
    zipfilename += ".zip"
95
 
    req.headers_out["Content-Disposition"] = ("attachment; filename=" +   
96
 
        zipfilename)
 
104
    #req.headers_out["Content-Disposition"] = ("attachment; filename=" +   
 
105
    #    zipfilename) # TODO
97
106
    zipfile = StringIO.StringIO()
98
107
    zipmod.make_zip(zipbasepath, paths, zipfile)
99
108
        
100
 
    req.write(zipfile.getvalue())
 
109
    print cjson.encode({'content': zipfile.getvalue(), 'type': zip_mimetype})
101
110
else:
102
 
    #req.content_type = default_mimetype
103
 
    req.sendfile(filename)
 
111
    print cjson.encode({'content': open(filename).read()})