题目AC后不计入AC计数器;
支持图片上传
This commit is contained in:
7
utils/urls.py
Normal file
7
utils/urls.py
Normal file
@@ -0,0 +1,7 @@
|
||||
from django.conf.urls import url
|
||||
|
||||
from .views import SimditorImageUploadAPIView
|
||||
|
||||
urlpatterns = [
|
||||
url(r"^upload_image/?$", SimditorImageUploadAPIView.as_view(), name="upload_image")
|
||||
]
|
||||
44
utils/views.py
Normal file
44
utils/views.py
Normal file
@@ -0,0 +1,44 @@
|
||||
import os
|
||||
from django.conf import settings
|
||||
from account.serializers import ImageUploadForm
|
||||
from utils.shortcuts import rand_str
|
||||
from utils.api import CSRFExemptAPIView
|
||||
import logging
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class SimditorImageUploadAPIView(CSRFExemptAPIView):
|
||||
request_parsers = ()
|
||||
|
||||
def post(self, request):
|
||||
form = ImageUploadForm(request.POST, request.FILES)
|
||||
if form.is_valid():
|
||||
img = form.cleaned_data["image"]
|
||||
else:
|
||||
return self.response({
|
||||
"success": False,
|
||||
"msg": "Upload failed",
|
||||
"file_path": ""})
|
||||
|
||||
suffix = os.path.splitext(img.name)[-1].lower()
|
||||
if suffix not in [".gif", ".jpg", ".jpeg", ".bmp", ".png"]:
|
||||
return self.response({
|
||||
"success": False,
|
||||
"msg": "Unsupported file format",
|
||||
"file_path": ""})
|
||||
img_name = rand_str(10) + suffix
|
||||
try:
|
||||
with open(os.path.join(settings.UPLOAD_DIR, img_name), "wb") as imgFile:
|
||||
for chunk in img:
|
||||
imgFile.write(chunk)
|
||||
except IOError as e:
|
||||
logger.error(e)
|
||||
return self.response({
|
||||
"success": True,
|
||||
"msg": "Upload Error",
|
||||
"file_path": f"{settings.UPLOAD_PREFIX}/{img_name}"})
|
||||
return self.response({
|
||||
"success": True,
|
||||
"msg": "Success",
|
||||
"file_path": f"{settings.UPLOAD_PREFIX}/{img_name}"})
|
||||
Reference in New Issue
Block a user