Files
gh-christianlouis-docuelevate/app/utils.py
T
Christian Krakau-Louis eefafc5775 Update app/utils.py
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
2025-03-28 14:06:17 +01:00

37 lines
1.0 KiB
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()
return sha256.hexdigest()
def log_task_progress(task_id, step_name, status, message=None, file_id=None):
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()