quivr/backend/packages/files/file.py
Stan Girard a95e311712
feat(ingestion): Add ingestion module and routes (#2393)
This pull request adds the ingestion module and routes to the project.
It includes the necessary files and code changes to implement the
ingestion functionality.
2024-04-01 18:40:56 -07:00

46 lines
1.3 KiB
Python

import hashlib
from io import BytesIO
from fastapi import UploadFile
def convert_bytes(bytes, precision=2):
"""Converts bytes into a human-friendly format."""
abbreviations = ["B", "KB", "MB"]
if bytes <= 0:
return "0 B"
size = bytes
index = 0
while size >= 1024 and index < len(abbreviations) - 1:
size /= 1024
index += 1
return f"{size:.{precision}f} {abbreviations[index]}"
def get_file_size(file: UploadFile):
if isinstance(file.file, BytesIO):
# If the file object is a BytesIO object, get the size of the bytes data
file_size = len(file.file.getvalue())
return file_size
# move the cursor to the end of the file
file.file._file.seek(0, 2) # pyright: ignore reportPrivateUsage=none
file_size = (
file.file._file.tell() # pyright: ignore reportPrivateUsage=none
) # Getting the size of the file
# move the cursor back to the beginning of the file
file.file.seek(0)
return file_size
def compute_sha1_from_file(file_path):
with open(file_path, "rb") as file:
bytes = file.read()
readable_hash = compute_sha1_from_content(bytes)
return readable_hash
def compute_sha1_from_content(content):
readable_hash = hashlib.sha1(content).hexdigest()
return readable_hash