diff --git a/.gitignore b/.gitignore index 42ddb4a..9d08b52 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ app/venv/ +app/files/ diff --git a/VO_Mathematik_3.pdf b/VO_Mathematik_3.pdf deleted file mode 100644 index a9e952f..0000000 Binary files a/VO_Mathematik_3.pdf and /dev/null differ diff --git a/app/__pycache__/main.cpython-313.pyc b/app/__pycache__/main.cpython-313.pyc index 721923a..3070ab3 100644 Binary files a/app/__pycache__/main.cpython-313.pyc and b/app/__pycache__/main.cpython-313.pyc differ diff --git a/app/main.py b/app/main.py index 365d55e..7f8ba0e 100644 --- a/app/main.py +++ b/app/main.py @@ -1,21 +1,52 @@ from typing import Annotated from typing import List, Dict, Tuple -from fastapi import FastAPI, File, UploadFile, Request, Form +from fastapi import FastAPI, File, HTTPException, UploadFile, Request, Form +from fastapi.responses import FileResponse + +# import fastapi from fastapi.staticfiles import StaticFiles import pymupdf import json -import os + +# import os +import mariadb app = FastAPI() -app.mount("/static", StaticFiles(directory="./"), name="static") - -locpaths = ["./VO_Mathematik_3.pdf"] # replace this with a database +app.mount("/favicon", StaticFiles(directory="./favicon"), name="favicon") +app.mount("/static", StaticFiles(directory="./static"), name="static") +db = mariadb.connect( + host="localhost", user="wildserver", password="DBPassword", database="Unizeug" +) -@app.post("/files/") -async def create_file(file: Annotated[bytes, File()]): - return {"filesize": len(file)} +# cur = db.cursor() +# cur.execute("select * from FIP;") +# for l in cur: +# print(l) +# locpaths = ["./VO_Mathematik_3.pdf"] # replace this with a database +@app.get("/") +async def get_index(): + return FileResponse("./index.html") + + +@app.get("/files/{file_id}") +async def get_file(file_id: str): + cur = db.cursor() + try: + cur.execute("Select filename from FIP where id=?", (file_id,)) + except mariadb.Error as e: + print(f"Mariadb Error: {e}") + raise HTTPException( + status_code=500, detail="Somethings wrong with the database" + ) + filename = cur.fetchone()[0] + return FileResponse(f"./app/files/{filename}") + + +# @app.post("/files/") +# async def create_file(file: Annotated[bytes, File()]): +# return {"filesize": len(file)} @app.post("/uploadfile/") @@ -23,14 +54,31 @@ async def create_upload_file(file: UploadFile): content = await file.read() filename = file.filename if file.filename is not None else "None" locpath = "./app/files/" + filename - locpaths.append(locpath) + # locpaths.append(locpath) + cur = db.cursor() + try: + cur.execute("Insert Into FIP (filename) Values(?)", (filename,)) + except mariadb.Error as e: + print(f"Error: {e}") + raise HTTPException( + status_code=500, detail="Somethings wrong with the database" + ) + try: + cur.execute("Select id From FIP where filename=?", (filename,)) + except mariadb.Error as e: + print(f"Error: {e}") + raise HTTPException( + status_code=500, detail="Somethings wrong with the database" + ) + id = cur.fetchone()[0] with open(locpath, "wb") as f: f.write(content) - app.mount("/files", StaticFiles(directory="./app/files/"), name="files") + # app.mount("/files", StaticFiles(directory="./app/files/"), name="files") + db.commit() return { "filename": filename, - "path": "/files/" + filename, - "fid": len(locpaths) - 1, + "path": "/files/" + id, + "fid": id, } @@ -39,7 +87,7 @@ async def get_submittion( lva: Annotated[str, Form()], # LVA Name and Number prof: Annotated[str, Form()], # Vortragender fname: Annotated[str, Form()], # Path to pdf File - fileId: Annotated[int, Form()], + fileId: Annotated[str, Form()], sem: Annotated[str, Form()], # Semester eg. 2024W stype: Annotated[str, Form()], # Type of File eg. Prüfung ex_date: Annotated[str, Form()], # Date of Exam only when type is exam @@ -53,7 +101,16 @@ async def get_submittion( print(lva, prof, fname, stype, sem, ex_date, rects, pagescales) rects_p = json.loads(rects) scales_p = json.loads(pagescales) - censor_pdf(locpaths[fileId], "./app/files/censored.pdf", rects_p, scales_p) + cur = db.cursor() + try: + cur.execute("Select filename from FIP where id=?", (fileId,)) + except mariadb.Error as e: + print(f"Mariadb Error: {e}") + raise HTTPException( + status_code=500, detail="Somethings wrong with the database" + ) + filepath = "./app/files/" + cur.fetchone()[0] + censor_pdf(filepath, "./app/files/censored.pdf", rects_p, scales_p) return {"done": "ok"} diff --git a/database_init.sql b/database_init.sql new file mode 100644 index 0000000..97e45de --- /dev/null +++ b/database_init.sql @@ -0,0 +1,6 @@ +CREATE DATABASE Unizeug; +USE Unizeug; +CREATE TABLE LVAs(id BIGINT(20) unsigned NOT NULL AUTO_INCREMENT,number MEDIUMINT unsigned, name VARCHAR(256),PRIMARY KEY(id)); +CREATE TABLE Porfs(id BIGINT(20) unsigned NOT NULL AUTO_INCREMENT,name VARCHAR(256),PRIMARY KEY(id)); +CREATE TABLE LPLink(id BIGINT(20) unsigned NOT NULL AUTO_INCREMENT,LVAID bigint(20),ProfID bigint(20),PRIMARY KEY(id)); +CREATE TABLE FIP(id UUID DEFAULT(UUID()), filename VARCHAR(256), PRIMARY KEY(id)); diff --git a/index.html b/index.html index ced4506..dec59cc 100644 --- a/index.html +++ b/index.html @@ -3,9 +3,9 @@