file_index_db/file_index_api/app.py
2025-08-29 18:51:39 +02:00

42 lines
913 B
Python

import os
from dataclasses import dataclass
from litestar import Litestar, get, post
import sqlite3
sql = None
if not os.path.isfile('./file_index.db'):
with open('create_table.sql') as fh:
sql = ' '.join(fh.readlines())
db = sqlite3.connect('./file_index.db')
if sql is not None:
db.executescript(sql)
@dataclass
class PostRequest:
hostname: str
source: str
uuid: str
file_path: str
shasum: str
@get("/")
async def hello_world() -> str:
return "Hello, world!"
@post("/")
async def register_file(data: PostRequest) -> str:
sql = f"""
INSERT INTO source VALUES ('{data.hostname}', '{data.source}', '{data.uuid}');
INSERT INTO shasum VALUES ('{data.shasum}');
INSERT INTO fileinfo VALUES ('{data.file_path}', '{data.uuid}', '{data.shasum}');
"""
try:
db.executescript(sql)
except sqlite3.IntegrityError:
pass
return "ok"
app = Litestar([register_file])