Skip to content

feat: File URL adaptation #3459

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion apps/application/models/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class Application(AppModelMixin):
model_params_setting = models.JSONField(verbose_name="模型参数相关设置", default=dict)
tts_model_params_setting = models.JSONField(verbose_name="模型参数相关设置", default=dict)
problem_optimization = models.BooleanField(verbose_name="问题优化", default=False)
icon = models.CharField(max_length=256, verbose_name="应用icon", default="/ui/favicon.ico")
icon = models.CharField(max_length=256, verbose_name="应用icon", default="./favicon.ico")
work_flow = models.JSONField(verbose_name="工作流数据", default=dict)
type = models.CharField(verbose_name="应用类型", choices=ApplicationTypeChoices.choices,
default=ApplicationTypeChoices.SIMPLE, max_length=256)
Expand Down
4 changes: 3 additions & 1 deletion apps/maxkb/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,10 @@
path(admin_api_prefix, include("knowledge.urls")),
path(admin_api_prefix, include("system_manage.urls")),
path(admin_api_prefix, include("application.urls")),
path(admin_api_prefix, include("oss.urls")),
path(chat_api_prefix, include("chat.urls")),
path('oss/', include('oss.urls')),
path(f'{admin_ui_prefix[1:]}/', include('oss.retrieval_urls')),
path(f'{chat_ui_prefix[1:]}/', include('oss.retrieval_urls')),
]
urlpatterns += [
path('schema/', SpectacularAPIView.as_view(), name='schema'), # schema的配置文件的路由,下面两个ui也是根据这个配置文件来生成的
Expand Down
21 changes: 21 additions & 0 deletions apps/oss/retrieval_urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# coding=utf-8
"""
@project: MaxKB
@Author:虎虎
@file: retrieval_urls.py
@date:2025/7/2 19:01
@desc:
"""
from django.urls import re_path

from . import views

app_name = 'oss'

urlpatterns = [
re_path(rf'^(.*)/oss/file/(?P<file_id>[\w-]+)/?$',
views.FileRetrievalView.as_view()),
re_path(rf'oss/file/(?P<file_id>[\w-]+)/?$',
views.FileRetrievalView.as_view()),

]
2 changes: 1 addition & 1 deletion apps/oss/serializers/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def upload(self, with_valid=True):
file_id = meta.get('file_id', uuid.uuid7())
file = File(id=file_id, file_name=self.data.get('file').name, meta=meta)
file.save(self.data.get('file').read())
return f'/oss/file/{file_id}'
return f'./oss/file/{file_id}'

class Operate(serializers.Serializer):
id = serializers.UUIDField(required=True)
Expand Down
3 changes: 1 addition & 2 deletions apps/oss/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,5 @@
app_name = 'oss'

urlpatterns = [
path('file', views.FileView.as_view()),
path('file/<str:file_id>', views.FileView.Operate.as_view()),
path('oss/file', views.FileView.as_view()),
]
26 changes: 15 additions & 11 deletions apps/oss/views/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,20 @@
from oss.serializers.file import FileSerializer


class FileRetrievalView(APIView):
@extend_schema(
methods=['GET'],
summary=_('Get file'),
description=_('Get file'),
operation_id=_('Get file'), # type: ignore
parameters=FileGetAPI.get_parameters(),
responses=FileGetAPI.get_response(),
tags=[_('File')] # type: ignore
)
def get(self, request: Request, file_id: str):
return FileSerializer.Operate(data={'id': file_id}).get()


class FileView(APIView):
authentication_classes = [TokenAuth]
parser_classes = [MultiPartParser]
Expand All @@ -31,17 +45,7 @@ def post(self, request: Request):
return result.success(FileSerializer(data={'file': request.FILES.get('file')}).upload())

class Operate(APIView):
@extend_schema(
methods=['GET'],
summary=_('Get file'),
description=_('Get file'),
operation_id=_('Get file'), # type: ignore
parameters=FileGetAPI.get_parameters(),
responses=FileGetAPI.get_response(),
tags=[_('File')] # type: ignore
)
def get(self, request: Request, file_id: str):
return FileSerializer.Operate(data={'id': file_id}).get()
authentication_classes = [TokenAuth]

@extend_schema(
methods=['DELETE'],
Expand Down
4 changes: 2 additions & 2 deletions ui/src/api/image.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Result } from '@/request/Result'
import { get, post, del, put } from '@/request/index'

const prefix = '/image'
const prefix = '/oss/file'
/**
* 上传图片
* @param 参数 file:file
Expand All @@ -11,5 +11,5 @@ const postImage: (data: any) => Promise<Result<any>> = (data) => {
}

export default {
postImage
postImage,
}
16 changes: 11 additions & 5 deletions ui/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,6 @@ export default defineConfig((conf: any) => {
target: 'http://127.0.0.1:8080',
changeOrigin: true,
}
proxyConf['/oss'] = {
target: 'http://127.0.0.1:8080',
changeOrigin: true,
rewrite: (path: string) => path.replace(ENV.VITE_BASE_PATH, '/'),
}
proxyConf['/chat/api'] = {
target: 'http://127.0.0.1:8080',
changeOrigin: true,
Expand All @@ -64,6 +59,17 @@ export default defineConfig((conf: any) => {
changeOrigin: true,
rewrite: (path: string) => path.replace(ENV.VITE_BASE_PATH, '/'),
}

// 前端静态资源转发到本身
proxyConf[`^${ENV.VITE_BASE_PATH}.+\/oss\/file\/.*$`] = {
target: `http://127.0.0.1:8080`,
changeOrigin: true,
}
// 前端静态资源转发到本身
proxyConf[`^${ENV.VITE_BASE_PATH}oss\/file\/.*$`] = {
target: `http://127.0.0.1:8080`,
changeOrigin: true,
}
// 前端静态资源转发到本身
proxyConf[ENV.VITE_BASE_PATH] = {
target: `http://127.0.0.1:${ENV.VITE_APP_PORT}`,
Expand Down
Loading