perf: 上传文件大小限制

pull/12328/head
wangruidong 2023-12-12 19:15:47 +08:00 committed by 老广
parent dce1079fdc
commit 27daebbe1b
7 changed files with 44 additions and 13 deletions

View File

@ -599,6 +599,8 @@ class Config(dict):
'GPT_PROXY': '',
'GPT_MODEL': 'gpt-3.5-turbo',
'VIRTUAL_APP_ENABLED': False,
'FILE_UPLOAD_SIZE_LIMIT_MB': 200
}
old_config_map = {

View File

@ -221,3 +221,5 @@ GPT_PROXY = CONFIG.GPT_PROXY
GPT_MODEL = CONFIG.GPT_MODEL
VIRTUAL_APP_ENABLED = CONFIG.VIRTUAL_APP_ENABLED
FILE_UPLOAD_SIZE_LIMIT_MB = CONFIG.FILE_UPLOAD_SIZE_LIMIT_MB

View File

@ -1,3 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:7d9296340c0815fcda81bc7370051b1408225ba63ab7067b397595d4dfb334f7
size 167881
oid sha256:d98ad65b020a577f1e7542c3e8c0c2a289b67d12c39f4d35a5db667fd218e0ea
size 168292

View File

@ -8,7 +8,7 @@ msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2023-12-13 16:13+0800\n"
"POT-Creation-Date: 2023-12-13 18:29+0800\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
@ -4051,10 +4051,16 @@ msgstr "タスクは存在しません"
msgid "Task {} args or kwargs error"
msgstr "タスク実行パラメータエラー"
#: ops/api/job.py:124
#: ops/api/job.py:132
msgid "Duplicate file exists"
msgstr "重複したファイルが存在する"
#: ops/api/job.py:137
#, python-brace-format
msgid ""
"File size exceeds maximum limit. Please select a file smaller than {limit}MB"
msgstr "ファイルサイズが最大制限を超えています。{limit}MB より小さいファイルを選択してください。"
#: ops/api/playbook.py:39
msgid "Currently playbook is being used in a job"
msgstr "現在プレイブックは1つのジョブで使用されています"

View File

@ -1,3 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:37662b3fc6df22480304d2a5f90cf156ba7d4e0249ec11076e5018daab545714
size 137610
oid sha256:92bd4bd4a6d342dcd259215f16baeedb17fd95d321d4527bb3601ce1dfd72202
size 137882

View File

@ -7,7 +7,7 @@ msgid ""
msgstr ""
"Project-Id-Version: JumpServer 0.3.3\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2023-12-13 16:13+0800\n"
"POT-Creation-Date: 2023-12-13 18:29+0800\n"
"PO-Revision-Date: 2021-05-20 10:54+0800\n"
"Last-Translator: ibuler <ibuler@qq.com>\n"
"Language-Team: JumpServer team<ibuler@qq.com>\n"
@ -4003,10 +4003,16 @@ msgstr "任务 {} 不存在"
msgid "Task {} args or kwargs error"
msgstr "任务 {} 执行参数错误"
#: ops/api/job.py:124
#: ops/api/job.py:132
msgid "Duplicate file exists"
msgstr "存在同名文件"
#: ops/api/job.py:137
#, python-brace-format
msgid ""
"File size exceeds maximum limit. Please select a file smaller than {limit}MB"
msgstr "文件大小超过最大限制。请选择小于 {limit}MB 的文件。"
#: ops/api/playbook.py:39
msgid "Currently playbook is being used in a job"
msgstr "当前 playbook 正在作业中使用"

View File

@ -99,7 +99,7 @@ class JobViewSet(OrgBulkModelViewSet):
lambda: run_ops_job_execution.apply_async((str(execution.id),), task_id=str(execution.id)))
@staticmethod
def get_duplicates_filenames(files):
def get_duplicates_files(files):
seen = set()
duplicates = set()
for file in files:
@ -109,6 +109,14 @@ class JobViewSet(OrgBulkModelViewSet):
seen.add(file)
return list(duplicates)
@staticmethod
def get_exceeds_limit_files(files):
exceeds_limit_files = []
for file in files:
if file.size > settings.FILE_UPLOAD_SIZE_LIMIT_MB * 1024 * 1024:
exceeds_limit_files.append(file)
return exceeds_limit_files
@action(methods=[POST], detail=False, serializer_class=FileSerializer, permission_classes=[IsValidUser, ],
url_path='upload')
def upload(self, request, *args, **kwargs):
@ -117,11 +125,18 @@ class JobViewSet(OrgBulkModelViewSet):
if not serializer.is_valid():
msg = 'Upload data invalid: {}'.format(serializer.errors)
return Response({'msg': msg}, status=400)
return Response({'error': msg}, status=400)
same_filenames = self.get_duplicates_filenames(uploaded_files)
if same_filenames:
return Response({'msg': _("Duplicate file exists")}, status=400)
same_files = self.get_duplicates_files(uploaded_files)
if same_files:
return Response({'error': _("Duplicate file exists")}, status=400)
exceeds_limit_files = self.get_exceeds_limit_files(uploaded_files)
if exceeds_limit_files:
return Response(
{'error': _("File size exceeds maximum limit. Please select a file smaller than {limit}MB").format(
limit=settings.FILE_UPLOAD_SIZE_LIMIT_MB)},
status=400)
job_id = request.data.get('job_id', '')
job = get_object_or_404(Job, pk=job_id)