summaryrefslogtreecommitdiff
path: root/slideapi.py
blob: b2c13248a401e668647b49dd8da99d39945fbfdf (plain)
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
#!/usr/bin/env python
#
# This file is part of the LibreOffice project.
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#

import os, json, time
from cherrypy2 import _cpwsgiserver3
from bottle import get, post, auth_basic, run, static_file, request
from bottle import debug, view, HTTPError, server_names, ServerAdapter
from subprocess import call
from lpod.document import odf_get_document, odf_new_document, ODF_MANIFEST
import tempfile

# config
#
# we understand:
# local_conf["users"], yielding {<username>: {"password": <password>, "description": <description>}, ... }
# local_conf["thumbnails"], yielding {"soffice": <path>, "convert": <path>, "thumbnail_size": <128x128>}
# local_conf["tags"], yielding { <tagname>: <tag_desc, ... }
#
local_conf=json.loads(open('local.json').read()) if os.path.exists('local.json') else {}
root=local_conf["filestore"]
tmp_root=local_conf["tmpstore"]

class Decks:
    slide = None
    deck = None

def validate_auth(user, password):
    if "users" in local_conf:
        user_dict = local_conf["users"]
        if user in user_dict and 'password' in user_dict[user]:
            return password == user_dict[user]['password']
    return False

# -------------------------------------------------------------------

def get_users():
    return {user: data['description'] for user, data in local_conf["users"].items()}

@get('/api/users/')
def users():
    return get_users()

@get('/users/')
@view('users')
def users():
    return dict(users=get_users())

# -------------------------------------------------------------------

def get_tags():
    return local_conf["tags"]

@get('/api/tags/')
def tags():
    return get_tags()

@get('/tags/')
@view('tags')
def tags():
    return dict(tags=get_tags())

# -------------------------------------------------------------------

def get_decks(user):
    path = "%s/%s/decks/" % (root,user)
    if not os.path.isdir(path):
        return []
    return [deck for deck in os.listdir(path) if os.path.isdir(path+deck)]

def get_groups(user):
    path = "%s/%s/groups/" % (root,user)
    if not os.path.isdir(path):
        return []
    return [group for group in os.listdir(path) if os.path.isdir(path+group)]

def get_lastrev(user,part,deck):
    path = "%s/%s/%s/%s/" % (root,user,part,deck)
    if not os.path.isdir(path):
        return 0
    # get last revision
    return int(sorted([rev for rev in os.listdir(path) if os.path.isdir(path+rev) and rev != 'latest'], key=int)[-1])

@get('/api/users/<user>/')
def list_decks(user):
    return json.dumps(['decks/'+deck for deck in get_decks(user)] + ['groups/'+group for group in get_groups(user)])

@get('/api/users/<user>/decks/')
def list_decks(user):
    return json.dumps(get_decks(user))

@get('/api/users/<user>/groups/')
def list_groups(user):
    return json.dumps(get_groups(user))

@get('/users/<user>/')
@view('decksgroups')
def list_decksgroups(user):
    return dict(decks=get_decks(user), groups=get_groups(user))

@get('/users/<user>/decks/')
@view('decks')
def list_decks(user):
    return dict(decks=get_decks(user))

@get('/users/<user>/groups/')
@view('groups')
def list_groups(user):
    return dict(groups=get_groups(user))

@get('/users/<user>/<part>/<deck>/thumbnail.png')
@get('/api/users/<user>/<part>/<deck>/thumbnail.png')
def get_thumbnail(user, part, deck):
    path = "%s/%s/%s/%s/" % (root,user,part,deck)
    last_rev = get_lastrev(user,part,deck)
    # serve thumbnail from that rev, first slide
    return static_file('thumbnail.png', root=path+str(last_rev)+'/0/', mimetype='image/png')

# -------------------------------------------------------------------

def get_revs(user,part,deck):
    path = "%s/%s/%s/%s/" % (root,user,part,deck)
    if not os.path.isdir(path):
        return []
    return sorted([rev for rev in os.listdir(path) if os.path.isdir(path+rev) and rev != 'latest'], key=int)

@get('/api/users/<user>/<part>/<deck>/')
def list_revs(user,part,deck):
    return json.dumps(get_revs(user,part,deck))

@get('/users/<user>/<part>/<deck>/')
@view('revs')
def list_revs(user,part,deck):
    return dict(revs=get_revs(user,part,deck))

@post('/users/<user>/decks/<deck>')
@post('/api/users/<user>/decks/<deck>')
@auth_basic(validate_auth, realm='upload')
def upload_deck(user,deck):
    path = "%s/%s/decks/%s/" % (root,user,deck)
    new_rev = get_lastrev(user,'decks',deck) + 1
    new_path = path+str(new_rev)

    if os.path.isdir(new_path):
        raise HTTPError(body='inconsistent repo, bailing out')

    if request.auth[0] != user:
        raise HTTPError(body='invalid user or insufficient rights, bailing out')

    os.makedirs(new_path)
    upload_path = new_path

    # TODO: Add all required parameters here
    tag     = request.forms.get('tag')
    content = request.files.get('file')
    # TODO: Create proper paths for uploads & Check for upload errors
    out = open(upload_path+'/deck.odp', 'wb')
    while True:
        bits = content.file.read(10240)
        if not bits:
            break
        out.write(bits)
    out.close()

    out = open(upload_path+'/meta.json', 'wb')
    json.dump({'tag': tag,
               'server_version': '1',
               'upload_filename': content.filename},
              out)

    # update link to latest rev
    if os.path.exists(path+'latest'):
        os.unlink(path+'latest')
    os.symlink(str(new_rev), path+'latest')

    # thumbnail generation happens asynchronously via updatedeck.py
    return 'Success:'+tag+':'+content.filename

@post('/users/<user>/groups/<deck>')
@post('/api/users/<user>/groups/<deck>')
@auth_basic(validate_auth, realm='upload')
def upload_group(user,deck):
    path = "%s/%s/groups/%s/" % (root,user,deck)
    new_rev = get_lastrev(user,'groups',deck) + 1
    new_path = path+str(new_rev)

    if os.path.isdir(new_path):
        raise HTTPError(body='inconsistent repo, bailing out')

    if request.auth[0] != user:
        raise HTTPError(body='invalid user or insufficient rights, bailing out')

    properties = request.json
    if properties[u'name'] != deck:
        raise HTTPError(body='Failue: url / name mismatch')

    os.makedirs(new_path)
    upload_path = new_path

    # link slides/decks
    for index, slide in enumerate(properties['slides']):
        if not os.path.isdir(root+'/'+slide):
            raise HTTPError(body='non-existing slide %d at path %s, bailing out' % (index, root+slide)) # todo: cleanup!!
        os.symlink('../../../../'+slide, upload_path+'/'+str(index))

    out = open(upload_path+'/meta.json', 'wb')
    json.dump({'server_version': '1',
               'payload': properties},
              out)

    return 'Success'


@get('/users/<user>/<part>/<deck>/<rev:int>/thumbnail.png')
@get('/api/users/<user>/<part>/<deck>/<rev:int>/thumbnail.png')
def get_thumbnail(user, part, deck, rev):
    path = "%s/%s/%s/%s/%d/" % (root,user,part,deck,rev)
    # serve thumbnail from that rev, first slide
    return static_file('thumbnail.png', root=path+'/0/', mimetype='image/png')

# get the (chosen) slides from Decks as one single deck
def get_slides_deck(decks_and_slides, output):
    new_deck = odf_new_document('presentation')
    new_deck.delete_styles()
    new_deck_body = new_deck.get_body()
    new_deck_manifest = new_deck.get_part(ODF_MANIFEST)

    for deck_and_slides in decks_and_slides:
        deck = odf_get_document(deck_and_slides.deck)
        if deck == None:
            continue
        new_deck.merge_styles_from(deck)
        deck_body = deck.get_body()
        deck_manifest = deck.get_part(ODF_MANIFEST)
        for slide in deck_and_slides.slides:
            try:
                slide_position = int(slide) - 1 # Assuming index starts with 1
                source_slide = deck_body.get_draw_page(position = slide_position)
            except:
                continue
            if source_slide == None:
                continue

            # Copy Images
            for image in source_slide.get_images():
                uri = image.get_url()
                media_type = deck_manifest.get_media_type(uri)
                new_deck_manifest.add_full_path(uri, media_type)
                new_deck.set_part(uri, deck.get_part(uri))

            new_deck_body.append(source_slide.clone())

    new_deck.save(target=output, pretty=True)

@get('/users/<user>/<part>/<deck>/<rev:int>/deck.odp')
@get('/api/users/<user>/<part>/<deck>/<rev:int>/deck.odp')
def send_deck(user, part, deck, rev):
    path = "%s/%s/%s/%s/%d/" % (root,user,part,deck,rev)
    slides = None
    try:
        slides=request.query['slides']
        print slides
    except:
        pass
    if slides != None:
        mode, tmp_file = tempfile.mkstemp(suffix='.odp', prefix='temp', dir=tmp_root)
        d = Decks()
        d.slides = slides.split(',')
        d.deck = path+'/deck.odp'
        get_slides_deck([d],tmp_file)
        filename = tmp_file
        if filename.rfind('/') >= 0:
            filename = filename[filename.rfind('/')+1:]
        print filename
        return static_file(filename, root=tmp_root, download='deck.odp')
    return static_file('deck.odp', root=path, mimetype='text/xml', download='deck.odp')

# -------------------------------------------------------------------

def get_slides(user,part,deck,rev):
    path = "%s/%s/%s/%s/%d/" % (root,user,part,deck,rev)
    if not os.path.isdir(path):
        return []
    return sorted([slide for slide in os.listdir(path) if os.path.isdir(path+slide)], key=int)

def get_revmeta(user,part,deck,rev):
    path = "%s/%s/%s/%s/%d/" % (root,user,part,deck,rev)
    comment = open(path+'comment').read() if os.path.exists(path+'comment') else ''
    meta = json.loads(open(path+'meta.json').read()) if os.path.exists(path+'meta.json') else {}
    return {'CreationDate': time.strftime("%a, %d %b %Y %H:%M:%S GMT",
                                          time.gmtime(os.stat(path).st_ctime)),
            'CommitComment': comment,
            'Meta': meta}

@get('/api/users/<user>/<part>/<deck>/<rev:int>/')
def list_slides(user, part, deck, rev):
    return json.dumps(get_slides(user,part,deck,rev))

@get('/users/<user>/<part>/<deck>/<rev:int>/')
@view('slides')
def list_slides(user, part, deck, rev):
    return dict(slides=get_slides(user,part,deck,rev), revmeta=get_revmeta(user,part,deck,rev))

@get('/users/<user>/<part>/<deck>/<rev:int>/<slide:int>/thumbnail.png')
@get('/api/users/<user>/<part>/<deck>/<rev:int>/<slide:int>/thumbnail.png')
def get_thumbnail(user, part, deck, rev, slide):
    path = "%s/%s/%s/%s/%d/%d/" % (root,user,part,deck,rev,slide)
    return static_file('thumbnail.png', root=path, mimetype='image/png')

@get('/api/users/<user>/<part>/<deck>/<rev:int>/meta.json')
def list_revmeta(user,part,deck,rev):
    return get_revmeta(user,part,deck,rev)

# -------------------------------------------------------------------

def get_slidemeta(user,part,deck,rev,slide):
    path = "%s/%s/%s/%s/%d/%d/" % (root,user,part,deck,rev,slide)
    return json.loads(open(path+'meta.json').read()) if os.path.exists(path+'meta.json') else {}

@get('/api/users/<user>/<part>/<deck>/<rev:int>/<slide:int>/meta.json')
def list_slidemeta(user, part, deck, rev, slide):
    return get_slidemeta(user, part, deck, rev, slide)

@get('/users/<user>/<part>/<deck>/<rev:int>/<slide:int>/slide.odp')
@get('/api/users/<user>/<part>/<deck>/<rev:int>/<slide:int>/slide.odp')
def send_slide(user, part, deck, rev, slide):
    path = "%s/%s/%s/%s/%d/%d/" % (root,user,part,deck,rev,slide)
    return static_file(str(slide)+'.odp', root=path, mimetype='text/xml')

@get('/')
def home_page():
    return '<html><body>Welcome</body></html>'

@get('/users/<username>/edit')
@view('app')
def app_page(username):
    return dict(user=username)

# -------------------------------------------------------------------

class SSLInterface(ServerAdapter):
    def run(self, handler):
        server = _cpwsgiserver3.CherryPyWSGIServer((self.host, self.port), handler)
        #cert = 'server.pem'
        #server.ssl_certificate = cert
        #server.ssl_private_key = cert
        try:
            server.start()
        finally:
            server.stop()

server_names['sslinterface'] = SSLInterface

def main():
    debug(True)
    run(host='localhost', port=8080, reloader=True, quiet=False, server='sslinterface')

if __name__ == "__main__":
    main()