##// END OF EJS Templates
Do not rely on the md5 hash of the file, compare the file contents when searching for duplicate
neko259 -
r1824:d33ed39f default
parent child Browse files
Show More
@@ -1,3 +1,5 b''
1 from itertools import zip_longest
2
1 import boards
3 import boards
2 from boards.models import STATUS_ARCHIVE
4 from boards.models import STATUS_ARCHIVE
3 from django.core.files.images import get_image_dimensions
5 from django.core.files.images import get_image_dimensions
@@ -12,10 +14,8 b' from boards.utils import get_upload_file'
12 class AttachmentManager(models.Manager):
14 class AttachmentManager(models.Manager):
13 def create_with_hash(self, file):
15 def create_with_hash(self, file):
14 file_hash = utils.get_file_hash(file)
16 file_hash = utils.get_file_hash(file)
15 existing = self.filter(hash=file_hash)
17 attachment = self._get_existing(file_hash, file)
16 if len(existing) > 0:
18 if not attachment:
17 attachment = existing[0]
18 else:
19 # FIXME Use full mimetype here, need to modify viewers too
19 # FIXME Use full mimetype here, need to modify viewers too
20 file_type = get_extension(file.name)
20 file_type = get_extension(file.name)
21 attachment = self.create(file=file, mimetype=file_type,
21 attachment = self.create(file=file, mimetype=file_type,
@@ -38,6 +38,23 b' class AttachmentManager(models.Manager):'
38 images = images.filter(attachment_posts__threads__tags__in=tags)
38 images = images.filter(attachment_posts__threads__tags__in=tags)
39 return images.order_by('?')[:count]
39 return images.order_by('?')[:count]
40
40
41 def _get_existing(self, file_hash, file):
42 """
43 Gets an attachment with the same file if one exists.
44 """
45 existing = self.filter(hash=file_hash)
46 attachment = None
47 for existing_attachment in existing:
48 equal = True
49 existing_file = existing_attachment.file
50 for chunk, existing_chunk in zip_longest(file.chunks(), existing_file.chunks()):
51 if chunk != existing_chunk:
52 equal = False
53 break
54 if equal:
55 attachment = existing[0]
56 return attachment
57
41
58
42 class Attachment(models.Model):
59 class Attachment(models.Model):
43 objects = AttachmentManager()
60 objects = AttachmentManager()
General Comments 0
You need to be logged in to leave comments. Login now