mirror of
https://github.com/QingdaoU/OnlineJudge.git
synced 2025-01-16 01:13:47 +00:00
增加文件上传功能
This commit is contained in:
parent
7df98245e4
commit
c192304fd8
@ -131,6 +131,10 @@ class ImageUploadForm(forms.Form):
|
|||||||
image = forms.FileField()
|
image = forms.FileField()
|
||||||
|
|
||||||
|
|
||||||
|
class FileUploadForm(forms.Form):
|
||||||
|
file = forms.FileField()
|
||||||
|
|
||||||
|
|
||||||
class RankInfoSerializer(serializers.ModelSerializer):
|
class RankInfoSerializer(serializers.ModelSerializer):
|
||||||
user = UsernameSerializer()
|
user = UsernameSerializer()
|
||||||
|
|
||||||
|
@ -1,7 +1,8 @@
|
|||||||
from django.conf.urls import url
|
from django.conf.urls import url
|
||||||
|
|
||||||
from .views import SimditorImageUploadAPIView
|
from .views import SimditorImageUploadAPIView, SimditorFileUploadAPIView
|
||||||
|
|
||||||
urlpatterns = [
|
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
|
import os
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from account.serializers import ImageUploadForm
|
from account.serializers import ImageUploadForm, FileUploadForm
|
||||||
from utils.shortcuts import rand_str
|
from utils.shortcuts import rand_str
|
||||||
from utils.api import CSRFExemptAPIView
|
from utils.api import CSRFExemptAPIView
|
||||||
import logging
|
import logging
|
||||||
@ -35,10 +35,41 @@ class SimditorImageUploadAPIView(CSRFExemptAPIView):
|
|||||||
except IOError as e:
|
except IOError as e:
|
||||||
logger.error(e)
|
logger.error(e)
|
||||||
return self.response({
|
return self.response({
|
||||||
"success": True,
|
"success": False,
|
||||||
"msg": "Upload Error",
|
"msg": "Upload Error",
|
||||||
"file_path": f"{settings.UPLOAD_PREFIX}/{img_name}"})
|
"file_path": ""})
|
||||||
return self.response({
|
return self.response({
|
||||||
"success": True,
|
"success": True,
|
||||||
"msg": "Success",
|
"msg": "Success",
|
||||||
"file_path": f"{settings.UPLOAD_PREFIX}/{img_name}"})
|
"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
|
return attrs
|
||||||
|
|
||||||
def _true_url(self, url):
|
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):
|
if prog.match(url):
|
||||||
return url
|
return url
|
||||||
else:
|
else:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user