Created
August 19, 2020 04:04
-
-
Save anhtran/c13d9d1a4181c3f71bf9fe858f3a7703 to your computer and use it in GitHub Desktop.
How to check duplicate files in django-filer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import hashlib | |
from filer.models import Image | |
def check(file: Image, user=None): | |
""" | |
Check duplicate files in django-filer | |
""" | |
sha1 = hashlib.sha1() | |
for chunk in file.chunks(): | |
sha1.update(chunk) | |
qs = Image.objects.filter(sha1=sha1.hexdigest()) | |
if user: | |
qs = qs.filter(owner=user) | |
dup_im = qs.first() | |
if dup_im: | |
filer_file = dup_im | |
else: | |
filer_file = Image.objects.create( | |
owner=user, | |
is_public=True, | |
file=file, | |
original_filename=file.name, | |
name='whatever', | |
folder=folder | |
) | |
return filer_file |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment