mirror of
https://github.com/open-webui/open-webui
synced 2025-04-22 23:35:26 +00:00
Feat: Add optional ingest_file and as_attachment params to upload and download endpoints
This commit is contained in:
parent
66015bb341
commit
a805e033a5
@ -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,7 +67,7 @@ def upload_file(
|
|||||||
}
|
}
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
if ingest_file:
|
||||||
try:
|
try:
|
||||||
if file.content_type in [
|
if file.content_type in [
|
||||||
"audio/mpeg",
|
"audio/mpeg",
|
||||||
@ -83,7 +84,6 @@ def upload_file(
|
|||||||
)
|
)
|
||||||
elif file.content_type not in ["image/png", "image/jpeg", "image/gif"]:
|
elif file.content_type not in ["image/png", "image/jpeg", "image/gif"]:
|
||||||
process_file(request, ProcessFileForm(file_id=id), user=user)
|
process_file(request, ProcessFileForm(file_id=id), user=user)
|
||||||
|
|
||||||
file_item = Files.get_file_by_id(id=id)
|
file_item = Files.get_file_by_id(id=id)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
log.exception(e)
|
log.exception(e)
|
||||||
@ -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,9 +246,12 @@ 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"attachment; filename*=UTF-8''{encoded_filename}"
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
if content_type == "application/pdf" or filename.lower().endswith(".pdf"):
|
||||||
headers["Content-Disposition"] = (
|
headers["Content-Disposition"] = (
|
||||||
f"inline; filename*=UTF-8''{encoded_filename}"
|
f"inline; filename*=UTF-8''{encoded_filename}"
|
||||||
)
|
)
|
||||||
|
Loading…
Reference in New Issue
Block a user