Skip to content

Commit

Permalink
fix formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
srijanpatel committed Jan 20, 2025
1 parent 877212e commit 857b2dd
Showing 1 changed file with 16 additions and 15 deletions.
31 changes: 16 additions & 15 deletions backend/app/utils/file_utils.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
import base64
import mimetypes
from pathlib import Path
from typing import Optional


def encode_file_to_base64_data_url(file_path: str) -> str:
"""
Read a file and encode it as a base64 data URL with the appropriate MIME type.
"""
path = Path(file_path)
mime_type = mimetypes.guess_type(path)[0] or 'application/octet-stream'
mime_type = mimetypes.guess_type(path)[0] or "application/octet-stream"

with open(path, 'rb') as f:
with open(path, "rb") as f:
file_content = f.read()
base64_data = base64.b64encode(file_content).decode('utf-8')
return f'data:{mime_type};base64,{base64_data}'
base64_data = base64.b64encode(file_content).decode("utf-8")
return f"data:{mime_type};base64,{base64_data}"


def get_file_mime_type(file_path: str) -> str:
"""
Expand All @@ -24,14 +25,14 @@ def get_file_mime_type(file_path: str) -> str:
# Default MIME types for common file types
ext = Path(file_path).suffix.lower()
mime_map = {
'.pdf': 'application/pdf',
'.txt': 'text/plain',
'.jpg': 'image/jpeg',
'.jpeg': 'image/jpeg',
'.png': 'image/png',
'.gif': 'image/gif',
'.mp3': 'audio/mpeg',
'.mp4': 'video/mp4',
".pdf": "application/pdf",
".txt": "text/plain",
".jpg": "image/jpeg",
".jpeg": "image/jpeg",
".png": "image/png",
".gif": "image/gif",
".mp3": "audio/mpeg",
".mp4": "video/mp4",
}
mime_type = mime_map.get(ext, 'application/octet-stream')
return mime_type
mime_type = mime_map.get(ext, "application/octet-stream")
return mime_type

0 comments on commit 857b2dd

Please sign in to comment.