mirror of
https://github.com/open-webui/open-webui
synced 2025-06-03 19:27:12 +00:00
Merge pull request #12176 from JunaidPinjari/feat/file-upload-download-options
Support skipping ingestion and downloading files as attachments
This commit is contained in:
commit
da5ea7acb8
@ -5,7 +5,7 @@ from pathlib import Path
|
|||||||
from typing import Optional
|
from typing import Optional
|
||||||
from urllib.parse import quote
|
from urllib.parse import quote
|
||||||
|
|
||||||
from fastapi import APIRouter, Depends, File, HTTPException, Request, UploadFile, status
|
from fastapi import APIRouter, Depends, File, HTTPException, Request, UploadFile, status, Query
|
||||||
from fastapi.responses import FileResponse, StreamingResponse
|
from fastapi.responses import FileResponse, StreamingResponse
|
||||||
from open_webui.constants import ERROR_MESSAGES
|
from open_webui.constants import ERROR_MESSAGES
|
||||||
from open_webui.env import SRC_LOG_LEVELS
|
from open_webui.env import SRC_LOG_LEVELS
|
||||||
@ -38,6 +38,7 @@ def upload_file(
|
|||||||
file: UploadFile = File(...),
|
file: UploadFile = File(...),
|
||||||
user=Depends(get_verified_user),
|
user=Depends(get_verified_user),
|
||||||
file_metadata: dict = {},
|
file_metadata: dict = {},
|
||||||
|
ingest_file: bool = Query(True)
|
||||||
):
|
):
|
||||||
log.info(f"file.content_type: {file.content_type}")
|
log.info(f"file.content_type: {file.content_type}")
|
||||||
try:
|
try:
|
||||||
@ -66,34 +67,33 @@ def upload_file(
|
|||||||
}
|
}
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
if ingest_file:
|
||||||
try:
|
try:
|
||||||
if file.content_type in [
|
if file.content_type in [
|
||||||
"audio/mpeg",
|
"audio/mpeg",
|
||||||
"audio/wav",
|
"audio/wav",
|
||||||
"audio/ogg",
|
"audio/ogg",
|
||||||
"audio/x-m4a",
|
"audio/x-m4a",
|
||||||
]:
|
]:
|
||||||
file_path = Storage.get_file(file_path)
|
file_path = Storage.get_file(file_path)
|
||||||
result = transcribe(request, file_path)
|
result = transcribe(request, file_path)
|
||||||
process_file(
|
process_file(
|
||||||
request,
|
request,
|
||||||
ProcessFileForm(file_id=id, content=result.get("text", "")),
|
ProcessFileForm(file_id=id, content=result.get("text", "")),
|
||||||
user=user,
|
user=user,
|
||||||
|
)
|
||||||
|
elif file.content_type not in ["image/png", "image/jpeg", "image/gif"]:
|
||||||
|
process_file(request, ProcessFileForm(file_id=id), user=user)
|
||||||
|
file_item = Files.get_file_by_id(id=id)
|
||||||
|
except Exception as e:
|
||||||
|
log.exception(e)
|
||||||
|
log.error(f"Error processing file: {file_item.id}")
|
||||||
|
file_item = FileModelResponse(
|
||||||
|
**{
|
||||||
|
**file_item.model_dump(),
|
||||||
|
"error": str(e.detail) if hasattr(e, "detail") else str(e),
|
||||||
|
}
|
||||||
)
|
)
|
||||||
elif file.content_type not in ["image/png", "image/jpeg", "image/gif"]:
|
|
||||||
process_file(request, ProcessFileForm(file_id=id), user=user)
|
|
||||||
|
|
||||||
file_item = Files.get_file_by_id(id=id)
|
|
||||||
except Exception as e:
|
|
||||||
log.exception(e)
|
|
||||||
log.error(f"Error processing file: {file_item.id}")
|
|
||||||
file_item = FileModelResponse(
|
|
||||||
**{
|
|
||||||
**file_item.model_dump(),
|
|
||||||
"error": str(e.detail) if hasattr(e, "detail") else str(e),
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
if file_item:
|
if file_item:
|
||||||
return file_item
|
return file_item
|
||||||
@ -228,7 +228,7 @@ async def update_file_data_content_by_id(
|
|||||||
|
|
||||||
|
|
||||||
@router.get("/{id}/content")
|
@router.get("/{id}/content")
|
||||||
async def get_file_content_by_id(id: str, user=Depends(get_verified_user)):
|
async def get_file_content_by_id(id: str, user=Depends(get_verified_user), as_attachment: bool = Query(False)):
|
||||||
file = Files.get_file_by_id(id)
|
file = Files.get_file_by_id(id)
|
||||||
if file and (file.user_id == user.id or user.role == "admin"):
|
if file and (file.user_id == user.id or user.role == "admin"):
|
||||||
try:
|
try:
|
||||||
@ -246,17 +246,20 @@ async def get_file_content_by_id(id: str, user=Depends(get_verified_user)):
|
|||||||
encoded_filename = quote(filename)
|
encoded_filename = quote(filename)
|
||||||
headers = {}
|
headers = {}
|
||||||
|
|
||||||
if content_type == "application/pdf" or filename.lower().endswith(
|
if as_attachment:
|
||||||
".pdf"
|
|
||||||
):
|
|
||||||
headers["Content-Disposition"] = (
|
|
||||||
f"inline; filename*=UTF-8''{encoded_filename}"
|
|
||||||
)
|
|
||||||
content_type = "application/pdf"
|
|
||||||
elif content_type != "text/plain":
|
|
||||||
headers["Content-Disposition"] = (
|
headers["Content-Disposition"] = (
|
||||||
f"attachment; filename*=UTF-8''{encoded_filename}"
|
f"attachment; filename*=UTF-8''{encoded_filename}"
|
||||||
)
|
)
|
||||||
|
else:
|
||||||
|
if content_type == "application/pdf" or filename.lower().endswith(".pdf"):
|
||||||
|
headers["Content-Disposition"] = (
|
||||||
|
f"inline; filename*=UTF-8''{encoded_filename}"
|
||||||
|
)
|
||||||
|
content_type = "application/pdf"
|
||||||
|
elif content_type != "text/plain":
|
||||||
|
headers["Content-Disposition"] = (
|
||||||
|
f"attachment; filename*=UTF-8''{encoded_filename}"
|
||||||
|
)
|
||||||
|
|
||||||
return FileResponse(file_path, headers=headers, media_type=content_type)
|
return FileResponse(file_path, headers=headers, media_type=content_type)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user