##// END OF EJS Templates
Deduplicated file hash calculation method
neko259 -
r1305:8d3d0486 default
parent child Browse files
Show More
@@ -5,6 +5,7 b' import time'
5 from random import random
5 from random import random
6
6
7 from django.db import models
7 from django.db import models
8 from boards import utils
8
9
9 from boards.models.attachment.viewers import get_viewers, AbstractViewer
10 from boards.models.attachment.viewers import get_viewers, AbstractViewer
10
11
@@ -15,26 +16,17 b" FILE_EXTENSION_DELIMITER = '.'"
15
16
16 class AttachmentManager(models.Manager):
17 class AttachmentManager(models.Manager):
17 def create_with_hash(self, file):
18 def create_with_hash(self, file):
18 file_hash = self.get_hash(file)
19 file_hash = utils.get_file_hash(file)
19 existing = self.filter(hash=file_hash)
20 existing = self.filter(hash=file_hash)
20 if len(existing) > 0:
21 if len(existing) > 0:
21 attachment = existing[0]
22 attachment = existing[0]
22 else:
23 else:
23 file_type = file.name.split(FILE_EXTENSION_DELIMITER)[-1].lower()
24 file_type = file.name.split(FILE_EXTENSION_DELIMITER)[-1].lower()
24 attachment = Attachment.objects.create(file=file,
25 attachment = Attachment.objects.create(
25 mimetype=file_type, hash=file_hash)
26 file=file, mimetype=file_type, hash=file_hash)
26
27
27 return attachment
28 return attachment
28
29
29 def get_hash(self, file):
30 """
31 Gets hash of an file.
32 """
33 md5 = hashlib.md5()
34 for chunk in file.chunks():
35 md5.update(chunk)
36 return md5.hexdigest()
37
38
30
39 class Attachment(models.Model):
31 class Attachment(models.Model):
40 objects = AttachmentManager()
32 objects = AttachmentManager()
@@ -6,7 +6,7 b' import time'
6 from django.db import models
6 from django.db import models
7 from django.template.defaultfilters import filesizeformat
7 from django.template.defaultfilters import filesizeformat
8
8
9 from boards import thumbs
9 from boards import thumbs, utils
10 import boards
10 import boards
11 from boards.models.base import Viewable
11 from boards.models.base import Viewable
12
12
@@ -24,7 +24,7 b" CSS_CLASS_THUMB = 'thumb'"
24
24
25 class PostImageManager(models.Manager):
25 class PostImageManager(models.Manager):
26 def create_with_hash(self, image):
26 def create_with_hash(self, image):
27 image_hash = self.get_hash(image)
27 image_hash = utils.get_file_hash(image)
28 existing = self.filter(hash=image_hash)
28 existing = self.filter(hash=image_hash)
29 if len(existing) > 0:
29 if len(existing) > 0:
30 post_image = existing[0]
30 post_image = existing[0]
@@ -33,15 +33,6 b' class PostImageManager(models.Manager):'
33
33
34 return post_image
34 return post_image
35
35
36 def get_hash(self, image):
37 """
38 Gets hash of an image.
39 """
40 md5 = hashlib.md5()
41 for chunk in image.chunks():
42 md5.update(chunk)
43 return md5.hexdigest()
44
45 def get_random_images(self, count, include_archived=False, tags=None):
36 def get_random_images(self, count, include_archived=False, tags=None):
46 images = self.filter(post_images__thread__archived=include_archived)
37 images = self.filter(post_images__thread__archived=include_archived)
47 if tags is not None:
38 if tags is not None:
@@ -89,7 +80,7 b' class PostImage(models.Model, Viewable):'
89 """
80 """
90
81
91 if not self.pk and self.image:
82 if not self.pk and self.image:
92 self.hash = PostImage.objects.get_hash(self.image)
83 self.hash = utils.get_file_hash(self.image)
93 super(PostImage, self).save(*args, **kwargs)
84 super(PostImage, self).save(*args, **kwargs)
94
85
95 def __str__(self):
86 def __str__(self):
@@ -1,6 +1,7 b''
1 """
1 """
2 This module contains helper functions and helper classes.
2 This module contains helper functions and helper classes.
3 """
3 """
4 import hashlib
4 import time
5 import time
5 import hmac
6 import hmac
6
7
@@ -81,4 +82,11 b' def is_moderator(request):'
81 except AttributeError:
82 except AttributeError:
82 moderate = False
83 moderate = False
83
84
84 return moderate No newline at end of file
85 return moderate
86
87
88 def get_file_hash(file) -> str:
89 md5 = hashlib.md5()
90 for chunk in file.chunks():
91 md5.update(chunk)
92 return md5.hexdigest()
General Comments 0
You need to be logged in to leave comments. Login now