Files
gh-christianlouis-docuelevate/app/utils.py
T
Christian Krakau-Louis f293567f1f Update utils.py
2025-03-28 14:10:18 +01:00

36 lines
941 B
Python

# app/utils.py
import hashlib
from app.database import SessionLocal
from app.models import ProcessingLog
def hash_file(filepath, chunk_size=65536):
"""
Returns the SHA-256 hash of the file at 'filepath'.
Reads the file in chunks to handle large files efficiently.
"""
sha256 = hashlib.sha256()
with open(filepath, "rb") as f:
while True:
data = f.read(chunk_size)
if not data:
break
sha256.update(data)
return sha256.hexdigest()
def log_task_progress(task_id, step_name, status, message=None, file_id=None):
"""
Logs the progress of a Celery task to the database.
"""
with SessionLocal() as db:
log_entry = ProcessingLog(
task_id=task_id,
step_name=step_name,
status=status,
message=message,
file_id=file_id,
)
db.add(log_entry)
db.commit()