Skip to content

Commit 8172c7e

Browse files
committed
refac
1 parent 498ff8c commit 8172c7e

3 files changed

Lines changed: 26 additions & 8 deletions

File tree

backend/open_webui/config.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -915,6 +915,7 @@ def feishu_oauth_register(oauth: OAuth):
915915
####################################
916916

917917
STORAGE_PROVIDER = os.environ.get('STORAGE_PROVIDER', 'local') # defaults to local, s3
918+
STORAGE_LOCAL_CACHE = os.environ.get('STORAGE_LOCAL_CACHE', 'true').lower() == 'true'
918919

919920
S3_ACCESS_KEY_ID = os.environ.get('S3_ACCESS_KEY_ID', None)
920921
S3_SECRET_ACCESS_KEY = os.environ.get('S3_SECRET_ACCESS_KEY', None)

backend/open_webui/routers/files.py

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
from open_webui.storage.provider import Storage
4949

5050

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
5252
from open_webui.utils.auth import get_admin_user, get_verified_user
5353
from open_webui.utils.misc import strict_match_mime_type
5454
from pydantic import BaseModel
@@ -88,6 +88,20 @@ def _is_text_file(file_path: str, chunk_size: int = 8192) -> bool:
8888
return False
8989

9090

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+
91105
async def process_uploaded_file(
92106
request,
93107
file,
@@ -150,11 +164,14 @@ async def _process_handler(db_session):
150164
db=db_session,
151165
)
152166

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)
158175

159176

160177
@router.post('/', response_model=FileModelResponse)

backend/open_webui/storage/provider.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ def sanitize_tag_value(s: str) -> str:
140140

141141
def upload_file(self, file: BinaryIO, filename: str, tags: Dict[str, str]) -> Tuple[bytes, str]:
142142
"""Handles uploading of the file to S3 storage."""
143-
_, file_path = LocalStorageProvider.upload_file(file, filename, tags)
143+
contents, file_path = LocalStorageProvider.upload_file(file, filename, tags)
144144
s3_key = os.path.join(self.key_prefix, filename)
145145
try:
146146
self.s3_client.upload_file(file_path, self.bucket_name, s3_key)
@@ -153,7 +153,7 @@ def upload_file(self, file: BinaryIO, filename: str, tags: Dict[str, str]) -> Tu
153153
Tagging=tagging,
154154
)
155155
return (
156-
open(file_path, 'rb').read(),
156+
contents,
157157
f's3://{self.bucket_name}/{s3_key}',
158158
)
159159
except ClientError as e:

0 commit comments

Comments
 (0)