Skip to content

Instantly share code, notes, and snippets.

@anhtran
Created August 19, 2020 04:04
Show Gist options
  • Save anhtran/c13d9d1a4181c3f71bf9fe858f3a7703 to your computer and use it in GitHub Desktop.
Save anhtran/c13d9d1a4181c3f71bf9fe858f3a7703 to your computer and use it in GitHub Desktop.
How to check duplicate files in django-filer
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