forked from SwiftLaTeX/Texlive-Ondemand
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
76 lines (57 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
from flask import Flask, send_file, make_response, send_from_directory
from threading import Lock
import time
import os.path
import pykpathsea_xetex
import pykpathsea_pdftex
from flask_cors import cross_origin
import re
import os
app = Flask(__name__)
regex = re.compile(r'[^a-zA-Z0-9 _\-\.]')
def san(name):
return regex.sub('', name)
@app.route('/xetex/<int:fileformat>/<filename>')
@cross_origin()
def xetex_fetch_file(fileformat, filename):
filename = san(filename)
url = None
if filename == "swiftlatexxetex.fmt" or filename == "xetexfontlist.txt":
url = filename
else:
url = pykpathsea_xetex.find_file(filename, fileformat)
if url is None or not os.path.isfile(url):
return "File not found", 301
else:
response = make_response(send_file(url, mimetype='application/octet-stream'))
response.headers['fileid'] = os.path.basename(url)
response.headers['Access-Control-Expose-Headers'] = 'fileid'
return response
@app.route('/pdftex/<int:fileformat>/<filename>')
@cross_origin()
def pdftex_fetch_file(fileformat, filename):
filename = san(filename)
url = None
if filename == "swiftlatexpdftex.fmt":
url = filename
else:
url = pykpathsea_pdftex.find_file(filename, fileformat)
if url is None or not os.path.isfile(url):
return "File not found", 301
else:
response = make_response(send_file(url, mimetype='application/octet-stream'))
response.headers['fileid'] = os.path.basename(url)
response.headers['Access-Control-Expose-Headers'] = 'fileid'
return response
@app.route('/pdftex/pk/<int:dpi>/<filename>')
@cross_origin()
def pdftex_fetch_pk(dpi, filename):
filename = san(filename)
url = pykpathsea_pdftex.find_pk(filename, dpi)
if url is None or not os.path.isfile(url):
return "File not found", 301
else:
response = make_response(send_file(url, mimetype='application/octet-stream'))
response.headers['pkid'] = os.path.basename(url)
response.headers['Access-Control-Expose-Headers'] = 'pkid'
return response