attachment.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. import os
  2. from hashlib import md5
  3. from io import BytesIO
  4. from django.conf import settings
  5. from django.core.files import File
  6. from django.core.files.base import ContentFile
  7. from django.core.urlresolvers import reverse
  8. from django.db import models
  9. from django.utils import timezone
  10. from django.utils.crypto import get_random_string
  11. from PIL import Image
  12. from misago.core.utils import slugify
  13. def upload_to(instance, filename):
  14. spread_path = md5(str(instance.secret[:16]).encode()).hexdigest()
  15. secret = Attachment.generate_new_secret()
  16. filename_lowered = filename.lower().strip()
  17. for extension in instance.filetype.extensions_list:
  18. if filename_lowered.endswith(extension):
  19. break
  20. filename_clean = u'.'.join((
  21. slugify(filename[:(len(extension) + 1) * -1])[:16],
  22. extension
  23. ))
  24. return os.path.join('attachments', spread_path[:2], spread_path[2:4], secret, filename_clean)
  25. class Attachment(models.Model):
  26. secret = models.CharField(max_length=64)
  27. filetype = models.ForeignKey('AttachmentType')
  28. post = models.ForeignKey(
  29. 'Post',
  30. blank=True,
  31. null=True,
  32. on_delete=models.SET_NULL
  33. )
  34. uploaded_on = models.DateTimeField(default=timezone.now)
  35. uploader = models.ForeignKey(
  36. settings.AUTH_USER_MODEL,
  37. blank=True,
  38. null=True,
  39. on_delete=models.SET_NULL
  40. )
  41. uploader_name = models.CharField(max_length=255)
  42. uploader_slug = models.CharField(max_length=255)
  43. uploader_ip = models.GenericIPAddressField()
  44. filename = models.CharField(max_length=255)
  45. size = models.PositiveIntegerField(default=0)
  46. thumbnail = models.ImageField(blank=True, null=True, upload_to=upload_to)
  47. image = models.ImageField(blank=True, null=True, upload_to=upload_to)
  48. file = models.FileField(blank=True, null=True, upload_to=upload_to)
  49. def delete(self, *args, **kwargs):
  50. self.delete_files()
  51. return super(Attachment, self).delete(*args, **kwargs)
  52. def delete_files(self):
  53. if self.thumbnail:
  54. self.thumbnail.delete(save=False)
  55. if self.image:
  56. self.image.delete(save=False)
  57. if self.file:
  58. self.file.delete(save=False)
  59. @classmethod
  60. def generate_new_secret(cls):
  61. return get_random_string(settings.MISAGO_ATTACHMENT_SECRET_LENGTH)
  62. @property
  63. def is_image(self):
  64. return bool(self.image)
  65. @property
  66. def is_file(self):
  67. return not self.is_image
  68. def get_absolute_url(self):
  69. return reverse('misago:attachment', kwargs={
  70. 'pk': self.pk,
  71. 'secret': self.secret,
  72. })
  73. def get_thumbnail_url(self):
  74. if self.thumbnail:
  75. return reverse('misago:attachment-thumbnail', kwargs={
  76. 'pk': self.pk,
  77. 'secret': self.secret,
  78. })
  79. else:
  80. return None
  81. def set_file(self, upload):
  82. self.file = File(upload, upload.name)
  83. def set_image(self, upload):
  84. fileformat = self.filetype.extensions_list[0]
  85. self.image = File(upload, upload.name)
  86. thumbnail = Image.open(upload)
  87. downscale_image = (
  88. thumbnail.size[0] > settings.MISAGO_ATTACHMENT_IMAGE_SIZE_LIMIT[0] or
  89. thumbnail.size[1] > settings.MISAGO_ATTACHMENT_IMAGE_SIZE_LIMIT[1]
  90. )
  91. strip_animation = fileformat == 'gif'
  92. thumb_stream = BytesIO()
  93. if downscale_image:
  94. thumbnail.thumbnail(settings.MISAGO_ATTACHMENT_IMAGE_SIZE_LIMIT)
  95. if fileformat == 'jpg':
  96. # normalize jpg to jpeg for Pillow
  97. thumbnail.save(thumb_stream, 'jpeg')
  98. else:
  99. thumbnail.save(thumb_stream, fileformat)
  100. elif strip_animation:
  101. thumbnail.save(thumb_stream, fileformat)
  102. if downscale_image or strip_animation:
  103. self.thumbnail = ContentFile(thumb_stream.getvalue(), upload.name)