2024-11-06 23:13:32 +00:00
|
|
|
import os
|
|
|
|
import shutil
|
|
|
|
|
2024-11-07 02:17:31 +00:00
|
|
|
from typing import BinaryIO, Iterator, Tuple
|
2024-11-06 23:13:32 +00:00
|
|
|
|
|
|
|
from open_webui.constants import ERROR_MESSAGES
|
|
|
|
|
2024-11-07 00:03:58 +00:00
|
|
|
from open_webui.storage.base_storage_provider import LocalFile, StorageProvider
|
2024-11-06 23:13:32 +00:00
|
|
|
|
|
|
|
class LocalStorageProvider(StorageProvider):
|
2024-11-07 04:30:46 +00:00
|
|
|
def __init__(self, folder: str):
|
|
|
|
self.folder: str = folder
|
|
|
|
|
2024-11-07 02:17:31 +00:00
|
|
|
def upload_file(self, file: BinaryIO, filename: str) -> Tuple[bytes, str]:
|
2024-11-06 23:13:32 +00:00
|
|
|
"""Uploads a file to the local file system."""
|
|
|
|
contents = file.read()
|
|
|
|
if not contents:
|
|
|
|
raise ValueError(ERROR_MESSAGES.EMPTY_CONTENT)
|
|
|
|
|
2024-11-07 04:30:46 +00:00
|
|
|
file_path = f"{self.folder}/{filename}"
|
2024-11-06 23:13:32 +00:00
|
|
|
with open(file_path, "wb") as f:
|
|
|
|
f.write(contents)
|
|
|
|
return contents, file_path
|
|
|
|
|
2024-11-07 02:17:31 +00:00
|
|
|
def get_file(self, file_path: str) -> Iterator[bytes]:
|
2024-11-06 23:13:32 +00:00
|
|
|
chunk_size = 8 * 1024
|
|
|
|
with open(file_path, 'rb') as file:
|
|
|
|
while True:
|
|
|
|
chunk = file.read(chunk_size)
|
|
|
|
if not chunk:
|
|
|
|
break
|
|
|
|
yield chunk
|
|
|
|
|
2024-11-07 02:17:31 +00:00
|
|
|
def as_local_file(self, file_path: str) -> LocalFile:
|
2024-11-06 23:13:32 +00:00
|
|
|
return LocalFile(file_path)
|
|
|
|
|
2024-11-07 02:17:31 +00:00
|
|
|
def delete_file(self, filename: str) -> None:
|
2024-11-06 23:13:32 +00:00
|
|
|
"""Deletes a file from the local file system."""
|
2024-11-07 04:30:46 +00:00
|
|
|
file_path = f"{self.folder}/{filename}"
|
2024-11-06 23:13:32 +00:00
|
|
|
if os.path.isfile(file_path):
|
|
|
|
os.remove(file_path)
|
|
|
|
else:
|
|
|
|
print(f"File {file_path} not found in local storage.")
|
|
|
|
|
2024-11-07 04:51:53 +00:00
|
|
|
def delete_all_files(self, folder) -> None:
|
2024-11-06 23:13:32 +00:00
|
|
|
"""Deletes all files from the storage."""
|
2024-11-07 04:51:53 +00:00
|
|
|
folder_to_delete = f"{self.folder}/{folder}"
|
|
|
|
if os.path.exists(folder_to_delete):
|
|
|
|
for filename in os.listdir(folder_to_delete):
|
|
|
|
file_path = os.path.join(folder_to_delete, filename)
|
2024-11-06 23:13:32 +00:00
|
|
|
try:
|
|
|
|
if os.path.isfile(file_path) or os.path.islink(file_path):
|
|
|
|
os.unlink(file_path) # Remove the file or link
|
|
|
|
elif os.path.isdir(file_path):
|
|
|
|
shutil.rmtree(file_path) # Remove the directory
|
|
|
|
except Exception as e:
|
|
|
|
print(f"Failed to delete {file_path}. Reason: {e}")
|
|
|
|
else:
|
2024-11-07 04:51:53 +00:00
|
|
|
print(f"Directory {folder_to_delete} not found in local storage.")
|