-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
69 lines (52 loc) · 2.05 KB
/
main.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
from flask import Flask, request, send_file
from documentCreation import convert_to
import os
import tempfile
import traceback
app = Flask(__name__)
@app.route("/convert_doc_to_pdf", methods = ['GET', 'POST'])
def convert_doc_to_pdf():
print("Converting Document To PDF")
if request.method == 'POST':
#Create a temporary file for the document
file_name = next(tempfile._get_candidate_names())
file_path = os.path.abspath(os.path.join(tempfile.gettempdir(),file_name))
print("Made File Path: ",file_path)
try:
if 'file' not in request.files:
print("File Not in File Request")
return "No File Uploaded Recognized: " + request
else:
print("Saving File From Request: ", file_path)
f= request.files['file']
f.save(file_path)
print("Validating the File has been saved: ", os.path.exists(file_path))
try:
#file_path = "BADPDF.pdf"
file_path = convert_to(file_path)
print("Converted File Name ",file_path)
except:
file_path = "BADPDF.pdf"
traceback.print_exc()
print("Excepted File Converion Failed")
print("Returning the following file, ", file_path)
response = send_file(
file_path,
mimetype='application/pdf',
as_attachment=True,
download_name='file.pdf'
)
return response
except:
print("Failed to Save File")
return "File Upload Failed"
else:
return "This function expects a POST of a file"
# return response
# else:
# return "Not a post request"
@app.route("/")
def running():
return "DOCX - PDF Conversion Service Running"
if __name__ == "__main__":
app.run(debug=True, host="0.0.0.0", port=int(os.environ.get("PORT", 8080)))