-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
57 lines (45 loc) · 2.12 KB
/
app.py
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
from flask import Flask, request, send_from_directory
from flask import make_response
from nau.course.certificate.course_certificate_to_pdf import CourseCertificateToPDF, CourseCertificateToImage
from nau.course.certificate.configuration import Configuration
app = Flask(__name__, static_folder='static')
def read_config():
configuration = Configuration('config.yml')
config = configuration.config()
if not config:
configuration = Configuration('default-config.yml')
config = configuration.config()
return config
def convert_certificate_to_pdf(content_disposition, path, query_string):
config = read_config()
course_certificate_to_pdf = CourseCertificateToPDF(config, path, query_string)
certificate_file_name = course_certificate_to_pdf.get_filename()
binary_pdf = course_certificate_to_pdf.convert()
response = make_response(binary_pdf)
response.headers['Content-Type'] = 'application/pdf'
response.headers['Content-Disposition'] = '{}; filename={}'.format(content_disposition, certificate_file_name)
return response
@app.route('/inline/<path:path>')
def inline(path):
return convert_certificate_to_pdf("inline", path, request.query_string)
@app.route('/attachment/<path:path>')
def attachment(path):
return convert_certificate_to_pdf("attachment", path, request.query_string)
@app.route('/favicon.ico')
@app.route('/robots.txt')
@app.route('/sitemap.xml')
def static_from_root():
return send_from_directory(app.static_folder, request.path[1:])
@app.route('/image/<path:path>')
def image(path):
config = read_config()
course_certificate_to_image = CourseCertificateToImage(config, path, request.query_string)
certificate_file_name = course_certificate_to_image.get_filename()
binary_result = course_certificate_to_image.convert()
response = make_response(binary_result)
response.headers['Content-Type'] = 'image/' + course_certificate_to_image.image_format()
response.headers['Content-Disposition'] = '{}; filename={}'.format('inline', certificate_file_name)
return response
# startup the Flask web server
if __name__ == '__main__':
app.run()