拍爱

storage_utils.py 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 filemd5 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 UUID Get or Create
  24. photo, created = PhotoUUIDInfo.objects.select_for_update().get_or_create(photo_md5=calculate_md5(file_))
  25. # 照片路径
  26. photo_path = photo.photo_path
  27. if not photo_path:
  28. photo_path = '{}/{}{}'.format(prefix, shortuuid.uuid(), ext)
  29. if default_storage.exists(photo_path):
  30. default_storage.delete(photo_path)
  31. default_storage.save(photo_path, file_)
  32. photo.photo_path = photo_path
  33. photo.save()
  34. # 水印
  35. if watermark:
  36. if not photo.photo_watermark_path:
  37. photo_watermark_path = 'photo/{}{}'.format(shortuuid.uuid(), ext)
  38. watermark_wrap(
  39. os.path.join(settings.MEDIA_ROOT, photo_path).replace('\\', '/'),
  40. settings.WATERMARK_LOGO,
  41. os.path.join(settings.MEDIA_ROOT, photo_watermark_path).replace('\\', '/')
  42. )
  43. photo.photo_watermark_path
  44. photo.save()
  45. # 缩略图
  46. if thumbnail:
  47. if not photo.photo_thumbnail_path:
  48. # 双列: 540, 40-50K
  49. photo_thumbnail_path = photo_path.replace('.', '_thumbnail.')
  50. photo_w, photo_h, photo_thumbnail_w, photo_thumbnail_h = make_thumbnail(
  51. os.path.join(settings.MEDIA_ROOT, photo_path).replace('\\', '/'),
  52. os.path.join(settings.MEDIA_ROOT, photo_thumbnail_path).replace('\\', '/'),
  53. settings.THUMBNAIL_MAX_WIDTH
  54. )
  55. photo.photo_w = photo_w
  56. photo.photo_h = photo_h
  57. photo.photo_thumbnail_path = photo_thumbnail_path
  58. photo.photo_thumbnail_w = photo_thumbnail_w
  59. photo.photo_thumbnail_h = photo_thumbnail_h
  60. if not photo.photo_thumbnail2_path:
  61. # 单列: 1080, xx-100K
  62. photo_thumbnail2_path = photo_path.replace('.', '_thumbnail2.')
  63. photo_w, photo_h, photo_thumbnail2_w, photo_thumbnail2_h = make_thumbnail(
  64. os.path.join(settings.MEDIA_ROOT, photo_path).replace('\\', '/'),
  65. os.path.join(settings.MEDIA_ROOT, photo_thumbnail2_path).replace('\\', '/'),
  66. settings.THUMBNAIL_MAX_WIDTH2
  67. )
  68. photo.photo_w = photo_w
  69. photo.photo_h = photo_h
  70. photo.photo_thumbnail2_path = photo_thumbnail2_path
  71. photo.photo_thumbnail2_w = photo_thumbnail2_w
  72. photo.photo_thumbnail2_h = photo_thumbnail2_h
  73. photo.save()
  74. return DotDict({
  75. 'ext': ext,
  76. 'photo_path': photo_path,
  77. 'photo_w': photo.photo_w,
  78. 'photo_h': photo.photo_h,
  79. 'photo_watermark_path': photo.photo_watermark_path,
  80. 'photo_thumbnail_path': photo.photo_thumbnail_path,
  81. 'photo_thumbnail_w': photo.photo_thumbnail_w,
  82. 'photo_thumbnail_h': photo.photo_thumbnail_h,
  83. 'photo_thumbnail2_path': photo.photo_thumbnail2_path,
  84. 'photo_thumbnail2_w': photo.photo_thumbnail2_w,
  85. 'photo_thumbnail2_h': photo.photo_thumbnail2_h,
  86. })