|
@@ -1,12 +1,15 @@
|
1
|
1
|
import os
|
2
|
|
-from flask import send_from_directory, jsonify, Blueprint, current_app
|
3
|
|
-from flask_cors import CORS, cross_origin
|
4
|
2
|
|
|
3
|
+from flask import (Blueprint, current_app, flash, jsonify, redirect, request,
|
|
4
|
+ send_from_directory, url_for)
|
|
5
|
+from flask_cors import CORS, cross_origin
|
|
6
|
+from werkzeug.utils import secure_filename
|
5
|
7
|
|
6
|
8
|
articles_api_bp = Blueprint(
|
7
|
9
|
"articles", __name__, template_folder="templates")
|
8
|
10
|
|
9
|
|
-cors = CORS(articles_api_bp, resources={r"/articles": {"origins": "http://localhost:5500"}})
|
|
11
|
+cors = CORS(articles_api_bp, resources={
|
|
12
|
+ r"/articles": {"origins": "http://localhost:5500"}})
|
10
|
13
|
|
11
|
14
|
|
12
|
15
|
@articles_api_bp.route("/<path:article>")
|
|
@@ -27,3 +30,28 @@ def index():
|
27
|
30
|
if os.path.isfile(path):
|
28
|
31
|
files.append(filename)
|
29
|
32
|
return jsonify(files)
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+@articles_api_bp.route("/upload", methods=("POST",))
|
|
36
|
+@cross_origin(origin="localhost", headers=["Content-Type", "Authorization"])
|
|
37
|
+def upload_file():
|
|
38
|
+ if request.method == "POST":
|
|
39
|
+ if "file" not in request.files:
|
|
40
|
+ flash("No file part")
|
|
41
|
+ return redirect(request.url)
|
|
42
|
+ file = request.files["file"]
|
|
43
|
+ if file.filename == '':
|
|
44
|
+ flash("No selected file")
|
|
45
|
+ return redirect(request.url)
|
|
46
|
+ if file and allowed_file(file.filename):
|
|
47
|
+ filename = secure_filename(file.filename)
|
|
48
|
+ file.save(os.path.join(
|
|
49
|
+ current_app.config["UPLOAD_FOLDER"], filename))
|
|
50
|
+ return redirect(url_for("articles.send_text_file",
|
|
51
|
+ article=filename))
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+def allowed_file(filename):
|
|
55
|
+ return '.' in filename and \
|
|
56
|
+ filename.rsplit('.', 1)[1].lower(
|
|
57
|
+ ) in current_app.config["ALLOWED_EXTENSIONS"]
|