Brak opisu

storage_utils.py 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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.thumbnail_utils import make_thumbnail
  10. from utils.watermark_utils import watermark_wrap
  11. class DotDict(dict):
  12. """ dot.notation access to dictionary attributes """
  13. def __getattr__(self, attr):
  14. return self.get(attr)
  15. __setattr__ = dict.__setitem__
  16. __delattr__ = dict.__delitem__
  17. @transaction.atomic
  18. def file_save(file_=None, file_path=None, prefix='img', ext='.jpeg', watermark=False, thumbnail=False):
  19. # Photo
  20. file_ = file_ or default_storage.open(file_path)
  21. # Ext
  22. ext = os.path.splitext(file_.name)[-1] or ext
  23. # Photo MD5
  24. photo_md5 = calculate_md5(file_)
  25. # Photo UUID Get or Create
  26. photo, created = PhotoUUIDInfo.objects.select_for_update().get_or_create(photo_md5=photo_md5)
  27. # 无水印
  28. if not photo.photo_path:
  29. photo_path = '{}/{}{}'.format(prefix, shortuuid.uuid(), ext)
  30. if default_storage.exists(photo_path):
  31. default_storage.delete(photo_path)
  32. default_storage.save(photo_path, file_)
  33. photo.photo_path = photo_path
  34. photo.save()
  35. # 有水印
  36. if watermark:
  37. if not photo.photo_watermark_path:
  38. if settings.WATERMARK_OR_NOT:
  39. photo_watermark_path = 'photo/{}{}'.format(shortuuid.uuid(), ext)
  40. watermark_wrap(
  41. os.path.join(settings.MEDIA_ROOT, photo_path).replace('\\', '/'),
  42. settings.WATERMARK_LOGO_PATH,
  43. os.path.join(settings.MEDIA_ROOT, photo_watermark_path).replace('\\', '/')
  44. )
  45. photo.photo_watermark_path = photo_watermark_path
  46. else:
  47. photo.photo_watermark_path = photo_path
  48. photo.save()
  49. # 缩略图
  50. if thumbnail:
  51. if not photo.photo_thumbnail_path:
  52. # 双列: 540, 40-50K
  53. photo_thumbnail_path = photo_path.replace('.', '_thumbnail.')
  54. photo_w, photo_h, photo_thumbnail_w, photo_thumbnail_h = make_thumbnail(
  55. os.path.join(settings.MEDIA_ROOT, photo_path).replace('\\', '/'),
  56. os.path.join(settings.MEDIA_ROOT, photo_thumbnail_path).replace('\\', '/'),
  57. settings.THUMBNAIL_MAX_WIDTH
  58. )
  59. photo.photo_w = photo_w
  60. photo.photo_h = photo_h
  61. photo.photo_thumbnail_path = photo_thumbnail_path
  62. photo.photo_thumbnail_w = photo_thumbnail_w
  63. photo.photo_thumbnail_h = photo_thumbnail_h
  64. if not photo.photo_thumbnail2_path:
  65. # 单列: 1080, xx-100K
  66. photo_thumbnail2_path = photo_path.replace('.', '_thumbnail2.')
  67. photo_w, photo_h, photo_thumbnail2_w, photo_thumbnail2_h = make_thumbnail(
  68. os.path.join(settings.MEDIA_ROOT, photo_path).replace('\\', '/'),
  69. os.path.join(settings.MEDIA_ROOT, photo_thumbnail2_path).replace('\\', '/'),
  70. settings.THUMBNAIL_MAX_WIDTH2
  71. )
  72. if watermark and settings.WATERMARK_OR_NOT:
  73. watermark_wrap(
  74. os.path.join(settings.MEDIA_ROOT, photo_thumbnail2_path).replace('\\', '/'),
  75. settings.WATERMARK_LOGO_PATH,
  76. os.path.join(settings.MEDIA_ROOT, photo_thumbnail2_path).replace('\\', '/')
  77. )
  78. photo.photo_w = photo_w
  79. photo.photo_h = photo_h
  80. photo.photo_thumbnail2_path = photo_thumbnail2_path
  81. photo.photo_thumbnail2_w = photo_thumbnail2_w
  82. photo.photo_thumbnail2_h = photo_thumbnail2_h
  83. photo.save()
  84. return DotDict({
  85. 'ext': ext,
  86. 'photo_md5': photo_md5,
  87. 'photo_path': photo.photo_path,
  88. 'photo_w': photo.photo_w,
  89. 'photo_h': photo.photo_h,
  90. 'photo_watermark_path': photo.photo_watermark_path,
  91. 'photo_thumbnail_path': photo.photo_thumbnail_path,
  92. 'photo_thumbnail_w': photo.photo_thumbnail_w,
  93. 'photo_thumbnail_h': photo.photo_thumbnail_h,
  94. 'photo_thumbnail2_path': photo.photo_thumbnail2_path,
  95. 'photo_thumbnail2_w': photo.photo_thumbnail2_w,
  96. 'photo_thumbnail2_h': photo.photo_thumbnail2_h,
  97. })