优化图片上传路径

This commit is contained in:
yangjian 2021-02-06 22:44:08 +08:00
parent e4634195bd
commit 5a79dcbe4c

View File

@ -4,7 +4,7 @@ from django.conf import settings
from django.views.decorators.csrf import csrf_exempt
from django.contrib.auth.decorators import login_required # 登录需求装饰器
import datetime,time,json,base64,os,uuid
from app_doc.models import Image,ImageGroup
from app_doc.models import Image,ImageGroup,Attachment
from app_admin.models import SysSetting
import requests
import random
@ -44,6 +44,7 @@ def upload_ice_img(request):
res_dic = {0:result,"length":1,'other_msg':iceEditor_img} #一个文件,直接把文件数量固定了
return HttpResponse(json.dumps(res_dic), content_type="application/json")
def ice_save_file(file_obj,user):
# 默认保留支持ice单文件上传功能可以iceEditor中开启
file_suffix = str(file_obj).split(".")[-1] # 提取图片格式
@ -66,7 +67,7 @@ def ice_save_file(file_obj,user):
path_file = relative_path + file_name
path_file = settings.MEDIA_ROOT + path_file
#file_Url 是文件的url下发路径
file_url = settings.MEDIA_URL + relative_path + file_name
file_url = (settings.MEDIA_URL + relative_path + file_name).replace("//","/")
with open(path_file, 'wb') as f:
for chunk in file_obj.chunks():
@ -80,7 +81,7 @@ def ice_save_file(file_obj,user):
)
else :
#文件上传暂时不屏蔽如果需要正常使用此功能是需要在iceeditor中修改的mrdoc使用的是自定义脚本上传
#文件上传暂时不屏蔽如果需要正常使用此功能是需要在iceeditor中修改的mrdoc使用的是自定义脚本上传
Attachment.objects.create(
user=user,
file_path=file_url,
@ -88,8 +89,10 @@ def ice_save_file(file_obj,user):
file_size=str(round(len(chunk)/1024,2))+"KB"
)
return {"error":0, "name": str(file_obj),'url':file_url}
return {"error": "文件存储异常"}
# ice_url图片上传
def ice_url_img_upload(url,user):
@ -101,7 +104,7 @@ def ice_url_img_upload(url,user):
path_file = os.path.join(relative_path, file_name)
path_file = settings.MEDIA_ROOT + path_file
# print('文件路径:', path_file)
file_url = settings.MEDIA_URL + relative_path + file_name
file_url = (settings.MEDIA_URL + relative_path + file_name).replace("//","/")
# print("文件URL", file_url)
header = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36"
@ -121,7 +124,6 @@ def ice_url_img_upload(url,user):
return resp_data
@login_required()
@csrf_exempt
def upload_img(request):
@ -202,7 +204,7 @@ def img_upload(files, dir_name, user, group_id=None):
path_file=os.path.join(relative_path, file_name)
path_file = settings.MEDIA_ROOT + path_file
# print('文件路径:',path_file)
file_url = settings.MEDIA_URL + relative_path + file_name
file_url = (settings.MEDIA_URL + relative_path + file_name).replace("//",'/')
# print("文件URL",file_url)
with open(path_file, 'wb') as f:
for chunk in files.chunks():
@ -226,7 +228,7 @@ def base_img_upload(files,dir_name, user):
path_file = os.path.join(relative_path, file_name)
path_file = settings.MEDIA_ROOT + path_file
# print('文件路径:', path_file)
file_url = settings.MEDIA_URL + relative_path + file_name
file_url = (settings.MEDIA_URL + relative_path + file_name).replace("//","/")
# print("文件URL", file_url)
with open(path_file, 'wb') as f:
f.write(files_base) # 保存文件
@ -247,7 +249,7 @@ def url_img_upload(url,dir_name,user):
path_file = os.path.join(relative_path, file_name)
path_file = settings.MEDIA_ROOT + path_file
# print('文件路径:', path_file)
file_url = settings.MEDIA_URL + relative_path + file_name
file_url = (settings.MEDIA_URL + relative_path + file_name).replace("//","/")
# print("文件URL", file_url)
header = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36"