拍爱

storage_utils.py 3.6KB

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