-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
57 lines (49 loc) · 2.04 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
from flask import Flask,send_from_directory, render_template, request,abort, flash, url_for, redirect
import werkzeug
from werkzeug.utils import secure_filename
from werkzeug.urls import urlencode
from werkzeug.exceptions import BadRequest
import os
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = './uploads'
app.config['ALLOWED_EXTENSIONS'] = {'txt', 'pdf', 'png', 'jpg', 'jpeg'}
host= input('enter host with port: ex:127.0.0.1:5000 ') #host
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1].lower() in app.config['ALLOWED_EXTENSIONS']
@app.route("/")
def hello_world():
return render_template('home.html')
@app.route('/upload', methods=['GET', 'POST'])
def upload_file():
if request.method == 'POST':
if 'file' not in request.files:
flash('No file part', 'error')
return redirect(request.url)
file = request.files['file']
if file.filename == '':
flash('No selected file', 'error')
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))
return render_template('home.html',host=host,path=url_for('uploaded_file', filename=filename))
else:
flash('Invalid file type', 'error')
return redirect(request.url)
else:
return render_template('home.html')
@app.route('/uploads/<filename>')
def uploaded_file(filename):
if not os.path.exists(os.path.join(app.config['UPLOAD_FOLDER'], filename)):
abort(404)
if not os.path.isfile(os.path.join(app.config['UPLOAD_FOLDER'], filename)):
abort(403)
if not allowed_file(filename):
abort(403)
return send_from_directory(app.config['UPLOAD_FOLDER'], filename)
if __name__ == "__main__":
app.secret_key = 'super secret key'
app.config['SESSION_TYPE'] = 'filesystem'
app.debug = True
app.run()