增加文件上传功能

This commit is contained in:
virusdefender 2019-03-12 14:40:47 +08:00
parent 7df98245e4
commit c192304fd8
4 changed files with 42 additions and 6 deletions

View File

@ -131,6 +131,10 @@ class ImageUploadForm(forms.Form):
image = forms.FileField()
class FileUploadForm(forms.Form):
file = forms.FileField()
class RankInfoSerializer(serializers.ModelSerializer):
user = UsernameSerializer()

View File

@ -1,7 +1,8 @@
from django.conf.urls import url
from .views import SimditorImageUploadAPIView
from .views import SimditorImageUploadAPIView, SimditorFileUploadAPIView
urlpatterns = [
url(r"^upload_image/?$", SimditorImageUploadAPIView.as_view(), name="upload_image")
url(r"^upload_image/?$", SimditorImageUploadAPIView.as_view(), name="upload_image"),
url(r"^upload_file/?$", SimditorFileUploadAPIView.as_view(), name="upload_file")
]

View File

@ -1,6 +1,6 @@
import os
from django.conf import settings
from account.serializers import ImageUploadForm
from account.serializers import ImageUploadForm, FileUploadForm
from utils.shortcuts import rand_str
from utils.api import CSRFExemptAPIView
import logging
@ -35,10 +35,41 @@ class SimditorImageUploadAPIView(CSRFExemptAPIView):
except IOError as e:
logger.error(e)
return self.response({
"success": True,
"success": False,
"msg": "Upload Error",
"file_path": f"{settings.UPLOAD_PREFIX}/{img_name}"})
"file_path": ""})
return self.response({
"success": True,
"msg": "Success",
"file_path": f"{settings.UPLOAD_PREFIX}/{img_name}"})
class SimditorFileUploadAPIView(CSRFExemptAPIView):
request_parsers = ()
def post(self, request):
form = FileUploadForm(request.POST, request.FILES)
if form.is_valid():
file = form.cleaned_data["file"]
else:
return self.response({
"success": False,
"msg": "Upload failed"
})
suffix = os.path.splitext(file.name)[-1].lower()
file_name = rand_str(10) + suffix
try:
with open(os.path.join(settings.UPLOAD_DIR, file_name), "wb") as f:
for chunk in file:
f.write(chunk)
except IOError as e:
logger.error(e)
return self.response({
"success": False,
"msg": "Upload Error"})
return self.response({
"success": True,
"msg": "Success",
"file_path": f"{settings.UPLOAD_PREFIX}/{file_name}",
"file_name": file.name})

View File

@ -142,7 +142,7 @@ class XSSHtml(HTMLParser):
return attrs
def _true_url(self, url):
prog = re.compile(r"^(http|https|ftp)://.+", re.I | re.S)
prog = re.compile(r"(^(http|https|ftp)://.+)|(^/)", re.I | re.S)
if prog.match(url):
return url
else: