uploaded.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. from hashlib import sha256
  2. from path import path
  3. from PIL import Image
  4. from django.core.exceptions import ValidationError
  5. from django.utils.translation import ugettext as _
  6. from misago.conf import settings
  7. from misago.users.avatars import store
  8. ALLOWED_EXTENSIONS = ('.gif', '.png', '.jpg', '.jpeg')
  9. ALLOWED_MIME_TYPES = ('image/gif', 'image/jpeg', 'image/png')
  10. def validate_file_size(uploaded_file):
  11. upload_limit = settings.avatar_upload_limit * 1024
  12. if uploaded_file.size > upload_limit:
  13. raise ValidationError(_("Uploaded file is too big."))
  14. def validate_extension(uploaded_file):
  15. lowercased_name = uploaded_file.name.lower()
  16. for extension in ALLOWED_EXTENSIONS:
  17. if lowercased_name.endswith(extension):
  18. return True
  19. else:
  20. raise ValidationError(_("Uploaded file type is not allowed."))
  21. def validate_mime(uploaded_file):
  22. if uploaded_file.content_type not in ALLOWED_MIME_TYPES:
  23. raise ValidationError(_("Uploaded file type is not allowed."))
  24. def validate_dimensions(uploaded_file):
  25. image = Image.open(uploaded_file)
  26. if min(image.size) < 100:
  27. message = _("Uploaded image should be at "
  28. "least 100 pixels tall and wide.")
  29. raise ValidationError(message)
  30. if image.size[0] * image.size[1] > 2000 * 3000:
  31. message = _("Uploaded image is too big.")
  32. raise ValidationError(message)
  33. image_ratio = float(min(image.size)) / float(max(image.size))
  34. if image_ratio < 0.25:
  35. message = _("Uploaded image ratio cannot be greater than 16:9.")
  36. raise ValidationError(message)
  37. return image
  38. def validate_uploaded_file(uploaded_file):
  39. try:
  40. validate_file_size(uploaded_file)
  41. validate_extension(uploaded_file)
  42. validate_mime(uploaded_file)
  43. return validate_dimensions(uploaded_file)
  44. except ValidationError as e:
  45. try:
  46. temporary_file_path = path(uploaded_file.temporary_file_path())
  47. if temporary_file_path.exists():
  48. temporary_file_path.remove()
  49. except Exception:
  50. pass
  51. raise e
  52. def handle_uploaded_file(user, uploaded_file):
  53. image = validate_uploaded_file(uploaded_file)
  54. store.store_temporary_avatar(user, image)
  55. def crop_string_to_dict(image, crop):
  56. message = _("Crop is invalid. Please try again.")
  57. crop_dict = {}
  58. try:
  59. crop_list = [int(x) for x in crop.split(',')]
  60. if len(crop_list) != 8:
  61. raise ValidationError(message)
  62. except (TypeError, ValueError):
  63. raise ValidationError(message)
  64. cropped_size = (crop_list[0], crop_list[1])
  65. if cropped_size[0] < 10:
  66. raise ValidationError(message)
  67. if cropped_size[1] < 10:
  68. raise ValidationError(message)
  69. if crop_list[2] != crop_list[3]:
  70. # We only allow cropping to squares
  71. raise ValidationError(message)
  72. crop_dict['width'] = crop_list[2]
  73. crop_dict['height'] = crop_list[3]
  74. if crop_dict['width'] != crop_dict['height']:
  75. raise ValidationError(message)
  76. if crop_dict['width'] > cropped_size[0]:
  77. raise ValidationError(message)
  78. if crop_dict['height'] > cropped_size[1]:
  79. raise ValidationError(message)
  80. crop_dict['source'] = (crop_list[4], crop_list[6],
  81. crop_list[5], crop_list[7])
  82. if crop_dict['source'][0] < 0 or crop_dict['source'][2] > cropped_size[0]:
  83. raise ValidationError(message)
  84. if crop_dict['source'][1] < 0 or crop_dict['source'][3] > cropped_size[1]:
  85. raise ValidationError(message)
  86. source_w = crop_dict['source'][2] - crop_dict['source'][0]
  87. source_h = crop_dict['source'][3] - crop_dict['source'][1]
  88. if source_w != source_h:
  89. raise ValidationError(message)
  90. crop_dict['ratio'] = float(image.size[0]) / float(cropped_size[0])
  91. return crop_dict
  92. def crop_source_image(user, source, crop):
  93. image = Image.open(store.avatar_file_path(user, source))
  94. crop = crop_string_to_dict(image, crop)
  95. crop_dimensions = [int(d * crop['ratio']) for d in crop['source']]
  96. cropped_image = image.crop(crop_dimensions)
  97. store.store_avatar(user, cropped_image)
  98. if source == 'tmp':
  99. store.store_original_avatar(user)
  100. return crop
  101. def avatar_source_token(user, source):
  102. token_seed = (
  103. unicode(user.pk),
  104. user.username,
  105. user.email,
  106. source,
  107. unicode(store.avatar_file_path(user, source)),
  108. settings.SECRET_KEY
  109. )
  110. return sha256('+'.join(token_seed)).hexdigest()[:10]
  111. def has_temporary_avatar(user):
  112. return store.avatar_file_exists(user, 'tmp')
  113. def has_original_avatar(user):
  114. return store.avatar_file_exists(user, 'org')