From 701cfa7d619bf9bfeba0228d3bc1c497778bd4f9 Mon Sep 17 00:00:00 2001 From: raizenxd Date: Sat, 29 Mar 2025 12:09:11 +0100 Subject: [PATCH] =?UTF-8?q?so=20ich=20denke=20das=20sollte=20f=C3=BCr=20ne?= =?UTF-8?q?=203=20reichen=20lol?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 4 +- README.md | 33 +++++++++-- _map.db | Bin 12288 -> 12288 bytes main.py | 103 ++++++++++++++++++++++++--------- pages/download.html | 15 +++++ pages/error.html | 12 ++++ index.html => pages/index.html | 5 -- pages/uploaded.html | 15 +++++ 8 files changed, 147 insertions(+), 40 deletions(-) create mode 100644 pages/download.html create mode 100644 pages/error.html rename index.html => pages/index.html (66%) create mode 100644 pages/uploaded.html diff --git a/.gitignore b/.gitignore index f5905ad..9d61207 100644 --- a/.gitignore +++ b/.gitignore @@ -168,5 +168,5 @@ cython_debug/ # option (not recommended) you can uncomment the following to ignore the entire idea folder. #.idea/ -_identifierAllocation.db -data/ \ No newline at end of file +data/ +_map.db \ No newline at end of file diff --git a/README.md b/README.md index 9ff4b1b..36f8173 100644 --- a/README.md +++ b/README.md @@ -1,12 +1,35 @@ -Dieses Projekt demonstriert die Umsetzung eines einfaches Dienstes mit Python 3, Flask, sqlite3 und Caddy welches ermöglicht das Hochladen/Herunterladen von Dateien über Weboberfläche oder API. +Dieses Projekt demonstriert die Umsetzung eines einfaches Dienstes mit flask (Routing), jinja2 (Templates) und sqlite3 in Python welches ermöglicht das Hochladen/Herunterladen von Dateien über Weboberfläche oder URL abruf. ## Pre-Installation 1. Installiere Abhängigkeiten. - Debian-basierte Distributionen: sudo apt install caddy python + pip install flask jinja2 sqlite3 - pip install flask sqlite3 +2. Klone die Repository + git clone https://git.raizen.me/raizen/E3FI2_Kamil_Filehosting.git -2. Editiere die Konfigurationsdatei von Caddy - nano /etc/caddy/Caddyfile +3. Führe das Skript aus + flask --app main.py run + oder + python -m flask --app main.py run +4. In Webbrowser http://127.0.0.1:5000 öffen + + +## Bedienung +Aus Perspektive der Nutzer: + 1. Rufe index auf + 2. Häge die Datei an das Formular, drücke auf Upload + 3. Eine Übersicht mit Downloadlink wird angezeigt + +Aus Perspektive der API: + 1. Übergebe die Datei an das /upload Endpoint (format: file -> binary data, filename -> name der datei) + 2. Aufruf von Downloads genau so wie Nutzer + + +## Struktur + +(host)/ -> Formular bei dem die Datei angehängt wird (nicht für API gedacht) +(host)/upload -> Formular oder API sendet die Datei an dieses Endpoint, als Rückgabe wird eine übersicht mit dem Link ausgegeben. +(host)/file/(id) -> Dateiübersicht die Name, Größe, Uploaddatum sowie den Downloadlink enthält (könnte für API nützlich sein) +(host)/file/(id)/download -> Startet den download von der Datei diff --git a/_map.db b/_map.db index 744bf75cebbc7c79e646a13fcf0150e7ae8bdf2d..71ca0faa622b58ece4180854d9529390ae308737 100644 GIT binary patch delta 443 zcmY+AJ!@1!6hPnGg~SB1-0`5G4Gvzd-uI})ZXhXH|;>Tw+|m`>&9gtp6W5b z9ByZt(YA>IlGv&2Oh(EWv~-{_NpDioUbDA8DrL!X2}uEAmMIkqE@+itIGxTuC{T6_ z^p*;nQjP#e(&dozLK%*vHaY_^Lc)wH8f7C}XKnPfkfAh5dxXr2y>{e?h)cBcu4yja zpX#Niy`u+DaPrt0^3&aqevkguhfs=-Y}i^ym;j@9(X$H_$R%SBRdiM7JdKhUOsqkih=hdXVc>Wi!R@Gwpc_l8y`AL8Crx@JmHucq+oqo4j Hxmx%KAK!Ph delta 271 zcmY+;PfG$p7=ZDa4U!h@WxEI!xDXwLTzB4?bsTRJ1koQ5I|ezsGZR8vmKxP1_uM7% zlSCguy7XZ@cg&96pC3Ha{M^flGmK@M zCv8+m9poasu!H>@7WVG)+&-)-e$|<{Yn|HLKj!;{5DH5I%hl>P%% 1): # Dateinamen die ein Suffix besitzen - path = f"./data/{hashlib.sha256((name[0] + idValue).encode()).hexdigest()}.{name[1]}" + path = f"./data/{hashlib.sha256((name[0] + idValue).encode()).hexdigest()}.{name[1]}" # Wirkt wie schwarze Magie ist aber sehr simpel else: # Dateinamen die kein Suffix besitzen path = f"./data/{hashlib.sha256((name[0] + idValue).encode()).hexdigest()}" file.save(path) - dbCursor.execute("INSERT INTO _idToFile VALUES (?, ?, datetime('now'), ?, ?);", (idValue, path, file.filename, 10)) - db.commit() + dbCursor.execute("INSERT INTO _idToFile VALUES (?, ?, datetime('now'), ?);", (idValue, path, file.filename,)) # Füge neuen Eintrag in die Datenbank + db.commit() # Bestätige die Transaktion damit der Eintrag tatsächlich gespeichert wird. Das beste ist ja, dass ich es nur bei INSERT tätigen muss - return "sank you" + if request.user_agent.string != "API": + return uploadedTemplate.render(link=("/file/" + idValue)) + else: + return jsonify(status=100,id=idValue) -@app.route("/download/") -def downloader(identifier): - dbCursor.execute("SELECT destination FROM _idToFile WHERE id=?;", (identifier,)) +# Nicht die eleganteste Lösung fürs Download aber ich hab kein Bock es anders zu lösen + +# Das hier ist quasi das "Entry Point", es wird eineeine Seite zurück gegeben mit Infos über die Datei sowie einen Download Link +@app.route("/file/") +def fileInfo(identifier): + dbCursor.execute("SELECT destination, uploadTime, filename FROM _idToFile WHERE id=?;", (identifier,)) # Hole die Infos für die Template + data = dbCursor.fetchone() + if data != None: + sizeBytes = os.path.getsize(data[0]) + factor = 0 # 0 -> 1 = Kilo -> 2 = Mega -> 3 = Giga, usw.. + + while ((sizeBytes / 1024) > 1): + sizeBytes = sizeBytes / 1024 + factor += 1 + + if request.user_agent.string != "API": + return downloadTemplate.render(filename=data[2], uploadDate=data[1], size=("%.2f" % sizeBytes + " " + prefixes[factor]), downloadPath=("/file/" + identifier + "/download")) + else: + return jsonify(status=100, name=data[2], size=("%.2f" % sizeBytes + " " + prefixes[factor]), uploadDate=data[1]) + else: + if request.user_agent.string != "API": + return errorTemplate.render(error="keine Datei gefunden") + else: + return jsonify(status=301) + +# Das Download Link +@app.route("/file//download") +def fileDownload(identifier): + dbCursor.execute("SELECT destination FROM _idToFile WHERE id=?;", (identifier,)) # Das Komma muss so bleiben sonst funktioniert """"prepared statement"""" nicht path = dbCursor.fetchone() if path != None: return send_file(path[0]) else: - return "no file, now fuck off" \ No newline at end of file + if request.user_agent.string != "API": + return errorTemplate.render(error="keine Datei gefunden") + else: + return jsonify(status=301) \ No newline at end of file diff --git a/pages/download.html b/pages/download.html new file mode 100644 index 0000000..169d12c --- /dev/null +++ b/pages/download.html @@ -0,0 +1,15 @@ + + + + + + Download Info + + +

{{ filename }}

+

Größe: {{ size }}

+

Uploaddatum: {{ uploadDate }}

+
+ Download + + \ No newline at end of file diff --git a/pages/error.html b/pages/error.html new file mode 100644 index 0000000..9ff7074 --- /dev/null +++ b/pages/error.html @@ -0,0 +1,12 @@ + + + + + + CHIEF! Somethings wrong! + + +

WEE WOO WEE WOO! WE FOUND AN ERROR!

+

{{ error }}

+ + \ No newline at end of file diff --git a/index.html b/pages/index.html similarity index 66% rename from index.html rename to pages/index.html index ced6a0a..3139083 100644 --- a/index.html +++ b/pages/index.html @@ -9,11 +9,6 @@

- - -
- -
diff --git a/pages/uploaded.html b/pages/uploaded.html new file mode 100644 index 0000000..1fbd6bf --- /dev/null +++ b/pages/uploaded.html @@ -0,0 +1,15 @@ + + + + + + Document + + +

File has been uploaded

+

+

Link: {{ link }}

+
+ Return + + \ No newline at end of file