Merge pull request #22 from liangliangyy/dev

为上传的图片文件自动压缩
sh_branch
车亮亮 9 years ago committed by GitHub
commit f280d3f5b9

@ -130,14 +130,14 @@ def load_sidebar(user):
show_adsense = settings.SHOW_GOOGLE_ADSENSE
# 标签云 计算字体大小
# 根据总数计算出平均值 大小为 (数目/平均值)*步长
increment = 10
increment = 5
tags = Tag.objects.all()
sidebar_tags = None
if tags:
s = list(map(lambda t: (t, t.get_article_count()), tags))
count = sum(map(lambda t: t[1], s))
dd = count / len(tags)
sidebar_tags = list(map(lambda x: (x[0], x[1], (x[1] / dd) * increment), s))
sidebar_tags = list(map(lambda x: (x[0], x[1], (x[1] / dd) * increment + 10), s))
return {
'recent_articles': recent_articles,

@ -209,19 +209,29 @@ class TagDetailView(ArticleListView):
@csrf_exempt
def fileupload(request):
if request.method == 'POST':
fname = ''
timestr = datetime.datetime.now().strftime('%Y/%m/%d')
basepath = os.path.join(r'/var/www/resource/image/', timestr)
if not os.path.exists(basepath):
os.makedirs(basepath)
fname = ''
response = []
for filename in request.FILES:
fname = filename
timestr = datetime.datetime.now().strftime('%Y/%m/%d')
imgextensions = ['jpg', 'png', 'jpeg', 'bmp']
fname = u''.join(str(filename))
isimage = len([i for i in imgextensions if fname.find(i) >= 0]) > 0
basepath = os.path.join(r'/var/www/resource/' + 'files' if not isimage else'image' + '/',
timestr)
url = 'https://resource.lylinux.net/{type}/{timestr}/{filename}'.format(
type='files' if not isimage else'image', timestr=timestr, filename=filename)
if not os.path.exists(basepath):
os.makedirs(basepath)
savepath = os.path.join(basepath, filename)
with open(savepath, 'wb+') as wfile:
for chunk in request.FILES[filename].chunks():
wfile.write(chunk)
return HttpResponse('https://resource.lylinux.net/' + 'image/' + timestr + '/' + fname)
if isimage:
from PIL import Image
image = Image.open(savepath)
image.save(savepath, quality=20, optimize=True)
response.append(url)
return HttpResponse(response)
else:
return HttpResponse("only for post")

@ -4,7 +4,7 @@ django-appconf==1.0.2
django-autoslug==1.9.3
django-compressor==2.1.1
django-debug-toolbar==1.8
django-haystack==2.6.0
django-haystack==2.6.1
django-ipware==1.1.6
django-pagedown==0.1.3
django-uuslug==1.1.8

@ -36,15 +36,12 @@
{% comment %}<script src="https://cdn.rawgit.com/google/code-prettify/master/loader/run_prettify.js"></script>{% endcomment %}
<!--[if lt IE 9]>
<link rel='stylesheet' id='twentytwelve-ie-css' href='{% static 'blog/css/ie.css' %}' type='text/css' media='all' />
<![endif]-->
<![endif]-->
<link rel="stylesheet" href="{% static 'pygments/default.css' %}"/>
<link rel="stylesheet" href="{% static 'blog/css/nprogress.css' %}">
{% block compress_css %}
{% endblock %}
{% endcompress %}
{% comment %}<script type='text/javascript' src='{% static 'blog/js/jquery.js' %}' defer='defer'></script>
<script type='text/javascript' src='{% static 'blog/js/jquery-migrate.min.js' %}' defer='defer'></script>{% endcomment %}
</head>
<body class="home blog custom-font-enabled">
@ -74,23 +71,9 @@
</div><!-- #main .wrapper -->
{% include 'share_layout/footer.html' %}
</div><!-- #page -->
{% comment %}
<div id="wp-auto-top">
<div id="wp-auto-top-top"></div>
<div id="wp-auto-top-bottom"></div>
</div>
<script> var commentPositionId = '.ds-comments:last';
var wpAutoTopSpeed = 1;</script>
<div id="su-footer-links" style="text-align: center;"></div>
{% endcomment %}
{% comment %}<script type="application/javascript" src="{% static 'blog/js/navigation.js' %}" defer="defer"></script>{% endcomment %}
</body>
<footer>
{% comment %}<link href="{% static 'highlight/styles/default.css' %}" rel="stylesheet">
<script type="application/javascript" src="{% static 'highlight/highlight.pack.js' %}"></script>
<script>hljs.initHighlightingOnLoad();</script>{% endcomment %}
{% compress js %}
<script type="text/javascript" src="{% static 'blog/js/jquery-3.1.1.js' %}"></script>
<script type="text/javascript" src="{% static 'blog/js/nprogress.js' %}"></script>

Loading…
Cancel
Save