diff --git a/boards/models/attachment/__init__.py b/boards/models/attachment/__init__.py --- a/boards/models/attachment/__init__.py +++ b/boards/models/attachment/__init__.py @@ -5,6 +5,7 @@ import time from random import random from django.db import models +from boards import utils from boards.models.attachment.viewers import get_viewers, AbstractViewer @@ -15,26 +16,17 @@ FILE_EXTENSION_DELIMITER = '.' class AttachmentManager(models.Manager): def create_with_hash(self, file): - file_hash = self.get_hash(file) + file_hash = utils.get_file_hash(file) existing = self.filter(hash=file_hash) if len(existing) > 0: attachment = existing[0] else: file_type = file.name.split(FILE_EXTENSION_DELIMITER)[-1].lower() - attachment = Attachment.objects.create(file=file, - mimetype=file_type, hash=file_hash) + attachment = Attachment.objects.create( + file=file, mimetype=file_type, hash=file_hash) return attachment - def get_hash(self, file): - """ - Gets hash of an file. - """ - md5 = hashlib.md5() - for chunk in file.chunks(): - md5.update(chunk) - return md5.hexdigest() - class Attachment(models.Model): objects = AttachmentManager() diff --git a/boards/models/image.py b/boards/models/image.py --- a/boards/models/image.py +++ b/boards/models/image.py @@ -6,7 +6,7 @@ import time from django.db import models from django.template.defaultfilters import filesizeformat -from boards import thumbs +from boards import thumbs, utils import boards from boards.models.base import Viewable @@ -24,7 +24,7 @@ CSS_CLASS_THUMB = 'thumb' class PostImageManager(models.Manager): def create_with_hash(self, image): - image_hash = self.get_hash(image) + image_hash = utils.get_file_hash(image) existing = self.filter(hash=image_hash) if len(existing) > 0: post_image = existing[0] @@ -33,15 +33,6 @@ class PostImageManager(models.Manager): return post_image - def get_hash(self, image): - """ - Gets hash of an image. - """ - md5 = hashlib.md5() - for chunk in image.chunks(): - md5.update(chunk) - return md5.hexdigest() - def get_random_images(self, count, include_archived=False, tags=None): images = self.filter(post_images__thread__archived=include_archived) if tags is not None: @@ -89,7 +80,7 @@ class PostImage(models.Model, Viewable): """ if not self.pk and self.image: - self.hash = PostImage.objects.get_hash(self.image) + self.hash = utils.get_file_hash(self.image) super(PostImage, self).save(*args, **kwargs) def __str__(self): diff --git a/boards/utils.py b/boards/utils.py --- a/boards/utils.py +++ b/boards/utils.py @@ -1,6 +1,7 @@ """ This module contains helper functions and helper classes. """ +import hashlib import time import hmac @@ -81,4 +82,11 @@ def is_moderator(request): except AttributeError: moderate = False - return moderate \ No newline at end of file + return moderate + + +def get_file_hash(file) -> str: + md5 = hashlib.md5() + for chunk in file.chunks(): + md5.update(chunk) + return md5.hexdigest()