|
48 | 48 | from open_webui.storage.provider import Storage |
49 | 49 |
|
50 | 50 |
|
51 | | -from open_webui.config import BYPASS_ADMIN_ACCESS_CONTROL |
| 51 | +from open_webui.config import BYPASS_ADMIN_ACCESS_CONTROL, STORAGE_LOCAL_CACHE, STORAGE_PROVIDER, UPLOAD_DIR |
52 | 52 | from open_webui.utils.auth import get_admin_user, get_verified_user |
53 | 53 | from open_webui.utils.misc import strict_match_mime_type |
54 | 54 | from pydantic import BaseModel |
@@ -88,6 +88,20 @@ def _is_text_file(file_path: str, chunk_size: int = 8192) -> bool: |
88 | 88 | return False |
89 | 89 |
|
90 | 90 |
|
| 91 | +def _cleanup_local_cache(file_path: str) -> None: |
| 92 | + """Remove the local cached copy of a cloud-stored file after processing.""" |
| 93 | + if STORAGE_LOCAL_CACHE or STORAGE_PROVIDER == 'local': |
| 94 | + return |
| 95 | + try: |
| 96 | + local_filename = os.path.basename(file_path) |
| 97 | + local_path = os.path.join(UPLOAD_DIR, local_filename) |
| 98 | + if os.path.isfile(local_path): |
| 99 | + os.remove(local_path) |
| 100 | + log.debug(f'Cleaned up local cache: {local_path}') |
| 101 | + except OSError as e: |
| 102 | + log.warning(f'Failed to clean up local cache for {file_path}: {e}') |
| 103 | + |
| 104 | + |
91 | 105 | async def process_uploaded_file( |
92 | 106 | request, |
93 | 107 | file, |
@@ -150,11 +164,14 @@ async def _process_handler(db_session): |
150 | 164 | db=db_session, |
151 | 165 | ) |
152 | 166 |
|
153 | | - if db: |
154 | | - await _process_handler(db) |
155 | | - else: |
156 | | - async with get_async_db_context() as db_session: |
157 | | - await _process_handler(db_session) |
| 167 | + try: |
| 168 | + if db: |
| 169 | + await _process_handler(db) |
| 170 | + else: |
| 171 | + async with get_async_db_context() as db_session: |
| 172 | + await _process_handler(db_session) |
| 173 | + finally: |
| 174 | + _cleanup_local_cache(file_path) |
158 | 175 |
|
159 | 176 |
|
160 | 177 | @router.post('/', response_model=FileModelResponse) |
|
0 commit comments