-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
57 lines (52 loc) · 1.99 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
import os
from os import getenv
from flask import Flask, after_this_request, send_file, request, redirect, flash, url_for
from werkzeug.utils import secure_filename
from gt_converter import create_list, draw_dxf
app = Flask(__name__)
ALLOWED_EXTENSIONS = {"gt"}
app.config['UPLOAD_FOLDER'] = 'uploads/'
app.config['MAX_CONTENT_PATH'] = 16 * 1024 * 1024
app.config['ALLOWED_EXTENSIONS'] = ALLOWED_EXTENSIONS
app.secret_key = getenv("SECRET_KEY")
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
@app.route('/', methods=['GET', 'POST'])
def upload_file():
if request.method == 'POST':
if 'file' not in request.files:
flash('No file part')
return redirect(request.url)
file = request.files['file']
if file.filename == '':
flash('No selected file')
return redirect(request.url)
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
to_convert = "uploads/" + filename
data_list = create_list(to_convert)
draw_dxf(to_convert, data_list)
to_download = to_convert + "_export.dxf"
print(to_download)
@after_this_request
def remove_file(response):
try:
os.remove(to_download)
os.remove(to_convert)
except Exception as error:
app.logger.error("Error removing or closing downloaded file handle", error)
return response
return return_file(to_download)
return '''
<!doctype html>
<title>Upload new File</title>
<h1>Upload new File</h1>
<form method=post enctype=multipart/form-data>
<input type=file name=file>
<input type=submit value=Upload>
</form>
'''
def return_file(to_download):
return send_file(to_download)