mirror of
https://github.com/QingdaoU/OnlineJudge.git
synced 2024-12-29 08:32:08 +00:00
b86ebf0ed7
支持图片上传
45 lines
1.5 KiB
Python
45 lines
1.5 KiB
Python
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}"})
|