-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
26 lines (24 loc) · 1.21 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
# from doi_lookup_sql import get_pdf_from_doi
from doi_lookup_csv import get_pdf_from_doi
from flask import Flask, send_file, redirect, url_for, send_from_directory
app = Flask(__name__)
@app.route('/', defaults={'path': ''})
@app.route('/<path:path>')
def catch_all(path):
try:
pdf_details = get_pdf_from_doi(path)
except:
return 'DOI not found: %s' % path
else:
response = send_from_directory(directory = pdf_details["folder"],
path = pdf_details["filepath"],
mimetype = 'application/pdf',
as_attachment = True)
# https://stackoverflow.com/questions/41543951/how-to-change-downloading-name-in-flask
# https://stackoverflow.com/questions/38564525/chrome-embedded-pdf-download-filename
response.headers["x-filename"] = pdf_details["filepath"]
response.headers["x-suggested-filename"] = pdf_details["filepath"]
response.headers["Content-Disposition"] = 'inline; filename="' + pdf_details["filepath"] + '"'
response.headers["Access-Control-Expose-Headers"] = 'x-filename'
response.headers["Access-Control-Expose-Headers"] = 'x-suggested-filename'
return response