diff --git a/simditor/urls.py b/simditor/urls.py index 8fc7887..ce5eaab 100644 --- a/simditor/urls.py +++ b/simditor/urls.py @@ -11,16 +11,16 @@ if django.VERSION >= (2, 0): # pylint disable=C0103 - from django.urls import path + from django.urls import re_path urlpatterns = [ - path('upload/', staff_member_required(views.UPLOAD), + re_path('upload[/]{0,1}', staff_member_required(views.UPLOAD), name='simditor_upload'), ] elif django.VERSION >= (1, 8): from django.conf.urls import url # pylint disable=C0103 urlpatterns = [ - url(r'^upload/', staff_member_required(views.UPLOAD), + url(r'^upload[/]{0,1}', staff_member_required(views.UPLOAD), name='simditor_upload'), ] else: @@ -28,7 +28,7 @@ # pylint disable=C0103 urlpatterns = patterns( '', - url(r'^upload/', staff_member_required(views.UPLOAD), + url(r'^upload[/]{0,1}', staff_member_required(views.UPLOAD), name='simditor_upload'), ) diff --git a/simditor/views.py b/simditor/views.py index c78b734..65a6a5f 100644 --- a/simditor/views.py +++ b/simditor/views.py @@ -58,15 +58,19 @@ def upload_handler(request): 'msg': '图片格式错误!'} return JsonResponse(retdata) - filename = get_upload_filename(uploaded_file.name) - saved_path = default_storage.save(filename, uploaded_file) + if hasattr(settings, 'SIMDITOR_UPLOAD_BACKEND') and hasattr(settings.SIMDITOR_UPLOAD_BACKEND, '__call__'): + retdata = settings.SIMDITOR_UPLOAD_BACKEND(request, uploaded_file) or {} - url = utils.get_media_url(saved_path) + else: + filename = get_upload_filename(uploaded_file.name) + saved_path = default_storage.save(filename, uploaded_file) - is_api = settings.SIMDITOR_CONFIGS.get('is_api', False) - url = request.META.get('HTTP_ORIGIN') + url if is_api else url + url = utils.get_media_url(saved_path) - retdata = {'file_path': url, 'success': True, 'msg': '上传成功!'} + is_api = settings.SIMDITOR_CONFIGS.get('is_api', False) + url = request.META.get('HTTP_ORIGIN') + url if is_api else url + + retdata = {'file_path': url, 'success': True, 'msg': '上传成功!'} return JsonResponse(retdata)