-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
70 lines (55 loc) · 2.3 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
from flask import Flask, render_template, request, redirect, url_for, flash
from werkzeug.utils import secure_filename
import os
from script import sourceComments
from flask import send_from_directory
FILE_DIR = os.path.dirname(os.path.abspath(__file__))
uploadFolder = os.path.join(FILE_DIR,'uploadedFiles')
allowedExtensions = {'cs'}
maxFileSize = '10,400,000' # 10 mb in bytes
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = uploadFolder
app.config['MAX_CONTENT_PATH'] = maxFileSize
def allowed_file(fname):
return '.' in fname and \
fname.rsplit('.', 1)[1].lower() in allowedExtensions
filename = ''
@app.route('/')
def landing_page():
return render_template('index.html',filenamee = filename)
@app.route('/uploader', methods=['GET', 'POST'])
def upload_file():
# function to let user submit the .cs file
if request.method == 'POST':
# check if the post request has the file part
if 'file' not in request.files:
flash('No file part')
return redirect(request.url)
file = request.files['file']
# if user does not select file, browser also submit an empty part without filename
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))
file_relativepath = os.path.join(
app.config['UPLOAD_FOLDER'], filename)
sourceComments(file_relativepath, '//') # return true
#return redirect(url_for('return_file', filename=filename)) #downloads the file
else:
return "invalid file type"
return render_template('index.html',filenamee = filename)
@app.route('/uploader/success/<path:filename>')
def return_file(filename):
# return "success fully updated"
try:
return send_from_directory(app.config['UPLOAD_FOLDER'], filename=filename, as_attachment=True)
return redirect(url_for(landing_page))
except FileNotFoundError:
abort(404)
else:
os.remove(filename)
if __name__ == '__main__':
app.run(debug = True,host=os.getenv('IP', '0.0.0.0'),
port=int(os.getenv('PORT', 4444)))