説明なし

storage_qiniu_utils.py 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. # -*- coding: utf-8 -*-
  2. import os
  3. import shortuuid
  4. from django.conf import settings
  5. from django.core.files.storage import default_storage
  6. from django.db import transaction
  7. from django_file_md5 import calculate_md5
  8. from photo.models import PhotoUUIDInfo
  9. from utils.qiniucdn import upload_file_path
  10. from utils.thumbnail_utils import make_thumbnail
  11. from utils.watermark_utils import watermark_wrap
  12. class DotDict(dict):
  13. """ dot.notation access to dictionary attributes """
  14. def __getattr__(self, attr):
  15. return self.get(attr)
  16. __setattr__ = dict.__setitem__
  17. __delattr__ = dict.__delitem__
  18. def file_abspath(file_path=None):
  19. return os.path.join(settings.MEDIA_ROOT, file_path).replace('\\', '/')
  20. @transaction.atomic
  21. def file_save(file_=None, file_path=None, prefix='img', ext='.jpeg', watermark=False, thumbnail=False):
  22. photo_path, photo_watermark_path, photo_thumbnail_path, photo_thumbnail2_path = '', '', '', ''
  23. # Photo
  24. file_ = file_ or default_storage.open(file_path)
  25. # Ext
  26. ext = os.path.splitext(file_.name)[-1] or ext
  27. # Photo MD5
  28. photo_md5 = calculate_md5(file_)
  29. # Photo UUID Get or Create
  30. photo, created = PhotoUUIDInfo.objects.select_for_update().get_or_create(photo_md5=photo_md5)
  31. # 无水印
  32. if not photo.photo_path:
  33. photo_path = '{}/{}{}'.format(prefix, shortuuid.uuid(), ext)
  34. if default_storage.exists(photo_path):
  35. default_storage.delete(photo_path)
  36. default_storage.save(photo_path, file_)
  37. photo_path_qiniu = upload_file_path(file_abspath(photo_path), bucket='photo')
  38. photo.photo_path = photo_path_qiniu
  39. photo.save()
  40. else:
  41. if ((watermark and not photo.photo_watermark_path) or (thumbnail and not (photo.photo_thumbnail_path and photo.photo_thumbnail2_path))) and (not default_storage.exists(photo.photo_path)):
  42. default_storage.save(photo.photo_path, file_)
  43. # 有水印
  44. if watermark:
  45. if not photo.photo_watermark_path:
  46. if settings.WATERMARK_OR_NOT:
  47. photo_watermark_path = 'photo/{}{}'.format(shortuuid.uuid(), ext)
  48. watermark_wrap(
  49. file_abspath(photo_path),
  50. settings.WATERMARK_LOGO_PATH,
  51. file_abspath(photo_watermark_path)
  52. )
  53. photo_watermark_path_qiniu = upload_file_path(file_abspath(photo_watermark_path), bucket='watermark')
  54. photo.photo_watermark_path = photo_watermark_path_qiniu
  55. else:
  56. photo.photo_watermark_path = photo_path_qiniu
  57. photo.save()
  58. # 缩略图
  59. if thumbnail:
  60. if not photo.photo_thumbnail_path:
  61. # 双列: 540, 40-50K
  62. photo_thumbnail_path = photo_path.replace('.', '_thumbnail.')
  63. photo_w, photo_h, photo_thumbnail_w, photo_thumbnail_h = make_thumbnail(
  64. file_abspath(photo_path),
  65. file_abspath(photo_thumbnail_path),
  66. settings.THUMBNAIL_MAX_WIDTH
  67. )
  68. photo_thumbnail_path_qiniu = upload_file_path(file_abspath(photo_thumbnail_path), bucket='thumbnail')
  69. photo.photo_w = photo_w
  70. photo.photo_h = photo_h
  71. photo.photo_thumbnail_path = photo_thumbnail_path_qiniu
  72. photo.photo_thumbnail_w = photo_thumbnail_w
  73. photo.photo_thumbnail_h = photo_thumbnail_h
  74. if not photo.photo_thumbnail2_path:
  75. # 单列: 1080, xx-100K
  76. photo_thumbnail2_path = photo_path.replace('.', '_thumbnail2.')
  77. photo_w, photo_h, photo_thumbnail2_w, photo_thumbnail2_h = make_thumbnail(
  78. file_abspath(photo_path),
  79. file_abspath(photo_thumbnail2_path),
  80. settings.THUMBNAIL_MAX_WIDTH2
  81. )
  82. if watermark and settings.WATERMARK_OR_NOT:
  83. watermark_wrap(
  84. file_abspath(photo_thumbnail2_path),
  85. settings.WATERMARK_LOGO_PATH,
  86. file_abspath(photo_thumbnail2_path)
  87. )
  88. photo_thumbnail2_path_qiniu = upload_file_path(file_abspath(photo_thumbnail2_path), bucket='thumbnail2')
  89. photo.photo_w = photo_w
  90. photo.photo_h = photo_h
  91. photo.photo_thumbnail2_path = photo_thumbnail2_path_qiniu
  92. photo.photo_thumbnail2_w = photo_thumbnail2_w
  93. photo.photo_thumbnail2_h = photo_thumbnail2_h
  94. photo.save()
  95. # 本地删除
  96. for path in [photo_path, photo_watermark_path, photo_thumbnail_path, photo_thumbnail2_path]:
  97. if path and default_storage.exists(path):
  98. default_storage.delete(path)
  99. return DotDict({
  100. 'ext': ext,
  101. 'photo_md5': photo_md5,
  102. 'photo_path': photo.photo_path,
  103. 'photo_w': photo.photo_w,
  104. 'photo_h': photo.photo_h,
  105. 'photo_watermark_path': photo.photo_watermark_path,
  106. 'photo_thumbnail_path': photo.photo_thumbnail_path,
  107. 'photo_thumbnail_w': photo.photo_thumbnail_w,
  108. 'photo_thumbnail_h': photo.photo_thumbnail_h,
  109. 'photo_thumbnail2_path': photo.photo_thumbnail2_path,
  110. 'photo_thumbnail2_w': photo.photo_thumbnail2_w,
  111. 'photo_thumbnail2_h': photo.photo_thumbnail2_h,
  112. })