##// 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 5 from random import random
6 6
7 7 from django.db import models
8 from boards import utils
8 9
9 10 from boards.models.attachment.viewers import get_viewers, AbstractViewer
10 11
@@ -15,26 +16,17 b" FILE_EXTENSION_DELIMITER = '.'"
15 16
16 17 class AttachmentManager(models.Manager):
17 18 def create_with_hash(self, file):
18 file_hash = self.get_hash(file)
19 file_hash = utils.get_file_hash(file)
19 20 existing = self.filter(hash=file_hash)
20 21 if len(existing) > 0:
21 22 attachment = existing[0]
22 23 else:
23 24 file_type = file.name.split(FILE_EXTENSION_DELIMITER)[-1].lower()
24 attachment = Attachment.objects.create(file=file,
25 mimetype=file_type, hash=file_hash)
25 attachment = Attachment.objects.create(
26 file=file, mimetype=file_type, hash=file_hash)
26 27
27 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 31 class Attachment(models.Model):
40 32 objects = AttachmentManager()
@@ -6,7 +6,7 b' import time'
6 6 from django.db import models
7 7 from django.template.defaultfilters import filesizeformat
8 8
9 from boards import thumbs
9 from boards import thumbs, utils
10 10 import boards
11 11 from boards.models.base import Viewable
12 12
@@ -24,7 +24,7 b" CSS_CLASS_THUMB = 'thumb'"
24 24
25 25 class PostImageManager(models.Manager):
26 26 def create_with_hash(self, image):
27 image_hash = self.get_hash(image)
27 image_hash = utils.get_file_hash(image)
28 28 existing = self.filter(hash=image_hash)
29 29 if len(existing) > 0:
30 30 post_image = existing[0]
@@ -33,15 +33,6 b' class PostImageManager(models.Manager):'
33 33
34 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 36 def get_random_images(self, count, include_archived=False, tags=None):
46 37 images = self.filter(post_images__thread__archived=include_archived)
47 38 if tags is not None:
@@ -89,7 +80,7 b' class PostImage(models.Model, Viewable):'
89 80 """
90 81
91 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 84 super(PostImage, self).save(*args, **kwargs)
94 85
95 86 def __str__(self):
@@ -1,6 +1,7 b''
1 1 """
2 2 This module contains helper functions and helper classes.
3 3 """
4 import hashlib
4 5 import time
5 6 import hmac
6 7
@@ -81,4 +82,11 b' def is_moderator(request):'
81 82 except AttributeError:
82 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