|
| 1 | +import io |
| 2 | + |
| 3 | +import db.events |
| 4 | +import flask |
| 5 | +import sqlalchemy as sa |
| 6 | + |
| 7 | +import db |
| 8 | +import db.programs |
| 9 | +import srv.auth |
| 10 | + |
| 11 | +from srv import app |
| 12 | + |
| 13 | +FILE_MAGIC_NUMBERS = { |
| 14 | + b'%PDF-' : 'application/pdf', |
| 15 | + b'\xFF\xD8\xFF' : 'image/jpeg', |
| 16 | + b'\x89PNG\r\n\x1a\n' : 'image/png', |
| 17 | +} |
| 18 | + |
| 19 | +@app.route('/programs/speakers') |
| 20 | +def programs_speaker_list_page(): |
| 21 | + isAdmin = srv.auth.isValid(flask.request) |
| 22 | + if isAdmin is False: |
| 23 | + return srv.auth.respondInValid() |
| 24 | + |
| 25 | + query = sa.select( |
| 26 | + db.events.Profile.pk, |
| 27 | + db.events.Profile.name, |
| 28 | + db.events.Profile.introduction, |
| 29 | + db.events.Profile.photoBlob, |
| 30 | + db.programs.Proposal.title, |
| 31 | + db.programs.Proposal.abstract, |
| 32 | + ).where( |
| 33 | + db.programs.Proposal.session == 'keynote' |
| 34 | + ).outerjoin( |
| 35 | + db.programs.Proposal, |
| 36 | + db.programs.Proposal.name == db.events.Profile.name, |
| 37 | + ).order_by( |
| 38 | + db.events.Profile.pk |
| 39 | + ) |
| 40 | + |
| 41 | + with db.engine.connect() as connection: |
| 42 | + cursor = connection.execute(query) |
| 43 | + |
| 44 | + return flask.render_template( |
| 45 | + '/speakers/speakers.djhtml', |
| 46 | + pageTitle = 'Speakers', |
| 47 | + pageDesc = 'List of all submitted speakers', |
| 48 | + baseURL = '/speakers', |
| 49 | + isAdmin = isAdmin, |
| 50 | + cursor = cursor |
| 51 | + ) |
| 52 | + |
| 53 | + |
| 54 | +@app.route('/programs/speakers/<int:pk>') |
| 55 | +def programs_speakers_file_download(pk): |
| 56 | + query = sa.select( |
| 57 | + db.events.Profile.photoBlob, |
| 58 | + ).where( |
| 59 | + db.events.Profile.pk == pk, |
| 60 | + ) |
| 61 | + |
| 62 | + with db.engine.connect() as connection: |
| 63 | + blob = connection.execute(query).scalar() |
| 64 | + |
| 65 | + if blob is None: |
| 66 | + return 'File Not Found', 404 |
| 67 | + |
| 68 | + for magic, mime_type in FILE_MAGIC_NUMBERS.items(): |
| 69 | + if blob.startswith(magic): break |
| 70 | + mime_type = 'application/octet-stream' |
| 71 | + |
| 72 | + download_name = '{slug}.{ext}'.format( |
| 73 | + slug = pk, |
| 74 | + ext = mime_type.split('/')[1] |
| 75 | + ) |
| 76 | + return flask.send_file( |
| 77 | + io.BytesIO(blob), |
| 78 | + #as_attachment = True, |
| 79 | + download_name = download_name, |
| 80 | + mimetype = mime_type, |
| 81 | + ),200 |
| 82 | + |
0 commit comments