~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
34
37
def determine_file_type(filename):
35
38
    filetype = mimetypes.guess_type(filename)[0]
36
39
    if filetype is None:
37
 
         filetype = ivle.mimetypes.DEFAULT_MIMETYPE
 
40
        filetype = ivle.mimetypes.DEFAULT_MIMETYPE
38
41
    return filetype
39
42
 
40
43
def throw_error(message, extra={}):
41
44
    error = {'error': message}
42
45
    error.update(extra)
43
 
    print cjson.encode(error)
 
46
    print json.dumps(error)
44
47
    sys.exit(0)
45
48
 
46
49
parser = OptionParser()
112
115
    else:
113
116
        if not download and \
114
117
           determine_file_type(filename) in ivle.conf.app.server.interpreters:
115
 
            throw_error('is-executable', {'path': filename})
 
118
            throw_error('is-executable', {'path': filename.decode('utf-8')})
116
119
 
117
120
        if not download and (
118
121
            (ivle.conf.app.server.blacklist_served_filetypes and \
138
141
    zipfile = StringIO.StringIO()
139
142
    zipmod.make_zip(zipbasepath, paths, zipfile)
140
143
 
141
 
    print cjson.encode({'type': zip_mimetype,
142
 
                        'name': zipfilename})
 
144
    print json.dumps({'type': zip_mimetype,
 
145
                      'name': zipfilename.decode('utf-8'),
 
146
                      'size': len(zipfile.getvalue()),
 
147
                      })
143
148
 
144
149
    stream = zipfile
145
150
    stream.seek(0)
146
151
else:
147
152
 
148
 
    print cjson.encode({'type': determine_file_type(filename),
149
 
                        'name': os.path.basename(filename)})
 
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
                      })
150
157
    stream = open(filename)
151
 
    
 
158
 
152
159
next = stream.read(1024)
153
160
while next:
154
161
    sys.stdout.write(next)