Skip to content

Commit 3e80f9e

Browse files
authored
Merge pull request #185 from lenoctambule/master
Added file format check and option to require authentication to upload files
2 parents cebeec9 + 6415e1b commit 3e80f9e

5 files changed

Lines changed: 24 additions & 2 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ MDEDITOR_CONFIGS = {
196196
                "help", "info",
197197
                "||", "preview", "watch", "fullscreen"], # custom edit box toolbar
198198
    'upload_image_formats': ["jpg", "jpeg", "gif", "png", "bmp", "webp"], # image upload format type
199+
'upload_require_auth' : False, # image upload authentication requirement
199200
    'image_folder': 'editor', # image save the folder name
200201
    'theme': 'default', # edit box theme, dark / default
201202
    'preview_theme': 'default', # Preview area theme, dark / default

README_CN.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,8 @@ MDEDITOR_CONFIGS = {
201201
"emoji", "html-entities", "pagebreak", "goto-line", "|",
202202
"help", "info",
203203
"||", "preview", "watch", "fullscreen"], # 自定义编辑框工具栏
204-
'upload_image_formats': ["jpg", "jpeg", "gif", "png", "bmp", "webp"], # 图片上传格式类型
204+
'upload_image_formats': ["jpg", "jpeg", "gif", "png", "bmp", "webp"], # 图片上传格式类型,
205+
'upload_require_auth' : False, # TODO: Translation needed here
205206
'image_folder': 'editor', # 图片保存文件夹名称
206207
'theme': 'default', # 编辑框主题 ,dark / default
207208
'preview_theme': 'default', # 预览区域主题, dark / default

mdeditor/configs.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
'upload_image_formats': ["jpg", "JPG", "jpeg", "JPEG", "gif", "GIF", "png",
1818
"PNG", "bmp", "BMP", "webp", "WEBP"],
1919
'upload_image_url': '/mdeditor/uploads/',
20+
'upload_require_auth' : False,
2021
'image_folder': 'editor',
2122
'theme': 'default', # dark / default
2223
'preview_theme': 'default', # dark / default

mdeditor/views.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@
88
from django.views.decorators.csrf import csrf_exempt
99
from django.utils.decorators import method_decorator
1010
from .configs import MDConfig
11+
from PIL import Image
1112

1213
# TODO 此处获取default配置,当用户设置了其他配置时,此处无效,需要进一步完善
1314
MDEDITOR_CONFIGS = MDConfig('default')
1415

15-
1616
class UploadView(generic.View):
1717
""" upload image file """
1818

@@ -23,6 +23,15 @@ def dispatch(self, *args, **kwargs):
2323
def post(self, request, *args, **kwargs):
2424
upload_image = request.FILES.get("editormd-image-file", None)
2525
media_root = settings.MEDIA_ROOT
26+
upload_require_auth = MDEDITOR_CONFIGS.get('upload_require_auth', False)
27+
28+
# Check if user is authenticated if it is required
29+
if upload_require_auth and not request.user.is_authenticated :
30+
return JsonResponse({
31+
'success' : 0,
32+
'message': "Authentication required.",
33+
'url': ""
34+
})
2635

2736
# image none check
2837
if not upload_image:
@@ -44,6 +53,15 @@ def post(self, request, *args, **kwargs):
4453
'url': ""
4554
})
4655

56+
try :
57+
Image.open(upload_image)
58+
except :
59+
return JsonResponse({
60+
'success': 0,
61+
'message': "File format not recognized.",
62+
'url': ""
63+
})
64+
4765
# image floder check
4866
file_path = os.path.join(media_root, MDEDITOR_CONFIGS['image_folder'])
4967
if not os.path.exists(file_path):

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ asgiref==3.5.0
22
Django==4.0.3
33
mistune==2.0.3
44
sqlparse==0.4.2
5+
Pillow==9.3.0

0 commit comments

Comments
 (0)