##// 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 3 import boards
2 4 from boards.models import STATUS_ARCHIVE
3 5 from django.core.files.images import get_image_dimensions
@@ -12,10 +14,8 b' from boards.utils import get_upload_file'
12 14 class AttachmentManager(models.Manager):
13 15 def create_with_hash(self, file):
14 16 file_hash = utils.get_file_hash(file)
15 existing = self.filter(hash=file_hash)
16 if len(existing) > 0:
17 attachment = existing[0]
18 else:
17 attachment = self._get_existing(file_hash, file)
18 if not attachment:
19 19 # FIXME Use full mimetype here, need to modify viewers too
20 20 file_type = get_extension(file.name)
21 21 attachment = self.create(file=file, mimetype=file_type,
@@ -38,6 +38,23 b' class AttachmentManager(models.Manager):'
38 38 images = images.filter(attachment_posts__threads__tags__in=tags)
39 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 59 class Attachment(models.Model):
43 60 objects = AttachmentManager()
General Comments 0
You need to be logged in to leave comments. Login now