Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

1.1 KB
Raw Blame
import base64

from sqlalchemy import Engine, select
from sqlalchemy.orm import sessionmaker
from werkzeug.exceptions import NotFound

from extensions.ext_storage import storage
from models.model import UploadFile

PREVIEW_WORDS_LIMIT = 3000


class AttachmentService:
    _session_maker: sessionmaker

    def __init__(self, session_factory: sessionmaker | Engine | None = None):
        match session_factory:
            case Engine():
                self._session_maker = sessionmaker(bind=session_factory)
            case sessionmaker():
                self._session_maker = session_factory
            case _:
                raise AssertionError("must be a sessionmaker or an Engine.")

    def get_file_base64(self, file_id: str) -> str:
        with self._session_maker(expire_on_commit=False) as session:
            upload_file = session.scalar(select(UploadFile).where(UploadFile.id == file_id).limit(1))
            if not upload_file:
                raise NotFound("File not found")
            upload_file_key = upload_file.key

        blob = storage.load_once(upload_file_key)
        return base64.b64encode(blob).decode()