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

« back to all changes in this revision

Viewing changes to services/serveservice

Modified the edit view of a worksheet to allow editing of the format
for the worksheet

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