open-webui/backend/open_webui/apps/webui/routers/files.py

347 lines
10 KiB
Python
Raw Normal View History

2024-08-27 22:10:27 +00:00
import logging
2024-06-18 18:36:55 +00:00
import os
2024-08-27 22:10:27 +00:00
import shutil
2024-06-18 18:36:55 +00:00
import uuid
2024-08-27 22:10:27 +00:00
from pathlib import Path
from typing import Optional
2024-10-04 07:23:14 +00:00
from pydantic import BaseModel
2024-10-05 00:22:00 +00:00
import mimetypes
2024-06-18 18:36:55 +00:00
from open_webui.apps.webui.models.files import FileForm, FileModel, Files
2024-10-04 05:22:22 +00:00
from open_webui.apps.retrieval.main import process_file, ProcessFileForm
2024-10-05 00:22:00 +00:00
from open_webui.config import UPLOAD_DIR, DOCS_DIR
from open_webui.env import SRC_LOG_LEVELS
2024-10-05 00:22:00 +00:00
from open_webui.constants import ERROR_MESSAGES
2024-10-03 03:42:10 +00:00
2024-08-27 22:10:27 +00:00
from fastapi import APIRouter, Depends, File, HTTPException, UploadFile, status
2024-10-02 13:19:09 +00:00
from fastapi.responses import FileResponse, StreamingResponse
2024-10-05 00:22:00 +00:00
from open_webui.utils.utils import get_admin_user, get_verified_user
2024-06-18 18:36:55 +00:00
log = logging.getLogger(__name__)
log.setLevel(SRC_LOG_LEVELS["MODELS"])
router = APIRouter()
############################
# Upload File
############################
@router.post("/")
2024-08-22 14:08:03 +00:00
def upload_file(file: UploadFile = File(...), user=Depends(get_verified_user)):
2024-06-18 18:36:55 +00:00
log.info(f"file.content_type: {file.content_type}")
try:
unsanitized_filename = file.filename
filename = os.path.basename(unsanitized_filename)
# replace filename with uuid
id = str(uuid.uuid4())
2024-07-15 11:05:38 +00:00
name = filename
2024-06-18 21:15:08 +00:00
filename = f"{id}_{filename}"
2024-06-18 18:36:55 +00:00
file_path = f"{UPLOAD_DIR}/{filename}"
contents = file.file.read()
with open(file_path, "wb") as f:
f.write(contents)
f.close()
file = Files.insert_new_file(
2024-06-18 20:50:18 +00:00
user.id,
FileForm(
**{
"id": id,
"filename": filename,
"meta": {
2024-07-15 11:05:38 +00:00
"name": name,
2024-06-18 20:50:18 +00:00
"content_type": file.content_type,
"size": len(contents),
"path": file_path,
},
}
),
2024-06-18 18:36:55 +00:00
)
2024-10-04 05:22:22 +00:00
try:
process_file(ProcessFileForm(file_id=id))
file = Files.get_file_by_id(id=id)
except Exception as e:
log.exception(e)
log.error(f"Error processing file: {file.id}")
2024-06-18 18:36:55 +00:00
if file:
return file
else:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail=ERROR_MESSAGES.DEFAULT("Error uploading file"),
)
except Exception as e:
log.exception(e)
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail=ERROR_MESSAGES.DEFAULT(e),
)
2024-10-05 00:22:00 +00:00
@router.post("/upload/dir")
def upload_dir(user=Depends(get_admin_user)):
2024-10-05 01:22:55 +00:00
file_ids = []
2024-10-05 00:22:00 +00:00
for path in Path(DOCS_DIR).rglob("./**/*"):
if path.is_file() and not path.name.startswith("."):
try:
log.debug(f"Processing file from path: {path}")
filename = path.name
file_content_type = mimetypes.guess_type(path)
# replace filename with uuid
id = str(uuid.uuid4())
name = filename
contents = path.read_bytes()
file_path = str(path)
file = Files.insert_new_file(
user.id,
FileForm(
**{
"id": id,
"filename": filename,
"meta": {
"name": name,
"content_type": file_content_type,
"size": len(contents),
"path": file_path,
},
}
),
)
try:
process_file(ProcessFileForm(file_id=id))
log.debug(f"File processed: {path}, {file.id}")
2024-10-05 01:22:55 +00:00
file_ids.append(file.id)
2024-10-05 00:22:00 +00:00
except Exception as e:
log.exception(e)
log.error(f"Error processing file: {file.id}")
except Exception as e:
log.exception(e)
pass
2024-10-05 01:22:55 +00:00
return file_ids
2024-10-05 00:22:00 +00:00
2024-06-18 18:36:55 +00:00
############################
# List Files
############################
2024-08-14 12:46:31 +00:00
@router.get("/", response_model=list[FileModel])
2024-08-22 14:08:03 +00:00
async def list_files(user=Depends(get_verified_user)):
2024-08-23 14:19:04 +00:00
if user.role == "admin":
files = Files.get_files()
else:
files = Files.get_files_by_user_id(user.id)
2024-06-18 18:36:55 +00:00
return files
2024-06-18 22:20:04 +00:00
############################
# Delete All Files
############################
@router.delete("/all")
async def delete_all_files(user=Depends(get_admin_user)):
result = Files.delete_all_files()
2024-06-18 22:20:04 +00:00
if result:
folder = f"{UPLOAD_DIR}"
try:
# Check if the directory exists
if os.path.exists(folder):
# Iterate over all the files and directories in the specified directory
for filename in os.listdir(folder):
file_path = os.path.join(folder, filename)
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:
print(f"The directory {folder} does not exist")
except Exception as e:
print(f"Failed to process the directory {folder}. Reason: {e}")
return {"message": "All files deleted successfully"}
else:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail=ERROR_MESSAGES.DEFAULT("Error deleting files"),
)
2024-06-18 18:36:55 +00:00
############################
# Get File By Id
############################
@router.get("/{id}", response_model=Optional[FileModel])
2024-08-22 14:08:03 +00:00
async def get_file_by_id(id: str, user=Depends(get_verified_user)):
file = Files.get_file_by_id(id)
2024-06-18 18:36:55 +00:00
2024-08-23 14:19:04 +00:00
if file and (file.user_id == user.id or user.role == "admin"):
2024-06-18 18:36:55 +00:00
return file
else:
raise HTTPException(
2024-06-20 20:49:04 +00:00
status_code=status.HTTP_404_NOT_FOUND,
2024-06-18 18:36:55 +00:00
detail=ERROR_MESSAGES.NOT_FOUND,
)
2024-10-04 07:23:14 +00:00
############################
# Get File Data Content By Id
############################
@router.get("/{id}/data/content")
async def get_file_data_content_by_id(id: str, user=Depends(get_verified_user)):
file = Files.get_file_by_id(id)
if file and (file.user_id == user.id or user.role == "admin"):
return {"content": file.data.get("content", "")}
else:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail=ERROR_MESSAGES.NOT_FOUND,
)
############################
# Update File Data Content By Id
############################
class ContentForm(BaseModel):
content: str
@router.post("/{id}/data/content/update")
async def update_file_data_content_by_id(
id: str, form_data: ContentForm, user=Depends(get_verified_user)
):
file = Files.get_file_by_id(id)
if file and (file.user_id == user.id or user.role == "admin"):
try:
process_file(ProcessFileForm(file_id=id, content=form_data.content))
file = Files.get_file_by_id(id=id)
except Exception as e:
log.exception(e)
log.error(f"Error processing file: {file.id}")
return {"content": file.data.get("content", "")}
else:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail=ERROR_MESSAGES.NOT_FOUND,
)
2024-06-18 21:33:44 +00:00
############################
# Get File Content By Id
############################
@router.get("/{id}/content", response_model=Optional[FileModel])
2024-08-22 14:08:03 +00:00
async def get_file_content_by_id(id: str, user=Depends(get_verified_user)):
file = Files.get_file_by_id(id)
2024-06-18 21:33:44 +00:00
2024-08-23 14:19:04 +00:00
if file and (file.user_id == user.id or user.role == "admin"):
2024-06-22 21:49:00 +00:00
file_path = Path(file.meta["path"])
# Check if the file already exists in the cache
if file_path.is_file():
print(f"file_path: {file_path}")
return FileResponse(file_path)
else:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail=ERROR_MESSAGES.NOT_FOUND,
)
else:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail=ERROR_MESSAGES.NOT_FOUND,
)
@router.get("/{id}/content/{file_name}", response_model=Optional[FileModel])
2024-08-22 14:08:03 +00:00
async def get_file_content_by_id(id: str, user=Depends(get_verified_user)):
2024-06-22 21:49:00 +00:00
file = Files.get_file_by_id(id)
2024-08-23 14:19:04 +00:00
if file and (file.user_id == user.id or user.role == "admin"):
2024-10-02 13:19:09 +00:00
file_path = file.meta.get("path")
if file_path:
file_path = Path(file_path)
# Check if the file already exists in the cache
if file_path.is_file():
print(f"file_path: {file_path}")
return FileResponse(file_path)
else:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail=ERROR_MESSAGES.NOT_FOUND,
)
2024-06-18 21:33:44 +00:00
else:
2024-10-02 13:19:09 +00:00
# File path doesnt exist, return the content as .txt if possible
file_content = file.content.get("content", "")
file_name = file.filename
# Create a generator that encodes the file content
def generator():
yield file_content.encode("utf-8")
return StreamingResponse(
generator(),
media_type="text/plain",
headers={"Content-Disposition": f"attachment; filename={file_name}"},
2024-06-18 21:33:44 +00:00
)
else:
raise HTTPException(
2024-06-20 20:49:04 +00:00
status_code=status.HTTP_404_NOT_FOUND,
2024-06-18 21:33:44 +00:00
detail=ERROR_MESSAGES.NOT_FOUND,
)
2024-06-18 18:36:55 +00:00
############################
# Delete File By Id
############################
@router.delete("/{id}")
2024-08-22 14:08:03 +00:00
async def delete_file_by_id(id: str, user=Depends(get_verified_user)):
file = Files.get_file_by_id(id)
2024-08-23 14:19:04 +00:00
if file and (file.user_id == user.id or user.role == "admin"):
result = Files.delete_file_by_id(id)
2024-06-18 18:36:55 +00:00
if result:
return {"message": "File deleted successfully"}
else:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail=ERROR_MESSAGES.DEFAULT("Error deleting file"),
)
else:
raise HTTPException(
2024-06-20 20:49:04 +00:00
status_code=status.HTTP_404_NOT_FOUND,
2024-06-18 18:36:55 +00:00
detail=ERROR_MESSAGES.NOT_FOUND,
)