拍爱

storage_utils.py 3.7KB

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