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

« back to all changes in this revision

Viewing changes to services/serveservice

  • Committer: William Grant
  • Date: 2012-06-28 01:52:02 UTC
  • Revision ID: me@williamgrant.id.au-20120628015202-f6ru7o367gt6nvgz
Hah

Show diffs side-by-side

added added

removed removed

Lines of Context:
25
25
import StringIO
26
26
from optparse import OptionParser
27
27
 
28
 
import cjson
 
28
try:
 
29
    import json
 
30
except ImportError:
 
31
    import simplejson as json
29
32
 
30
33
from ivle import zip as zipmod
31
34
import ivle.conf.app.server
 
35
import ivle.mimetypes
32
36
 
33
37
def determine_file_type(filename):
34
38
    filetype = mimetypes.guess_type(filename)[0]
35
39
    if filetype is None:
36
 
         filetype = ivle.conf.mimetypes.default_mimetype
 
40
        filetype = ivle.mimetypes.DEFAULT_MIMETYPE
37
41
    return filetype
38
42
 
39
43
def throw_error(message, extra={}):
40
44
    error = {'error': message}
41
45
    error.update(extra)
42
 
    print cjson.encode(error)
 
46
    print json.dumps(error)
43
47
    sys.exit(0)
44
48
 
45
49
parser = OptionParser()
111
115
    else:
112
116
        if not download and \
113
117
           determine_file_type(filename) in ivle.conf.app.server.interpreters:
114
 
            throw_error('is-executable', {'path': filename})
 
118
            throw_error('is-executable', {'path': filename.decode('utf-8')})
115
119
 
116
 
        if (ivle.conf.app.server.blacklist_served_filetypes and \
 
120
        if not download and (
 
121
            (ivle.conf.app.server.blacklist_served_filetypes and \
117
122
                determine_file_type(filename) in \
118
123
                ivle.conf.app.server.served_filetypes_blacklist) or \
119
124
            (ivle.conf.app.server.served_filetypes_whitelist and \
120
125
                determine_file_type(filename) not in \
121
 
                ivle.conf.app.server.served_filetypes_whitelist):
 
126
                ivle.conf.app.server.served_filetypes_whitelist)):
122
127
            throw_error('forbidden')
123
128
 
124
129
if zipmode:
136
141
    zipfile = StringIO.StringIO()
137
142
    zipmod.make_zip(zipbasepath, paths, zipfile)
138
143
 
139
 
    print cjson.encode({'type': zip_mimetype,
140
 
                        'name': zipfilename})
141
 
    print zipfile.getvalue()
 
144
    print json.dumps({'type': zip_mimetype,
 
145
                      'name': zipfilename.decode('utf-8'),
 
146
                      'size': len(zipfile.getvalue()),
 
147
                      })
 
148
 
 
149
    stream = zipfile
 
150
    stream.seek(0)
142
151
else:
143
 
    print cjson.encode({'type': determine_file_type(filename),
144
 
                        'name': os.path.basename(filename)})
145
 
    print open(filename).read()
 
152
 
 
153
    print json.dumps({'type': determine_file_type(filename),
 
154
                      'name': os.path.basename(filename).decode('utf-8'),
 
155
                      'size': os.path.getsize(filename),
 
156
                      })
 
157
    stream = open(filename)
 
158
 
 
159
next = stream.read(1024)
 
160
while next:
 
161
    sys.stdout.write(next)
 
162
    next = stream.read(1024)