Refa: better MIME content type (#8801)

### What problem does this PR solve?

Better uniform MIME content type.

### Type of change

- [x] Refactoring
This commit is contained in:
Yongteng Lei
2025-07-11 18:47:19 +08:00
committed by GitHub
parent f569401398
commit 72c19b44c3
3 changed files with 58 additions and 11 deletions

View File

@ -31,6 +31,7 @@ from api.db.services.file_service import FileService
from api import settings
from api.utils.api_utils import get_json_result
from api.utils.file_utils import filename_type
from api.utils.web_utils import CONTENT_TYPE_MAP
from rag.utils.storage_factory import STORAGE_IMPL
@ -334,15 +335,14 @@ def get(file_id):
blob = STORAGE_IMPL.get(b, n)
response = flask.make_response(blob)
ext = re.search(r"\.([^.]+)$", file.name)
ext = re.search(r"\.([^.]+)$", file.name.lower())
ext = ext.group(1) if ext else None
if ext:
if file.type == FileType.VISUAL.value:
response.headers.set('Content-Type', 'image/%s' % ext.group(1))
content_type = CONTENT_TYPE_MAP.get(ext, f"image/{ext}")
else:
response.headers.set(
'Content-Type',
'application/%s' %
ext.group(1))
content_type = CONTENT_TYPE_MAP.get(ext, f"application/{ext}")
response.headers.set("Content-Type", content_type)
return response
except Exception as e:
return server_error_response(e)
@ -373,4 +373,4 @@ def move():
FileService.move_file(file_ids, parent_id)
return get_json_result(data=True)
except Exception as e:
return server_error_response(e)
return server_error_response(e)