#-*- coding:utf-8 -*-
from django.http import HttpResponse
from django.views.decorators.csrf import csrf_exempt
from django.conf import settings
from datetime import *
import os, codecs
import json
import random
import re
config_path = os.path.join(os.path.dirname(__file__), "ueconfig.json")
base_dir = settings.BASE_DIR #项目的根目录
#本地上传图片时构造json返回值
class JsonResult(object):
def __init__(self,state="未知错误",url="",title="",original="",error="null"):
super(JsonResult,self).__init__()
self.state = state
self.url = url
self.title = title
self.original = original
self.error = error
#构造返回json
def buildJsonResult(result):
jsondata = {"state":result.state,"url":result.url,"title":result.title,"original":result.original,"error":result.error}
return json.dumps(jsondata)
def buildFileName(pathformat, filename):
"""
PathFormat处理
"""
dt = datetime.now()
name,ext = os.path.splitext(filename)
#创建替换字典
keys = ['{filename}', '{time}', '{yyyy}', '{yy}', '{mm}', '{dd}', '{hh}', '{ii}', '{ss}', ]
values = [name, '%H%M%S', '%Y', '%y', '%m', '%d', '%H', '%M', '%S', ]
texts = dict(zip(keys, values))
#遍历对应替换
format_text = pathformat
"""
File "/home/yc/py6/myproject-test/ueditor/controller.py", line 45, in buildFileName
for key, value in texts.iteritems():
AttributeError: 'dict' object has no attribute 'iteritems'
"""
# for key, value in texts.iteritems():
for key, value in texts.items():
format_text = format_text.replace(key, value)
#处理随机数
regstr = r'{rand:(\d+?)}'
ms = re.search(regstr, format_text)
group = ms.group()
if group:
rand_length = int(ms.groups()[0]) #获取随机数字的长度
rand_number = random.randint(1, 10**rand_length -1) #生成随机数
rand_number = str(rand_number).zfill(rand_length) #不足位数补0
format_text = format_text.replace(group, rand_number)
return dt.strftime(format_text) + ext
#读取json文件
def getConfigContent():
# name 'file' is not defined
# jsonfile = file(config_path)
jsonfile = open(config_path)
content = json.load(jsonfile)
return content
#上传配置类
class UploadConfig(object):
def __init__(self,PathFormat,UploadFieldName,SizeLimit,AllowExtensions,SavePath,Base64,Base64Filename):
super(UploadConfig,self).__init__()
self.PathFormat = PathFormat
self.UploadFieldName = UploadFieldName
self.SizeLimit = SizeLimit
self.AllowExtensions = AllowExtensions
self.SavePath = SavePath
self.Base64 = Base64
self.Base64Filename = Base64Filename
#获取json配置中的某属性值
def GetConfigValue(key):
config = getConfigContent()
return config[key]
#检查文件扩展名是否在允许的扩展名内
def CheckFileType(filename,AllowExtensions):
exts = list(AllowExtensions)
name,ext = os.path.splitext(filename)
return ext in exts
def CheckFileSize(filesize,SizeLimit):
return filesize<SizeLimit
#处理上传图片、文件、视频文件
@csrf_exempt
def uploadFile(request,config):
result = JsonResult()
if config.Base64:
pass
else:
buf = request.FILES.get(config.UploadFieldName)
filename = buf.name
if not CheckFileType(filename,config.AllowExtensions):
result.error =u"不允许的文件格式"
return HttpResponse(buildJsonResult(result))
if not CheckFileSize(buf.size,config.SizeLimit):
result.error = u"文件大小超出服务器限制"
return HttpResponse(buildJsonResult(result))
# try:
truelyName = buildFileName(config.PathFormat, filename)
webUrl = config.SavePath+ truelyName
savePath = base_dir + webUrl
#判断文件夹是否存在,不存在则创建
folder, filename = os.path.split(savePath)
if not os.path.isdir(folder):
os.makedirs(folder)
print(base_dir,savePath)
f = codecs.open(savePath,"wb")
for chunk in buf.chunks():
f.write(chunk)
f.flush()
f.close()
add_watermark(savePath) #加水印
result.state = "SUCCESS"
result.url = truelyName
result.title = truelyName
result.original = truelyName
response = HttpResponse(buildJsonResult(result))
response["Content-Type"] = "text/plain"
return response
# except Exception as e:
# result.error = u"网络错误"
# return HttpResponse(buildJsonResult(result))
#加水印
def add_watermark(savePath):
try:
#判断是否是图片文件
if not os.path.splitext(savePath)[-1].lower() in ['.jpg', '.jpge', '.png', '.bmp']:
return
#获取配置
config = getConfigContent()
is_mark = config.get('openWaterMark', False) #是否开启加水印功能
watermark = config.get('waterMarkText', '') #水印内容
font = config.get('waterMarkFont', 'msyhbd.ttf') #字体
size = config.get('waterMarkSize', 15) #字体大小
bottom = config.get('waterMarkBottom', 45) #下边距
right = config.get('waterMarkRight', 155) #右边距
#判断是否开启了加水印功能
if not is_mark:
return
#python2.7 pillow
from PIL import Image, ImageDraw, ImageFont
#打开图片
im = Image.open(savePath).convert('RGBA')
#透明的图层,用于写文本
text_layer = Image.new('RGBA', im.size, (0,0,0,0))
draw = ImageDraw.Draw(text_layer)
#加载字体,设置大小
font_path = os.path.join(os.path.dirname(__file__), font)
fnt = ImageFont.truetype(font_path, size) #要加中文字体才能识别中文
point = (text_layer.size[0]-right, text_layer.size[1]-bottom) #位置
draw.text(point, watermark, font=fnt, fill=(0,255,0,255))
# draw.text(point, watermark, font=fnt, fill=(255,255,255,255))
#out=Image.alpha_composite(im, text_layer)
out = Image.composite(text_layer, im, text_layer)
out.save(savePath)
out.close()
except Exception as e:
print('[error]', e.message)
#处理在线图片与在线文件
#返回的数据格式:{"state":"SUCCESS","list":[{"url":"upload/image/20140627/6353948647502438222009315.png"},{"url":"upload/image/20140627/6353948659383617789875352.png"},{"url":"upload/image/20140701/6353980733328090063690725.png"},{"url":"upload/image/20140701/6353980745691597223366891.png"},{"url":"upload/image/20140701/6353980747586705613811538.png"},{"url":"upload/image/20140701/6353980823509548151892908.png"}],"start":0,"size":20,"total":6}
def listFileManage(request,imageManagerListPath,imageManagerAllowFiles,listsize):
pstart = request.GET.get("start")
start = pstart==None and int(pstart) or 0
psize = request.GET.get("size")
size = psize==None and int(GetConfigValue(listsize)) or int(psize)
localPath = base_dir + imageManagerListPath
#2016-10-31自动创建在线管理的文件夹
if not os.path.isdir(localPath):
os.makedirs(localPath)
filelist = []
exts = list(imageManagerAllowFiles)
index = start
for imagename in os.listdir(localPath):
name,ext = os.path.splitext(imagename)
if ext in exts:
filelist.append(dict(url=imagename))
index+=1
if index-start>=size:
break
jsondata = {"state":"SUCCESS","list":filelist,"start":start,"size":size,"total":index}
return HttpResponse(json.dumps(jsondata))
#返回配置信息
def configHandler(request):
content = getConfigContent()
callback = request.GET.get("callback")
if callback:
return HttpResponse("{
没有合适的资源?快使用搜索试试~ 我知道了~
基于spark的豆瓣阅读分析与推荐系统.zip
共744个文件
jpg:180个
png:151个
js:104个
需积分: 10 2 下载量 190 浏览量
2022-07-19
14:16:28
上传
评论 2
收藏 94.1MB ZIP 举报
温馨提示
基于spark的豆瓣阅读分析与推荐系统. ,本资源适合新手小白和在校学生,使用前请务必查看说明文档
资源详情
资源评论
资源推荐
收起资源包目录
基于spark的豆瓣阅读分析与推荐系统.zip (744个子文件)
vendor.css 267KB
amazeui.min.css 249KB
style.css 136KB
style.css 119KB
bootstrap.min.css 108KB
style.min.css 89KB
animate.css 66KB
app.css 47KB
ueditor.css 43KB
ueditor.min.css 34KB
responsive.css 27KB
font-awesome.min.css 27KB
video-js.css 21KB
image.css 18KB
fullcalendar.min.css 15KB
video.css 15KB
attachment.css 14KB
video-js.min.css 11KB
chosen.min.css 11KB
amazeui.datatables.min.css 9KB
flexslider.css 7KB
shCoreDefault.css 7KB
fullcalendar.print.css 5KB
admin.css 5KB
scrawl.css 4KB
owl.carousel.css 3KB
codemirror.css 3KB
meanmenu.min.css 3KB
charts.css 3KB
background.css 2KB
emotion.css 2KB
dialogbase.css 2KB
music.css 2KB
edittable.css 1KB
template.css 1KB
ImgUpRead.css 552B
webuploader.css 515B
help.css 389B
iframe.css 41B
fontawesome-webfont.eot 162KB
fontawesome-webfont.eot 75KB
Linearicons-Free.eot 55KB
glyphicons-halflings-regular.eot 20KB
vjs.eot 3KB
UEditorSnapscreen.exe 508KB
wface.gif 49KB
jxface2.gif 40KB
yface.gif 28KB
bface.gif 27KB
icons.gif 20KB
file-icons.gif 20KB
file-icons.gif 20KB
tface.gif 19KB
fface.gif 18KB
cface.gif 8KB
icons-all.gif 4KB
videologo.gif 2KB
cancelbutton.gif 1KB
button-bg.gif 1KB
lock.gif 1KB
alignicon.gif 1KB
word.gif 1019B
icon_doc.gif 1012B
icon_psd.gif 1009B
icon_rar.gif 1007B
icon_xls.gif 1005B
icon_ppt.gif 1001B
icon_mv.gif 1001B
icon_pdf.gif 996B
icon_mp3.gif 986B
icon_txt.gif 970B
icon_jpg.gif 950B
icon_exe.gif 949B
icon_chm.gif 923B
loading.gif 734B
icons.gif 453B
icons.gif 453B
icons.gif 453B
success.gif 445B
success.gif 445B
success.gif 445B
cursor_v.gif 370B
cursor_h.gif 253B
anchor.gif 184B
highlighted.gif 111B
unhighlighted.gif 111B
bg.gif 84B
pagebreak.gif 54B
spacer.gif 43B
0.gif 43B
.gitignore 38B
my-account.html 31KB
bookdetails.html 20KB
base.html 18KB
list.html 18KB
index.html 17KB
orderitem.html 17KB
index.html 12KB
base.html 12KB
confirm.html 11KB
共 744 条
- 1
- 2
- 3
- 4
- 5
- 6
- 8
大数据程序定制
- 粉丝: 142
- 资源: 48
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- OpenFOAM 二维圆柱体周围的涡流脱落
- Unity有哪些流行的游戏案例?
- 708946149858210笔记.zip
- Segment Anything Model 2(SAM 2)分割大模型预训练权重sam2.1-hiera-tiny.pt
- java行为分析系统源码带本地搭建教程数据库 MySQL源码类型 WebForm
- 【java毕业设计】游戏交易系统源码(springboot+vue+mysql+说明文档+LW).zip
- Django开发的Redis管理平台Repoll,旨在提高企业对redis实例平台化管理,助力运维转型
- 【java毕业设计】疫情信息管理系统源码(springboot+vue+mysql+说明文档+LW).zip
- 13章完结Electron+Vue3+AI+云存储-实战跨平台桌面应用
- SAM2(Segment Anything2)预训练权重sam2.1-hiera-base-plus.pt
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0