@@ -240,9 +240,11 @@ def DJANGO_FILE_UPLOAD_STORAGE_FUNC(request, file_path=None): |
||
240 | 240 |
def DJANGO_FILE_UPLOAD_CALLBACK_FUNC(request, file_path=None, file_url=None): |
241 | 241 |
""" DJANGO FILE UPLOAD Callback Func """ |
242 | 242 |
import os |
243 |
+ |
|
243 | 244 |
from logs.models import MchLogInfo |
244 | 245 |
file_type = request.POST.get('file_type', '') |
245 | 246 |
upload_qiniu = request.POST.get('upload_qiniu', '') |
247 |
+ compress = bool(request.POST.get('compress', 0)) |
|
246 | 248 |
|
247 | 249 |
if file_type == 'logfile': |
248 | 250 |
optor_id = request.POST.get('optor_id', '') |
@@ -259,7 +261,7 @@ def DJANGO_FILE_UPLOAD_CALLBACK_FUNC(request, file_path=None, file_url=None): |
||
259 | 261 |
height = request.POST.get('height', 0) |
260 | 262 |
file = request.FILES.get('file', '') |
261 | 263 |
# upload_file_path('media/' + file_path, file_path, bucket='tamron') |
262 |
- upload_file_req(file, file_path, bucket='tamron') |
|
264 |
+ upload_file_req(file, file_path, bucket='tamron', compress=compress) |
|
263 | 265 |
file_url = qiniu_file_url(file_path, bucket='tamron') |
264 | 266 |
|
265 | 267 |
return { |
@@ -271,7 +273,7 @@ def DJANGO_FILE_UPLOAD_CALLBACK_FUNC(request, file_path=None, file_url=None): |
||
271 | 273 |
elif file_type == 'member_activity' or (file_type and upload_qiniu): |
272 | 274 |
file = request.FILES.get('file', '') |
273 | 275 |
# upload_file_path('media/' + file_path, file_path, bucket='tamron') |
274 |
- upload_file_req(file, file_path, bucket='tamron') |
|
276 |
+ upload_file_req(file, file_path, bucket='tamron', compress=compress) |
|
275 | 277 |
file_url = qiniu_file_url(file_path, bucket='tamron') |
276 | 278 |
|
277 | 279 |
return { |
@@ -270,6 +270,9 @@ QINIU_FILE_URL_AFTER = 'http://img.tamron.kodo.com.cn' # QINIU_FILE_URL_HTTPS F |
||
270 | 270 |
FILE_UPLOAD_MAX_MEMORY_SIZE = 5242880 # InMemoryUploadedFile 文件最大值 |
271 | 271 |
FILE_UPLOAD_PERMISSIONS = 0o644 # TemporaryUploadedFile 文件权限设置 |
272 | 272 |
|
273 |
+# 图片压缩设置 |
|
274 |
+PNG_QUANT_FILE = '/home/paiai/work/pngquant-2.7.2/pngquant' |
|
275 |
+ |
|
273 | 276 |
# 唯一标识设置 |
274 | 277 |
CURTAIL_UUID_LENGTH = 7 # Used in django-curtail-uuid==1.0.0 |
275 | 278 |
|
@@ -8,6 +8,7 @@ jsonfield==3.1.0 |
||
8 | 8 |
mock==4.0.3 |
9 | 9 |
monetary==1.0.3 |
10 | 10 |
mysqlclient==2.1.1 |
11 |
+pngquant==1.0.7 |
|
11 | 12 |
pysnippets==1.1.4 |
12 | 13 |
pyzbar==0.1.9 |
13 | 14 |
qiniu==7.9.0 |
@@ -1,39 +1,46 @@ |
||
1 | 1 |
# -*- coding: utf-8 -*- |
2 | 2 |
|
3 |
+import pngquant |
|
3 | 4 |
import qiniu |
4 | 5 |
from django.conf import settings |
5 | 6 |
|
6 | 7 |
|
7 | 8 |
QINIU = settings.QINIU |
8 | 9 |
auth = qiniu.Auth(QINIU['access_key'], QINIU['secret_key']) |
10 |
+pngquant.config(settings.PNG_QUANT_FILE) |
|
9 | 11 |
|
10 | 12 |
|
11 |
-def upload(data, key=None, mime_type='application/octet-stream', bucket=QINIU['bucket_default']): |
|
13 |
+def upload(data, key=None, mime_type='application/octet-stream', bucket=QINIU['bucket_default'], compress=True): |
|
12 | 14 |
if not data: |
13 | 15 |
return '' |
16 |
+ if compress: |
|
17 |
+ try: |
|
18 |
+ data = pngquant.quant_data(data)[1] |
|
19 |
+ except Exception as e: |
|
20 |
+ pass |
|
14 | 21 |
token = auth.upload_token(bucket, key=key) |
15 | 22 |
ret, _ = qiniu.put_data(token, key, data, mime_type=mime_type) |
16 | 23 |
return ret.get('key') |
17 | 24 |
|
18 | 25 |
|
19 |
-def upload_file_admin(obj, key=None, mime_type='application/octet-stream', bucket=QINIU['bucket_default']): |
|
26 |
+def upload_file_admin(obj, key=None, mime_type='application/octet-stream', bucket=QINIU['bucket_default'], compress=True): |
|
20 | 27 |
# Django Admin Upload |
21 | 28 |
if not obj.image: |
22 | 29 |
return '' |
23 | 30 |
obj.image.seek(0) |
24 |
- return upload(obj.image.read(), key=key or obj.image.name, mime_type=mime_type, bucket=bucket) |
|
31 |
+ return upload(obj.image.read(), key=key or obj.image.name, mime_type=mime_type, bucket=bucket, compress=compress) |
|
25 | 32 |
|
26 | 33 |
|
27 |
-def upload_file_req(file, key=None, mime_type='application/octet-stream', bucket=QINIU['bucket_default']): |
|
34 |
+def upload_file_req(file, key=None, mime_type='application/octet-stream', bucket=QINIU['bucket_default'], compress=True): |
|
28 | 35 |
# photo = request.FILES.get('photo', '') |
29 | 36 |
# <InMemoryUploadedFile: photo.png (image/png)> |
30 | 37 |
if not file: |
31 | 38 |
return '' |
32 | 39 |
file.seek(0) |
33 |
- return upload(file.read(), key=key or file.name, mime_type=mime_type, bucket=bucket) |
|
40 |
+ return upload(file.read(), key=key or file.name, mime_type=mime_type, bucket=bucket, compress=compress) |
|
34 | 41 |
|
35 | 42 |
|
36 |
-def upload_file_path(path, key=None, mime_type='application/octet-stream', bucket=QINIU['bucket_default']): |
|
43 |
+def upload_file_path(path, key=None, mime_type='application/octet-stream', bucket=QINIU['bucket_default'], compress=True): |
|
37 | 44 |
if not path: |
38 | 45 |
return '' |
39 | 46 |
token = auth.upload_token(bucket, key=key) |