Files
llm-gateway-sub-backup/workspace/utils/prompt_cache.py
2025-08-11 18:56:38 +09:00

33 lines
972 B
Python

import hashlib
import os
from workspace.config.setting import CACHED_PROMPT_DIR
# ✅ 프롬프트 캐시 저장 디렉토리가 없으면 자동 생성
def ensure_cache_dir():
os.makedirs(CACHED_PROMPT_DIR, exist_ok=True)
# ✅ 파일에서 바이트를 읽어옴 (UploadFile 또는 SimpleUploadFile 모두 대응)
def read_file_bytes(upload_file) -> bytes:
upload_file.file.seek(0)
return upload_file.file.read()
# ✅ SHA-256 해시 생성
def compute_file_hash(upload_file) -> str:
content = read_file_bytes(upload_file)
return hashlib.sha256(content).hexdigest()
# ✅ {해시}.txt 형태로 저장
def save_prompt_file_if_not_exists(file_hash: str, upload_file) -> str:
ensure_cache_dir()
file_path = os.path.join(CACHED_PROMPT_DIR, f"{file_hash}.txt")
if not os.path.exists(file_path):
content = read_file_bytes(upload_file)
with open(file_path, "wb") as f:
f.write(content)
return file_path