功能变化: 支持富文本

pull/102/MERGE
李强 2023-07-28 22:52:36 +08:00
parent 5c80c9e755
commit 70e1a077aa
2 changed files with 38 additions and 19 deletions

View File

@ -333,20 +333,20 @@ class FileList(CoreModel):
instance.url = os.path.join(file_path.replace('media/', ''), file_name)
if file_engine == 'oss':
from dvadmin_cloud_storage.views.aliyun import ali_oss_upload
file = File(open(os.path.join(BASE_DIR, file_path, file_name)))
file_path = ali_oss_upload(file, file_name=os.path.join(file_path.replace('media/', ''), file_name))
if file_path:
instance.file_url = file_path
else:
raise ValueError("上传失败")
with open(os.path.join(BASE_DIR, file_path, file_name)) as file:
file_path = ali_oss_upload(file, file_name=os.path.join(file_path.replace('media/', ''), file_name))
if file_path:
instance.file_url = file_path
else:
raise ValueError("上传失败")
elif file_engine == 'cos':
from dvadmin_cloud_storage.views.tencent import tencent_cos_upload
file = File(open(os.path.join(BASE_DIR, file_path, file_name)))
file_path = tencent_cos_upload(file, file_name=os.path.join(file_path.replace('media/', ''), file_name))
if file_path:
instance.file_url = file_path
else:
raise ValueError("上传失败")
with open(os.path.join(BASE_DIR, file_path, file_name)) as file:
file_path = tencent_cos_upload(file, file_name=os.path.join(file_path.replace('media/', ''), file_name))
if file_path:
instance.file_url = file_path
else:
raise ValueError("上传失败")
else:
instance.url = os.path.join(file_path.replace('media/', ''), file_name)
instance.save()

View File

@ -11,9 +11,10 @@ from django.views.decorators.csrf import csrf_exempt
from rest_framework import serializers
from rest_framework.decorators import action
from application.settings import BASE_DIR
from application import dispatch
from application import dispatch, settings
from dvadmin.system.models import FileList, media_file_name
from dvadmin.system.views.ueditor_settings import ueditor_upload_settings, ueditor_settings
from dvadmin.utils.json_response import DetailResponse
from dvadmin.utils.serializers import CustomModelSerializer
from dvadmin.utils.string_util import format_bytes
from dvadmin.utils.viewset import CustomModelViewSet
@ -23,6 +24,16 @@ class FileSerializer(CustomModelSerializer):
url = serializers.SerializerMethodField(read_only=True)
def get_url(self, instance):
if self.request.query_params.get('prefix'):
if settings.ENVIRONMENT in ['local']:
prefix = 'http://127.0.0.1:8000'
elif settings.ENVIRONMENT in ['test']:
prefix = 'http://{host}/api'.format(host=self.request.get_host())
else:
prefix = 'https://{host}/api'.format(host=self.request.get_host())
if instance.file_url:
return instance.file_url if instance.file_url.startswith('http') else f"{prefix}/{instance.file_url}"
return (f'{prefix}/media/{str(instance.url)}')
return instance.file_url or (f'media/{str(instance.url)}')
class Meta:
@ -89,6 +100,12 @@ class FileViewSet(CustomModelViewSet):
filter_fields = ['name', ]
permission_classes = []
def create(self, request, *args, **kwargs):
serializer = self.get_serializer(data=request.data, request=request)
serializer.is_valid(raise_exception=True)
self.perform_create(serializer)
return DetailResponse(data=serializer.data, msg="新增成功")
@csrf_exempt
@action(methods=["GET", "POST"], detail=False, permission_classes=[])
def ueditor(self, request):
@ -144,16 +161,17 @@ class FileViewSet(CustomModelViewSet):
# 涂鸦功能上传处理
def save_scrawl_file(self, request, file_path, file_name):
import base64
instance = None
try:
content = request.data.get(ueditor_upload_settings.get("scrawlFieldName", "upfile"))
f = open(os.path.join(BASE_DIR, file_path, file_name), 'wb')
f.write(base64.b64decode(content))
f.close()
state = "SUCCESS"
FileList.save_file(request, file_path, file_name, mime_type='image/png')
instance = FileList.save_file(request, file_path, file_name, mime_type='image/png')
except Exception as e:
state = f"写入图片文件错误:{e}"
return state
return state, instance
def upload_file(self, request):
"""上传文件"""
@ -219,20 +237,21 @@ class FileViewSet(CustomModelViewSet):
# 取得输出文件的路径
format_file_name, output_path = self.get_output_path(path_format_var)
# 所有检测完成后写入文件
file_instance = None
if state == "SUCCESS":
if action == "uploadscrawl":
state = self.save_scrawl_file(request, file_path=output_path,
file_name=format_file_name)
state, file_instance = self.save_scrawl_file(request, file_path=output_path,
file_name=format_file_name)
else:
file = request.FILES.get(upload_field_name, None)
# 保存到文件中如果保存错误需要返回ERROR
state = self.save_upload_file(file, os.path.join(BASE_DIR, output_path, format_file_name))
# 保存到附件管理中
FileList.save_file(request, output_path, format_file_name, mime_type=file.content_type)
file_instance = FileList.save_file(request, output_path, format_file_name, mime_type=file.content_type)
# 返回数据
return_info = {
'url': os.path.join(output_path, format_file_name), # 保存后的文件名称
'url': file_instance.file_url if file_instance else os.path.join(output_path, format_file_name), # 保存后的文件名称
'original': upload_file_name, # 原始文件名
'type': upload_original_ext,
'state': state, # 上传状态成功时返回SUCCESS,其他任何值将原样返回至图片上传框中