mirror of
https://github.com/QingdaoU/OnlineJudge.git
synced 2024-12-28 16:12:13 +00:00
增加文件上传功能
This commit is contained in:
parent
7df98245e4
commit
c192304fd8
@ -131,6 +131,10 @@ class ImageUploadForm(forms.Form):
|
||||
image = forms.FileField()
|
||||
|
||||
|
||||
class FileUploadForm(forms.Form):
|
||||
file = forms.FileField()
|
||||
|
||||
|
||||
class RankInfoSerializer(serializers.ModelSerializer):
|
||||
user = UsernameSerializer()
|
||||
|
||||
|
@ -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")
|
||||
]
|
||||
|
@ -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})
|
@ -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:
|
||||
|
Loading…
Reference in New Issue
Block a user