mirror of
https://github.com/danswer-ai/danswer.git
synced 2025-06-02 11:09:20 +02:00
* work in progress * work in progress * WIP * refactor, use inline attachment for image (base64 encoding doesn't work) * pretty sure this belongs behind a multi_tenant check * code review / refactor --------- Co-authored-by: Richard Kuo (Danswer) <rkuo@onyx.app>
37 lines
1.1 KiB
Python
37 lines
1.1 KiB
Python
from typing import cast
|
|
|
|
import puremagic
|
|
from pydantic import BaseModel
|
|
|
|
from onyx.utils.logger import setup_logger
|
|
|
|
logger = setup_logger()
|
|
|
|
|
|
class FileWithMimeType(BaseModel):
|
|
data: bytes
|
|
mime_type: str
|
|
|
|
|
|
class OnyxStaticFileManager:
|
|
"""Retrieve static resources with this class. Currently, these should all be located
|
|
in the static directory ... e.g. static/images/logo.png"""
|
|
|
|
@staticmethod
|
|
def get_static(filename: str) -> FileWithMimeType | None:
|
|
try:
|
|
mime_type: str = "application/octet-stream"
|
|
with open(filename, "rb") as f:
|
|
file_content = f.read()
|
|
matches = puremagic.magic_string(file_content)
|
|
if matches:
|
|
mime_type = cast(str, matches[0].mime_type)
|
|
except (OSError, FileNotFoundError, PermissionError) as e:
|
|
logger.error(f"Failed to read file {filename}: {e}")
|
|
return None
|
|
except Exception as e:
|
|
logger.error(f"Unexpected exception reading file {filename}: {e}")
|
|
return None
|
|
|
|
return FileWithMimeType(data=file_content, mime_type=mime_type)
|