diff --git a/blueprints/upload_photo/upload_photo.html b/blueprints/upload_photo/upload_photo.html index 890bc5c..38f7778 100644 --- a/blueprints/upload_photo/upload_photo.html +++ b/blueprints/upload_photo/upload_photo.html @@ -7,6 +7,13 @@ +
@@ -15,7 +22,11 @@ -
+ +
+
0%
+
@@ -24,7 +35,7 @@
- +
diff --git a/blueprints/upload_photo/upload_photo.py b/blueprints/upload_photo/upload_photo.py index 0445998..20b6557 100644 --- a/blueprints/upload_photo/upload_photo.py +++ b/blueprints/upload_photo/upload_photo.py @@ -1,11 +1,40 @@ -from flask import Blueprint, request, render_template +from flask import Blueprint, request, render_template, jsonify, current_app +from werkzeug.utils import secure_filename +import os upload_photo_bp = Blueprint('upload_photo', __name__, url_prefix='/upload', template_folder='.') -@upload_photo_bp.route('/photo') +@upload_photo_bp.route('/photo', methods=['GET', 'POST']) def get_page(): if request.method == "GET": return render_get(), 200 + elif request.method == "POST": + return upload() def render_get(): - return render_template('upload_photo.html') \ No newline at end of file + return render_template('upload_photo.html') + +def upload(): + if not request.files['files[]']: + return jsonify({ + 'status': 'FAILED', + 'error': '/upload/photo requires 1 photo' + }), 400 + + # TODO Button name data exists + # TODO Button name already exists validation + # TODO File input validation + print(request.files['files[]']) + + file = request.files['files[]'] + + if file: + folder_path = os.path.join(current_app.config["config"]["upload_location"], request.form["name"]) + if not os.path.exists(folder_path): + os.makedirs(folder_path) + + filename = secure_filename(file.filename) + file.save(os.path.join(folder_path, filename)) + return jsonify({ + 'status': 'SUCCESS' + }), 200 \ No newline at end of file diff --git a/config.json b/config.json index 72d53c8..5e02f7c 100644 --- a/config.json +++ b/config.json @@ -2,5 +2,6 @@ "database": { "type": "SQLite", "name": "scdb.db" - } + }, + "upload_location": "uploads" } \ No newline at end of file diff --git a/sc_webserver.py b/sc_webserver.py index 3ae5deb..9c23fee 100644 --- a/sc_webserver.py +++ b/sc_webserver.py @@ -1,8 +1,17 @@ from flask import Flask from blueprints.home.home import home_bp from blueprints.upload_photo.upload_photo import upload_photo_bp +import os +import json + +with open("config.json", 'r') as config_file: + config_json = json.loads(config_file.read()) + +if not os.path.exists(config_json["upload_location"]): + os.makedirs(config_json["upload_location"]) app = Flask(__name__) +app.config["config"] = config_json app.register_blueprint(home_bp) app.register_blueprint(upload_photo_bp) diff --git a/static/script.js b/static/script.js index 06a7d5e..6c8a55b 100644 --- a/static/script.js +++ b/static/script.js @@ -1,4 +1,16 @@ -function uploadFiles(submitButtonID, fileInputID, progressBarID, targetEndpoint, otherData) { +function updatePercentage(input_percent, progressBarContainerID, progressBarID) { + const clampedPercentage = Math.min(100, Math.max(0, input_percent)) + + const progressBar = document.getElementById(progressBarID) + const progressContainer = document.getElementById(progressBarContainerID) + + progressBar.style.width = `${clampedPercentage}%` + progressContainer.setAttribute('aria-valuenow', clampedPercentage) + + progressBar.textContent = `${clampedPercentage}%` +} + +function uploadFiles(submitButtonID, fileInputID, progressBarContainerID, progressBarID, targetEndpoint, otherData) { const submitButton = document.getElementById(submitButtonID) submitButton.disabled = true @@ -33,7 +45,8 @@ function uploadFiles(submitButtonID, fileInputID, progressBarID, targetEndpoint, xhr.upload.onprogress = function(event) { if(event.lengthComputable) { const percentComplete = (event.loaded / event.total) * 100 - document.getElementById(progressBarID).value = percentComplete + + updatePercentage(percentComplete, progressBarContainerID, progressBarID) } } xhr.onerror = function() { diff --git a/utilities/__init__.py b/utilities/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/utilities/tools.py b/utilities/tools.py index d33675f..357ad1b 100644 --- a/utilities/tools.py +++ b/utilities/tools.py @@ -1,21 +1,12 @@ -from flask import g -from database.sqlite import SQLite +from flask import g, current_app +from .database.sqlite import SQLite import json -def get_config(): - config = getattr(g, "_config", None) - - if config is None: - with open("../config.json") as config_file: - config = g._config = json.loads(config_file.read()) - - return config - def get_db(): db = getattr(g, "_database", None) if db is None: - if str(get_config()["database"]["type"]).casefold() == "SQLite".casefold(): - db = g._database = SQLite(get_config()["database"]["name"]) + if str(current_app.config["config"]["database"]["type"]).casefold() == "SQLite".casefold(): + db = g._database = SQLite(current_app.config["config"]["database"]["name"]) return db \ No newline at end of file