Importer updates: watch directories, handle metadata updates

This commit is contained in:
Agate 2020-05-07 09:55:29 +02:00
parent a179229f6d
commit 6eb049b2d9
16 changed files with 1005 additions and 120 deletions

View file

@ -1,4 +1,5 @@
import datetime
import hashlib
from django.core.files.base import ContentFile
from django.http import request
@ -458,3 +459,19 @@ def monkey_patch_request_build_absolute_uri():
request.HttpRequest.scheme = property(scheme)
request.HttpRequest.get_host = get_host
def get_file_hash(file, algo=None, chunk_size=None, full_read=False):
algo = algo or settings.HASHING_ALGORITHM
chunk_size = chunk_size or settings.HASHING_CHUNK_SIZE
handler = getattr(hashlib, algo)
hash = handler()
file.seek(0)
if full_read:
for byte_block in iter(lambda: file.read(chunk_size), b""):
hash.update(byte_block)
else:
# sometimes, it's useful to only hash the beginning of the file, e.g
# to avoid a lot of I/O when crawling large libraries
hash.update(file.read(chunk_size))
return "{}:{}".format(algo, hash.hexdigest())