added database for Files in Progress

This commit is contained in:
Marcel Gansfusz
2025-02-02 00:00:32 +01:00
parent e9bbba26ac
commit 4c14969d40
10 changed files with 119 additions and 17 deletions

1
.gitignore vendored
View File

@@ -1 +1,2 @@
app/venv/ app/venv/
app/files/

Binary file not shown.

Binary file not shown.

View File

@@ -1,21 +1,52 @@
from typing import Annotated from typing import Annotated
from typing import List, Dict, Tuple 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 from fastapi.staticfiles import StaticFiles
import pymupdf import pymupdf
import json import json
import os
# import os
import mariadb
app = FastAPI() app = FastAPI()
app.mount("/static", StaticFiles(directory="./"), name="static") app.mount("/favicon", StaticFiles(directory="./favicon"), name="favicon")
app.mount("/static", StaticFiles(directory="./static"), name="static")
locpaths = ["./VO_Mathematik_3.pdf"] # replace this with a database db = mariadb.connect(
host="localhost", user="wildserver", password="DBPassword", database="Unizeug"
)
@app.post("/files/") # cur = db.cursor()
async def create_file(file: Annotated[bytes, File()]): # cur.execute("select * from FIP;")
return {"filesize": len(file)} # 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/") @app.post("/uploadfile/")
@@ -23,14 +54,31 @@ async def create_upload_file(file: UploadFile):
content = await file.read() content = await file.read()
filename = file.filename if file.filename is not None else "None" filename = file.filename if file.filename is not None else "None"
locpath = "./app/files/" + filename 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: with open(locpath, "wb") as f:
f.write(content) f.write(content)
app.mount("/files", StaticFiles(directory="./app/files/"), name="files") # app.mount("/files", StaticFiles(directory="./app/files/"), name="files")
db.commit()
return { return {
"filename": filename, "filename": filename,
"path": "/files/" + filename, "path": "/files/" + id,
"fid": len(locpaths) - 1, "fid": id,
} }
@@ -39,7 +87,7 @@ async def get_submittion(
lva: Annotated[str, Form()], # LVA Name and Number lva: Annotated[str, Form()], # LVA Name and Number
prof: Annotated[str, Form()], # Vortragender prof: Annotated[str, Form()], # Vortragender
fname: Annotated[str, Form()], # Path to pdf File fname: Annotated[str, Form()], # Path to pdf File
fileId: Annotated[int, Form()], fileId: Annotated[str, Form()],
sem: Annotated[str, Form()], # Semester eg. 2024W sem: Annotated[str, Form()], # Semester eg. 2024W
stype: Annotated[str, Form()], # Type of File eg. Prüfung stype: Annotated[str, Form()], # Type of File eg. Prüfung
ex_date: Annotated[str, Form()], # Date of Exam only when type is exam 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) print(lva, prof, fname, stype, sem, ex_date, rects, pagescales)
rects_p = json.loads(rects) rects_p = json.loads(rects)
scales_p = json.loads(pagescales) 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"} return {"done": "ok"}

6
database_init.sql Normal file
View File

@@ -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));

View File

@@ -3,9 +3,9 @@
<head> <head>
<title>Unizeug uploader</title> <title>Unizeug uploader</title>
<link rel="stylesheet" href="style.css" /> <link rel="stylesheet" href="static/style.css" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/pdf.js/2.4.456/pdf.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/pdf.js/2.4.456/pdf.min.js"></script>
<script src="app.js" defer></script> <script src="static/app.js" defer></script>
<link rel="icon" type="image/png" href="/favicon/favicon-96x96.png" sizes="96x96" /> <link rel="icon" type="image/png" href="/favicon/favicon-96x96.png" sizes="96x96" />
<link rel="icon" type="image/svg+xml" href="/favicon/favicon.svg" /> <link rel="icon" type="image/svg+xml" href="/favicon/favicon.svg" />
<link rel="shortcut icon" href="/favicon/favicon.ico" /> <link rel="shortcut icon" href="/favicon/favicon.ico" />

View File

@@ -1 +1,3 @@
sudo systemctl start mariadb.service
python -m uvicorn app.main:app --reload python -m uvicorn app.main:app --reload
Requirements: For pymupdf tesseract needs to be installed for the language deu un the system.

View File

@@ -340,7 +340,10 @@ function initListeners() {
}); });
} }
const startPdf = () => { const startPdf = () => {
doc = new PDFDocument("./VO_Mathematik_3.pdf", 0); doc = new PDFDocument(
"./files/b78c869f-e0bb-11ef-9b58-84144d05d665",
"b78c869f-e0bb-11ef-9b58-84144d05d665",
);
//pdf = new PDFView("./VO_Mathematik_3.pdf"); //pdf = new PDFView("./VO_Mathematik_3.pdf");
initDraw(); initDraw();
initUpload(); initUpload();

33
test_session.py Normal file
View File

@@ -0,0 +1,33 @@
# coding: utf-8
import mariadb
db=mariadb.connect(host="localhost",user="wildserver",password:"DBPassword",database="Unizeug")
db=mariadb.connect(host="localhost",user="wildserver",password="DBPassword",database="Unizeug")
cur=db.cursor()
cur.execute("Select id form FIP where filename='VO_Mathematik_3.pdf';")
cur.execute("Select id form FIP where filename=?;",("VO_Mathematik_3.pdf"))
cur.execute("Select id form FIP where filename=?;",("VO_Mathematik_3.pdf",))
cur.execute("Select id form FIP Where filename=?;",("VO_Mathematik_3.pdf",))
cur.execute("Select id form FIP Where filename=?;",("VO_Mathematik_3.pdf"))
cur.execute("Select id form FIP Where filename=?;",("VO_Mathematik_3.pdf",))
cur.execute("Select id from FIP Where filename=?;",("VO_Mathematik_3.pdf",))
cur.fetchone()
cur.fetchone()
cur.execute("show tables;")
cur.fetchone()
cur.fetchone()
cur.fetchone()#
cur.fetchone()#
cur.fetchone()#
cur.fetchone()#
cur.fetchone()#
cur.fetchone()#
cur.fetchone()#
cur.fetchone()#
cur.execute("insert into FIP filename Values(?);",("test",))
cur.execute("insert into FIP (filename) Values(?);",("test",))
cur.fetchone()
cur.execute("Select * from FIP Where filename=?;",("VO_Mathematik_3.pdf",))
cur.fetchone()
cur.execute("Select * from FIP Where id=?;",("b78c869f-e0bb-11ef-9b58-84144d05d665",))
cur.fetchone()
get_ipython().run_line_magic('save', '')