A bunch of random crap working towards the first (simplest) upload mechanism

This commit is contained in:
Bradley Bickford 2025-11-01 21:03:51 -04:00
parent d59e23fc4c
commit d406574707
4 changed files with 82 additions and 15 deletions

View File

@ -6,10 +6,26 @@
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet"> <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
<link rel="stylesheet" type="text/css" href="../static/style.css"> <link rel="stylesheet" type="text/css" href="../static/style.css">
<script src="../static/script.js"></script>
</head> </head>
<body> <body>
<div class="container"> <div class="container">
<h1 class="text-center">Upload a Static Photo</h1> <h1 class="text-center">Upload a Static Photo</h1>
<hr>
<div class="alert alert-info text-center" role="alert">
Upload a single static photo, single photos are good if you want something that doesn't distract during services.
</div>
<form method="POST" enctype="multipart/form-data" action="/upload/photo">
<div class="form-group">
<label for="fileToUpload">File to Upload</label>
<input type="file" class="form-control" name="file" id="fileToUpload" />
</div>
<div class="form-group">
<label for="buttonName">Button Name</label>
<input type="text" class="form-control" name="buttonName" id="buttonName" />
</div>
<button type="submit" class="btn btn-primary form-control">Submit</button>
</form>
</div> </div>
</body> </body>
</html> </html>

45
static/script.js Normal file
View File

@ -0,0 +1,45 @@
function uploadFiles(submitButtonID, fileInputID, progressBarID, targetEndpoint, otherData) {
const submitButton = document.getElementById(submitButtonID)
submitButton.disabled = true
const files = document.getElementById(fileInputID).files
if (!files[0]) {
alert('Please select at least one file')
submitButton.disabled = false
return
}
const formData = new FormData()
for(var i = 0; i < files.length; i++) {
formData.append("files[]", files[i])
}
for(const key in otherData) {
formData.append(key, otherData[key])
}
var xhr = new XMLHttpRequest()
xhr.open("POST", targetEndpoint)
xhr.onload = function() {
if (xhr.status === 200) {
window.location.href = "/"
} else {
alert("Something went wrong during the upload, please try again later")
submitButton.disabled = false
}
}
xhr.upload.onprogress = function(event) {
if(event.lengthComputable) {
const percentComplete = (event.loaded / event.total) * 100
document.getElementById(progressBarID).value = percentComplete
}
}
xhr.onerror = function() {
alert("An error occurred, please try again later")
submitButton.disabled = false
}
xhr.send(formData)
}

View File

@ -1,15 +0,0 @@
from flask import g
from sqlite import SQLite
import json
def get_db():
db = getattr(g, "_database", None)
if db is None:
with open('../../config.json') as config_file:
config_json = json.load(config_file.read())
if str(config_json["database"]["type"]).casefold() == "SQLite".casefold():
db = g._database = SQLite(config_json["database"]["name"])
return db

21
utilities/tools.py Normal file
View File

@ -0,0 +1,21 @@
from flask import g
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"])
return db