No Description

qiniucdn.py 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. # -*- coding: utf-8 -*-
  2. import re
  3. import pngquant
  4. import qiniu
  5. from django.conf import settings
  6. from django_file_md5 import calculate_data_md5
  7. from utils.thumbnail_utils import make_thumbnail2
  8. QINIU = settings.QINIU
  9. auth = qiniu.Auth(QINIU['access_key'], QINIU['secret_key'])
  10. pngquant.config(settings.PNG_QUANT_FILE)
  11. def generate_new_key(key, data):
  12. # key = re.sub(r"(.+/)(.+)\.(.+)", lambda m: '{}{}.{}'.format(m.group(1), calculate_data_md5(data), m.group(3)), key)
  13. sections = re.split(r'/|\.', key)
  14. base_path, ext = '/'.join(sections[:-2]), sections[-1]
  15. return '{}{}{}.{}'.format(base_path, base_path and '/', calculate_data_md5(data), ext)
  16. def upload(data, key=None, mime_type='application/octet-stream', bucket=QINIU['bucket_default'], compress=True, thumbnail=True, thumbnailw=1080, thumbnailh=720):
  17. if not data:
  18. return ''
  19. if thumbnail:
  20. w, h = thumbnailw or 1080, thumbnailh or 720
  21. data = make_thumbnail2(data, w=w, h=h)
  22. if compress:
  23. try:
  24. data = pngquant.quant_data(data, ndeep=3)[1]
  25. except Exception as e:
  26. pass
  27. if (thumbnail or compress) and key:
  28. key = generate_new_key(key, data)
  29. token = auth.upload_token(bucket, key=key)
  30. ret, _ = qiniu.put_data(token, key, data, mime_type=mime_type)
  31. return ret.get('key')
  32. def upload_file_admin(obj, key=None, mime_type='application/octet-stream', bucket=QINIU['bucket_default'], compress=True, thumbnail=True, thumbnailw=1080, thumbnailh=720):
  33. # Django Admin Upload
  34. if not obj.image:
  35. return ''
  36. obj.image.seek(0)
  37. return upload(obj.image.read(), key=key or obj.image.name, mime_type=mime_type, bucket=bucket, compress=compress, thumbnail=thumbnail, thumbnailw=thumbnailw, thumbnailh=thumbnailh)
  38. def upload_file_req(file, key=None, mime_type='application/octet-stream', bucket=QINIU['bucket_default'], compress=True, thumbnail=True, thumbnailw=1080, thumbnailh=720):
  39. # photo = request.FILES.get('photo', '')
  40. # <InMemoryUploadedFile: photo.png (image/png)>
  41. if not file:
  42. return ''
  43. file.seek(0)
  44. return upload(file.read(), key=key or file.name, mime_type=mime_type, bucket=bucket, compress=compress, thumbnail=thumbnail, thumbnailw=thumbnailw, thumbnailh=thumbnailh)
  45. def upload_file_path(path, key=None, mime_type='application/octet-stream', bucket=QINIU['bucket_default'], compress=True):
  46. if not path:
  47. return ''
  48. token = auth.upload_token(bucket, key=key)
  49. ret, _ = qiniu.put_file(token, key, path, mime_type=mime_type)
  50. return ret.get('key')
  51. def qiniu_file_url(key, bucket=QINIU['bucket_default']):
  52. if not key:
  53. return ''
  54. return '{}/{}'.format(QINIU['buckets'][bucket] if settings.QINIU_FILE_URL_HTTPS else settings.QINIU_FILE_URL_AFTER, key)